From ccfb55e263ec94119bcf2c12aeacb79e52c719a5 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Sun, 12 Dec 2021 16:01:07 +0000 Subject: [PATCH] Pass context to FetchLogs The context will take care of the HTTP request timeout. --- api/graph/resolver.go | 13 ++++--------- api/graph/schema.resolvers.go | 4 ++-- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/api/graph/resolver.go b/api/graph/resolver.go index 254eb77..53ef75d 100644 --- a/api/graph/resolver.go +++ b/api/graph/resolver.go @@ -9,27 +9,22 @@ import ( "io" "io/ioutil" "net/http" - "time" "git.sr.ht/~sircmpwn/builds.sr.ht/api/graph/model" ) type Resolver struct{} -func FetchLogs(url string) (*model.Log, error) { - // TODO: Add context +func FetchLogs(ctx context.Context, url string) (*model.Log, error) { // TODO: It might be possible/desirable to set up an API with the runners // we can use to fetch logs in bulk, perhaps gzipped, and set up a loader // for it. - client := &http.Client{ - Timeout: 2 * time.Second, - } - req, err := http.NewRequest("GET", url, nil) + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) if err != nil { return nil, err } req.Header.Add("Range", "bytes=-131072") // Last 128 KiB - resp, err := client.Do(req) + resp, err := http.DefaultClient.Do(req) if err != nil { return nil, err } @@ -49,7 +44,7 @@ func FetchLogs(url string) (*model.Log, error) { if err != nil { return nil, err } - return &model.Log { + return &model.Log{ Last128KiB: string(log), FullURL: url, }, nil diff --git a/api/graph/schema.resolvers.go b/api/graph/schema.resolvers.go index c3d9f60..3d27a1f 100644 --- a/api/graph/schema.resolvers.go +++ b/api/graph/schema.resolvers.go @@ -107,7 +107,7 @@ func (r *jobResolver) Log(ctx context.Context, obj *model.Job) (*model.Log, erro return nil, nil } url := fmt.Sprintf("http://%s/logs/%d/log", *obj.Runner, obj.ID) - return FetchLogs(url) + return FetchLogs(ctx, url) } func (r *jobResolver) Secrets(ctx context.Context, obj *model.Job) ([]model.Secret, error) { @@ -669,7 +669,7 @@ func (r *taskResolver) Log(ctx context.Context, obj *model.Task) (*model.Log, er return nil, nil } url := fmt.Sprintf("http://%s/logs/%d/%s/log", *obj.Runner, obj.JobID, obj.Name) - return FetchLogs(url) + return FetchLogs(ctx, url) } func (r *taskResolver) Job(ctx context.Context, obj *model.Task) (*model.Job, error) { -- 2.38.5