MCPcopy Create free account
hub / github.com/cronitorio/cronitor-cli / handleUpdatePerform

Function handleUpdatePerform

cmd/dash.go:2935–3019  ·  view source on GitHub ↗

handleUpdatePerform handles POST requests to perform an update

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

2933
2934// handleUpdatePerform handles POST requests to perform an update
2935func handleUpdatePerform(w http.ResponseWriter, r *http.Request) {
2936 if r.Method != "POST" {
2937 http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
2938 return
2939 }
2940
2941 if isSafeModeEnabled {
2942 http.Error(w, "Updates are disabled in safe mode", http.StatusForbidden)
2943 return
2944 }
2945
2946 // Check for update first
2947 status, err := checkForUpdate()
2948 if err != nil {
2949 http.Error(w, err.Error(), http.StatusInternalServerError)
2950 return
2951 }
2952
2953 if !status.HasUpdate {
2954 http.Error(w, "No update available", http.StatusBadRequest)
2955 return
2956 }
2957
2958 // Set up SSE headers for streaming progress
2959 w.Header().Set("Content-Type", "text/event-stream")
2960 w.Header().Set("Cache-Control", "no-cache")
2961 w.Header().Set("Connection", "keep-alive")
2962
2963 // Function to send progress updates
2964 sendProgress := func(message string) {
2965 defer func() {
2966 if r := recover(); r != nil {
2967 // Connection was likely closed during update, silently ignore
2968 log(fmt.Sprintf("Progress update failed (connection closed): %v", r))
2969 }
2970 }()
2971
2972 // Check if w is nil
2973 if w == nil {
2974 return
2975 }
2976
2977 progressData, _ := json.Marshal(map[string]string{"progress": message})
2978 if _, err := fmt.Fprintf(w, "data: %s\n\n", progressData); err != nil {
2979 return
2980 }
2981
2982 // Simple flush without nested defer
2983 if flusher, ok := w.(http.Flusher); ok && flusher != nil {
2984 flusher.Flush() // This will be caught by the main defer/recover if it panics
2985 }
2986 }
2987
2988 sendProgress("Starting update process...")
2989
2990 // Perform the update in a goroutine
2991 go func() {
2992 if err := performUpdateWithRestart(status, sendProgress); err != nil {

Callers

nothing calls this directly

Calls 4

checkForUpdateFunction · 0.85
logFunction · 0.85
performUpdateWithRestartFunction · 0.85
ErrorMethod · 0.80

Tested by

no test coverage detected