From d60451a829a4370a593ff5f6a7af2833e08257a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Wed, 26 Jun 2024 10:48:03 +0200 Subject: [PATCH] Manager: run some worker management API calls in a background context Run the actually-doing-stuff parts of `RequestWorkerStatusChange()` and `SetWorkerTags()` in a background context. That way the operation can continue even when the HTTP client disconnects. --- internal/manager/api_impl/worker_mgt.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/internal/manager/api_impl/worker_mgt.go b/internal/manager/api_impl/worker_mgt.go index f36411f3..88ca016d 100644 --- a/internal/manager/api_impl/worker_mgt.go +++ b/internal/manager/api_impl/worker_mgt.go @@ -179,6 +179,11 @@ func (f *Flamenco) RequestWorkerStatusChange(e echo.Context, workerUUID string) logger.Info().Msg("worker status change requested") + // All information to do the operation is known, so even when the client + // disconnects, the work should be completed. + ctx, ctxCancel := bgContext() + defer ctxCancel() + if dbWorker.Status == change.Status { // Requesting that the worker should go to its current status basically // means cancelling any previous status change request. @@ -188,7 +193,7 @@ func (f *Flamenco) RequestWorkerStatusChange(e echo.Context, workerUUID string) } // Store the status change. - if err := f.persist.SaveWorker(e.Request().Context(), dbWorker); err != nil { + if err := f.persist.SaveWorker(ctx, dbWorker); err != nil { logger.Error().Err(err).Msg("saving worker after status change request") return sendAPIError(e, http.StatusInternalServerError, "error saving worker: %v", err) } @@ -232,6 +237,11 @@ func (f *Flamenco) SetWorkerTags(e echo.Context, workerUUID string) error { Logger() logger.Info().Msg("worker tag change requested") + // All information to do the operation is known, so even when the client + // disconnects, the work should be completed. + ctx, ctxCancel := bgContext() + defer ctxCancel() + // Store the new tag assignment. if err := f.persist.WorkerSetTags(ctx, dbWorker, change.TagIds); err != nil { logger.Error().Err(err).Msg("saving worker after tag change request")