~comcloudway/builds.sr.ht

ccfb55e263ec94119bcf2c12aeacb79e52c719a5 — Simon Ser 2 years ago 690f1aa
Pass context to FetchLogs

The context will take care of the HTTP request timeout.
2 files changed, 6 insertions(+), 11 deletions(-)

M api/graph/resolver.go
M api/graph/schema.resolvers.go
M api/graph/resolver.go => api/graph/resolver.go +4 -9
@@ 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

M api/graph/schema.resolvers.go => api/graph/schema.resolvers.go +2 -2
@@ 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) {