Skip to content

Commit

Permalink
implement health check
Browse files Browse the repository at this point in the history
  • Loading branch information
consolethinks committed Jan 14, 2025
1 parent 87f00c8 commit dcec8bb
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 74 deletions.
14 changes: 6 additions & 8 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -468,17 +468,15 @@ components:
OtherHealthResponse:
type: object
properties:
ingestorStatus:
status:
type: string
description: Status of the ingestor.
scicatStatus:
type: string
description: Status of SciCat.
globusStatus:
type: string
description: Status of Globus.
errors:
type: object
additionalProperties:
type: string
required:
- ingestorStatus
- status
- scicatStatus
- globusStatus
GetExtractorResponse:
Expand Down
5 changes: 5 additions & 0 deletions cmd/openem-ingestor-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func main() {

ctx := context.Background()

// setup globus if we have a refresh token
if config.Transfer.Globus.RefreshToken != "" {
core.GlobusLoginWithRefreshToken(config.Transfer.Globus)
}

tq := core.TaskQueue{
Config: config,
AppContext: ctx,
Expand Down
14 changes: 6 additions & 8 deletions internal/core/globus.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ func GlobusIsClientReady() bool {
return globusClient.IsClientSet()
}

/*func GlobusHealthCheck() error {
// NOTE: this is not a proper health check and takes a long time to finish (~900ms)
_, err := globusClient.TransferGetTaskList(0, 1)
return err
}*/

func GlobusSetHttpClient(client *http.Client) {
globusClient = globus.HttpClientToGlobusClient(client)
}
Expand Down Expand Up @@ -99,14 +105,6 @@ func globusCheckTransfer(globusTaskId string) (bytesTransferred int, filesTransf
}

func GlobusTransfer(globusConf task.GlobusTransferConfig, task task.IngestionTask, taskCtx context.Context, localTaskId uuid.UUID, datasetFolder string, fileList []datasetIngestor.Datafile, notifier ProgressNotifier) error {
// check if globus client is properly set up, use refresh token if available
if !globusClient.IsClientSet() {
if globusConf.RefreshToken == "" {
return fmt.Errorf("globus: not logged into globus")
}
GlobusLoginWithRefreshToken(globusConf)
}

// transfer given filelist
var filePathList []string
var fileIsSymlinkList []bool
Expand Down
26 changes: 26 additions & 0 deletions internal/core/scicathealth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package core

import (
"errors"
"fmt"
"net/http"
)

func ScicatHealthTest(APIServer string) error {
// note: there's no function to use the /health endpoint in scicat-cli
// so here's a function that uses it.
resp, err := http.DefaultClient.Get(APIServer + "/health")
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode == 503 {
return errors.New("health check failed")
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d %s", resp.StatusCode, resp.Status)
}

return nil
}
102 changes: 49 additions & 53 deletions internal/webserver/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 24 additions & 5 deletions internal/webserver/other.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package webserver

import (
"context"

"github.com/SwissOpenEM/Ingestor/internal/core"
)

func (i *IngestorWebServerImplemenation) OtherControllerGetVersion(ctx context.Context, request OtherControllerGetVersionRequestObject) (OtherControllerGetVersionResponseObject, error) {
Expand All @@ -11,9 +13,26 @@ func (i *IngestorWebServerImplemenation) OtherControllerGetVersion(ctx context.C
}

func (i *IngestorWebServerImplemenation) OtherControllerGetHealth(ctx context.Context, request OtherControllerGetHealthRequestObject) (OtherControllerGetHealthResponseObject, error) {
return OtherControllerGetHealth200JSONResponse{
IngestorStatus: "placeholder",
ScicatStatus: "placeholder",
GlobusStatus: "placeholder",
}, nil
errors := map[string]string{}

err := core.ScicatHealthTest(i.taskQueue.Config.Scicat.Host)
if err != nil {
errors["scicat"] = err.Error()
}

/*err = core.GlobusHealthCheck()
if err != nil {
errors["globus"] = err.Error()
}*/

if len(errors) > 0 {
return OtherControllerGetHealth200JSONResponse{
Status: "error",
Errors: &errors,
}, nil
} else {
return OtherControllerGetHealth200JSONResponse{
Status: "ok",
}, nil
}
}

0 comments on commit dcec8bb

Please sign in to comment.