Skip to content

Commit 1696ce0

Browse files
feat: file health check
1 parent b82b338 commit 1696ce0

File tree

1 file changed

+115
-11
lines changed

1 file changed

+115
-11
lines changed

health.go

Lines changed: 115 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ func RunOpsHealthCheck(resp http.ResponseWriter, request *http.Request) {
732732
log.Printf("[ERROR] Failed running app health check: %s", err)
733733
}
734734

735+
appHealth.Result = ""
735736
openapiAppHealthChannel <- appHealth
736737
errorChannel <- err
737738
}()
@@ -758,10 +759,22 @@ func RunOpsHealthCheck(resp http.ResponseWriter, request *http.Request) {
758759
errorChannel <- err
759760
}()
760761

762+
fileHealthChannel := make(chan FileHealth)
763+
go func () {
764+
fileHealth, err := RunOpsFile(apiKey, orgId)
765+
if err != nil {
766+
log.Printf("[ERROR] Failed running file health check: %s", err)
767+
}
768+
769+
fileHealthChannel <- fileHealth
770+
errorChannel <- err
771+
}()
772+
761773
// Use channel for getting RunOpsWorkflow function results
762774
platformHealth.Apps = <-openapiAppHealthChannel
763775
platformHealth.PythonApps = <-pythonAppHealthChannel
764776
platformHealth.Datastore = <-datastoreHealthChannel
777+
platformHealth.FileOps = <-fileHealthChannel
765778
}
766779

767780
platformHealth.Workflows = <-workflowHealthChannel
@@ -1992,7 +2005,7 @@ func RunOpsDatastore(apikey, orgId string) (DatastoreHealth, error) {
19922005
}
19932006

19942007
// create datastore entry
1995-
PAYLOAD := `{"key": "SHUFFLE_HEALTH_CHECK", "value": "yesy"}, "category": "SHUFFLE_HEALTH_CHECK"}`
2008+
PAYLOAD := `{"key": "SHUFFLE_HEALTH_CHECK", "value": "yesy", "category": "SHUFFLE_HEALTH_CHECK"}`
19962009
url := fmt.Sprintf("%s/api/v1/orgs/%s/set_cache", baseUrl, orgId)
19972010
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(PAYLOAD)))
19982011
if err != nil {
@@ -2011,8 +2024,6 @@ func RunOpsDatastore(apikey, orgId string) (DatastoreHealth, error) {
20112024
log.Printf("[ERROR] Failed to send request (%s) for set_cache %s", url, err)
20122025
return datastoreHealth, err
20132026
}
2014-
2015-
log.Printf("Org-Id: %s", orgId)
20162027

20172028
datastoreHealth.Create = true
20182029
//read datastore entry
@@ -2064,11 +2075,20 @@ func RunOpsDatastore(apikey, orgId string) (DatastoreHealth, error) {
20642075
return datastoreHealth, nil
20652076
}
20662077

2067-
func RunFileHealthOps(baseUrl, apikey, orgId string) (FileHealth, error) {
2078+
func RunOpsFile(apikey, orgId string) (FileHealth, error) {
2079+
baseUrl := os.Getenv("SHUFFLE_CLOUDRUN_URL")
2080+
if len(baseUrl) == 0 {
2081+
baseUrl = "https://shuffler.io"
2082+
}
2083+
2084+
if project.Environment == "onprem" {
2085+
baseUrl = "http://localhost:5001"
2086+
}
2087+
20682088
fileHealth := FileHealth{
20692089
Create: false,
20702090
FileId: "",
2071-
GetFile: false,
2091+
Upload: false,
20722092
Delete: false,
20732093
}
20742094

@@ -2084,22 +2104,106 @@ func RunFileHealthOps(baseUrl, apikey, orgId string) (FileHealth, error) {
20842104
req.Header.Set("Authorization", "Bearer "+apikey)
20852105
req.Header.Set("Content-Type", "application/json")
20862106

2107+
var fileRespStruct struct {
2108+
Success bool `json:success`
2109+
Id string `json:id`
2110+
}
2111+
20872112
client := GetExternalClient(baseUrl)
20882113
resp, err := client.Do(req)
2089-
resp.Body.Close()
2114+
defer resp.Body.Close()
20902115
if err != nil {
20912116
log.Printf("[ERROR] Failed to send request (%s) for set_cache %s", url, err)
20922117
return fileHealth, err
20932118
}
20942119

2095-
if resp.StatusCode != 200 {
2096-
return fileHealth, errors.New("Failed to create cache internal server error not 200 status code")
2120+
body, err := io.ReadAll(resp.Body)
2121+
if err != nil {
2122+
log.Printf("[ERROR] Failed to read response body")
2123+
return fileHealth, err
20972124
}
2098-
2125+
2126+
2127+
err = json.Unmarshal(body, &fileRespStruct)
2128+
if err != nil {
2129+
log.Printf("[ERROR] Failed to unmarshal response")
2130+
return fileHealth, err
2131+
}
2132+
20992133
fileHealth.Create = true
2100-
//Read metadata
2101-
//Delete file
2134+
//Upload file
2135+
url = fmt.Sprintf("%s/api/v1/files/%s/upload", baseUrl, fileRespStruct.Id)
2136+
remoteUrl := "https://raw.githubusercontent.com/Shuffle/Shuffle/refs/heads/main/LICENSE"
2137+
2138+
resp, err = http.Get(remoteUrl)
2139+
if err != nil {
2140+
log.Printf("[ERROR] Failed to fetch remote file: %s", err)
2141+
return fileHealth, err
2142+
}
2143+
2144+
defer resp.Body.Close()
2145+
2146+
var buf bytes.Buffer
2147+
w := multipart.NewWriter(&buf)
2148+
formFile, err := w.CreateFormFile("shuffle_file", "file.txt")
2149+
if err != nil {
2150+
log.Printf("[ERROR] Failed to create form file: %s", err)
2151+
return fileHealth, err
2152+
}
2153+
2154+
if _, err := io.Copy(formFile, resp.Body); err != nil {
2155+
log.Printf("[ERROR] Failed to copy remote file to form: %s", err)
2156+
return fileHealth, err
2157+
}
2158+
2159+
w.Close()
2160+
req, err = http.NewRequest("POST", url, &buf)
2161+
if err != nil {
2162+
log.Printf("[ERROR] Failed to create upload request: %s", err)
2163+
return fileHealth, err
2164+
}
2165+
2166+
req.Header.Set("Authorization", "Bearer "+apikey)
2167+
req.Header.Set("Content-Type", w.FormDataContentType())
2168+
uploadResp, err := client.Do(req)
2169+
if err != nil {
2170+
log.Printf("[ERROR] Upload request failed: %s", err)
2171+
return fileHealth, err
2172+
}
21022173

2174+
defer uploadResp.Body.Close()
2175+
if uploadResp.StatusCode != 200 {
2176+
log.Printf("[ERROR] Failed to upload file, not 200 status code")
2177+
return fileHealth, fmt.Errorf("upload failed for file")
2178+
}
2179+
2180+
log.Printf("[INFO] Filed uploaded successfully to %s", url)
2181+
fileHealth.FileId = fileRespStruct.Id
2182+
fileHealth.Upload = true
2183+
//Delete file
2184+
url = fmt.Sprintf("%s/api/v1/files/%s?remove_metadata=true", baseUrl, fileRespStruct.Id)
2185+
req, err = http.NewRequest("DELETE", url, nil)
2186+
if err != nil {
2187+
log.Printf("[ERROR] Failed to create delete request: %s", err)
2188+
return fileHealth, err
2189+
}
2190+
2191+
req.Header.Set("Authorization", "Bearer "+apikey)
2192+
resp, err = client.Do(req)
2193+
if err != nil {
2194+
log.Printf("[ERROR] Failed to send delete request: %s", err)
2195+
return fileHealth, err
2196+
}
2197+
defer resp.Body.Close()
2198+
2199+
if resp.StatusCode != 200 {
2200+
log.Printf("[ERROR] Failed to delete file, not 200 status code")
2201+
return fileHealth, fmt.Errorf("delete failed for file")
2202+
}
2203+
2204+
log.Printf("[INFO] File %s deleted successfully with metadata.", fileRespStruct.Id)
2205+
fileHealth.Delete = true
2206+
21032207
return fileHealth, nil
21042208
}
21052209

0 commit comments

Comments
 (0)