Skip to content
Snippets Groups Projects
Commit af5cf073 authored by Christian Pointner's avatar Christian Pointner
Browse files

job.Start() now accepts a parent context

parent f25a011f
No related branches found
No related tags found
No related merge requests found
......@@ -29,6 +29,7 @@ import (
"net/http"
"net/url"
"time"
"context"
"github.com/gorilla/mux"
"gitlab.servus.at/autoradio/tank/importer"
......@@ -83,7 +84,7 @@ func (api *API) CreateFileForGroup() http.Handler {
sendError(w, err)
return
}
if err = job.Start(3 * time.Hour); err != nil { // TODO: hardcoded value
if err = job.Start(context.Background(), 3 * time.Hour); err != nil { // TODO: hardcoded value
// shall we remove the file here... thinking...
sendError(w, err)
return
......@@ -123,6 +124,7 @@ func (api *API) PatchFileOfGroup() http.Handler {
return
}
// TODO: warn if data contains invalid/unknown keys
// TODO: only allow this for successfully imported files...!?
file, err := api.store.UpdateFileMetadata(vars["group-id"], id, data)
if err != nil {
sendError(w, err)
......
......@@ -84,7 +84,7 @@ func (job *Job) run() (err error) {
return
}
func (job *Job) Start(timeout time.Duration) error {
func (job *Job) Start(ctx context.Context, timeout time.Duration) error {
if !atomic.CompareAndSwapUint32((*uint32)(&job.State), uint32(JobNew), uint32(JobInitializing)) {
if atomic.LoadUint32((*uint32)(&job.State)) == uint32(JobCanceled) {
// the job has already been canceled and thus should just be removed from the inventory
......@@ -96,9 +96,9 @@ func (job *Job) Start(timeout time.Duration) error {
// shall we use a global context here so we can cancel all jobs at once? i.e. for forced shutdown...
if timeout > 0 {
job.ctx, job.cancel = context.WithTimeout(context.Background(), timeout)
job.ctx, job.cancel = context.WithTimeout(ctx, timeout)
} else {
job.ctx, job.cancel = context.WithCancel(context.Background())
job.ctx, job.cancel = context.WithCancel(ctx)
}
// from here on we need to take care that job.cancel() will be called in any case so that resources
// allocated by job.ctx are freed up.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment