slow down

This commit is contained in:
Erik Winter 2024-01-18 07:56:25 +01:00
parent 0c0775214c
commit 86b45fa480
2 changed files with 3 additions and 6 deletions

View File

@ -9,7 +9,7 @@ type JobStatus string
type Action string type Action string
const ( const (
interval = 10 * time.Second interval = 20 * time.Second
ActionRefreshIMDBReviews Action = "refresh-imdb-reviews" ActionRefreshIMDBReviews Action = "refresh-imdb-reviews"
ActionRefreshAllIMDBReviews Action = "refresh-all-imdb-reviews" ActionRefreshAllIMDBReviews Action = "refresh-all-imdb-reviews"

View File

@ -43,6 +43,7 @@ func (jq *JobQueue) Run() {
logger := jq.logger.With("method", "run") logger := jq.logger.With("method", "run")
logger.Info("starting job queue") logger.Info("starting job queue")
for { for {
time.Sleep(interval)
row := jq.db.QueryRow(` row := jq.db.QueryRow(`
SELECT id, movie_id, action SELECT id, movie_id, action
FROM job_queue FROM job_queue
@ -55,11 +56,9 @@ LIMIT 1`)
switch { switch {
case errors.Is(err, sql.ErrNoRows): case errors.Is(err, sql.ErrNoRows):
logger.Info("nothing to do") logger.Info("nothing to do")
time.Sleep(interval)
continue continue
case err != nil: case err != nil:
logger.Error("could not fetch next job", "error", row.Err()) logger.Error("could not fetch next job", "error", row.Err())
time.Sleep(interval)
continue continue
} }
logger.Info("found a job", "id", job.ID) logger.Info("found a job", "id", job.ID)
@ -69,7 +68,6 @@ UPDATE job_queue
SET status='doing' SET status='doing'
WHERE id=?`, job.ID); err != nil { WHERE id=?`, job.ID); err != nil {
logger.Error("could not set job to doing", "error") logger.Error("could not set job to doing", "error")
time.Sleep(interval)
continue continue
} }
@ -80,8 +78,7 @@ WHERE id=?`, job.ID); err != nil {
func (jq *JobQueue) MarkDone(id int) { func (jq *JobQueue) MarkDone(id int) {
logger := jq.logger.With("method", "markdone") logger := jq.logger.With("method", "markdone")
if _, err := jq.db.Exec(` if _, err := jq.db.Exec(`
UPDATE job_queue SET DELETE FROM job_queue
status='done'
WHERE id=?`, id); err != nil { WHERE id=?`, id); err != nil {
logger.Error("could not mark job done", "error", err) logger.Error("could not mark job done", "error", err)
} }