From 0c0775214c42ff077f70b3a3c7ef00bfb0298960 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Thu, 18 Jan 2024 07:47:36 +0100 Subject: [PATCH] clear queue --- cmd/api-service/handler/job.go | 13 +++++++++++++ cmd/api-service/job/queue.go | 10 +++++++++- cmd/api-service/job/worker.go | 4 ++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/cmd/api-service/handler/job.go b/cmd/api-service/handler/job.go index 12d2e52..47f510c 100644 --- a/cmd/api-service/handler/job.go +++ b/cmd/api-service/handler/job.go @@ -31,6 +31,8 @@ func (jobAPI *JobAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) { jobAPI.List(w, r) case r.Method == http.MethodDelete && subPath != "": jobAPI.Delete(w, r, subPath) + case r.Method == http.MethodDelete && subPath == "": + jobAPI.DeleteAll(w, r) default: Error(w, http.StatusNotFound, "unregistered path", nil, logger) } @@ -81,3 +83,14 @@ func (jobAPI *JobAPI) Delete(w http.ResponseWriter, r *http.Request, id string) w.WriteHeader(http.StatusNoContent) } + +func (jobAPI *JobAPI) DeleteAll(w http.ResponseWriter, r *http.Request) { + logger := jobAPI.logger.With("method", "deleteall") + + if err := jobAPI.jq.DeleteAll(); err != nil { + Error(w, http.StatusInternalServerError, "could not delete all jobs", err, logger) + return + } + + w.WriteHeader(http.StatusNoContent) +} diff --git a/cmd/api-service/job/queue.go b/cmd/api-service/job/queue.go index 0aea3ce..793b3d9 100644 --- a/cmd/api-service/job/queue.go +++ b/cmd/api-service/job/queue.go @@ -77,7 +77,7 @@ WHERE id=?`, job.ID); err != nil { } } -func (jq *JobQueue) MarkDone(id string) { +func (jq *JobQueue) MarkDone(id int) { logger := jq.logger.With("method", "markdone") if _, err := jq.db.Exec(` UPDATE job_queue SET @@ -117,3 +117,11 @@ WHERE id=?`, id); err != nil { } return nil } + +func (jq *JobQueue) DeleteAll() error { + if _, err := jq.db.Exec(` +DELETE FROM job_queue`); err != nil { + return err + } + return nil +} diff --git a/cmd/api-service/job/worker.go b/cmd/api-service/job/worker.go index 2c467f7..8f7c7a9 100644 --- a/cmd/api-service/job/worker.go +++ b/cmd/api-service/job/worker.go @@ -55,6 +55,9 @@ func (w *Worker) RefreshAllReviews(jobID int) { return } } + + logger.Info("refresh all reviews", "count", len(movies)) + w.jq.MarkDone(jobID) } func (w *Worker) RefreshReviews(jobID int, movieID string) { @@ -85,4 +88,5 @@ func (w *Worker) RefreshReviews(jobID int, movieID string) { } logger.Info("refresh reviews", "count", len(reviews)) + w.jq.MarkDone(jobID) }