2023-12-30 09:19:53 +01:00
|
|
|
package job
|
2023-12-29 19:10:31 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"log/slog"
|
|
|
|
"time"
|
|
|
|
|
2023-12-30 09:19:53 +01:00
|
|
|
"ewintr.nl/emdb/cmd/api-service/moviestore"
|
2023-12-29 19:10:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type JobQueue struct {
|
2023-12-30 09:19:53 +01:00
|
|
|
db *moviestore.SQLite
|
2023-12-29 19:10:31 +01:00
|
|
|
out chan Job
|
|
|
|
logger *slog.Logger
|
|
|
|
}
|
|
|
|
|
2023-12-30 09:19:53 +01:00
|
|
|
func NewJobQueue(db *moviestore.SQLite, logger *slog.Logger) *JobQueue {
|
2023-12-29 19:10:31 +01:00
|
|
|
return &JobQueue{
|
|
|
|
db: db,
|
|
|
|
out: make(chan Job),
|
|
|
|
logger: logger.With("service", "jobqueue"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jq *JobQueue) Add(movieID string, action Action) error {
|
2024-01-18 08:57:56 +01:00
|
|
|
if !Valid(action) {
|
|
|
|
return errors.New("invalid action")
|
2024-01-18 07:38:32 +01:00
|
|
|
}
|
|
|
|
|
2023-12-29 19:10:31 +01:00
|
|
|
_, err := jq.db.Exec(`INSERT INTO job_queue (movie_id, action, status)
|
|
|
|
VALUES (?, ?, 'todo')`, movieID, action)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jq *JobQueue) Next() chan Job {
|
|
|
|
return jq.out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jq *JobQueue) Run() {
|
|
|
|
logger := jq.logger.With("method", "run")
|
|
|
|
logger.Info("starting job queue")
|
|
|
|
for {
|
2024-01-18 07:56:25 +01:00
|
|
|
time.Sleep(interval)
|
2023-12-29 19:10:31 +01:00
|
|
|
row := jq.db.QueryRow(`
|
|
|
|
SELECT id, movie_id, action
|
|
|
|
FROM job_queue
|
|
|
|
WHERE status='todo'
|
2024-01-18 08:57:56 +01:00
|
|
|
ORDER BY id ASC
|
2023-12-29 19:10:31 +01:00
|
|
|
LIMIT 1`)
|
|
|
|
|
|
|
|
var job Job
|
|
|
|
err := row.Scan(&job.ID, &job.MovieID, &job.Action)
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, sql.ErrNoRows):
|
|
|
|
logger.Info("nothing to do")
|
|
|
|
continue
|
|
|
|
case err != nil:
|
|
|
|
logger.Error("could not fetch next job", "error", row.Err())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logger.Info("found a job", "id", job.ID)
|
|
|
|
|
|
|
|
if _, err := jq.db.Exec(`
|
|
|
|
UPDATE job_queue
|
|
|
|
SET status='doing'
|
|
|
|
WHERE id=?`, job.ID); err != nil {
|
|
|
|
logger.Error("could not set job to doing", "error")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
jq.out <- job
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 07:47:36 +01:00
|
|
|
func (jq *JobQueue) MarkDone(id int) {
|
2023-12-29 19:10:31 +01:00
|
|
|
logger := jq.logger.With("method", "markdone")
|
|
|
|
if _, err := jq.db.Exec(`
|
2024-01-18 07:56:25 +01:00
|
|
|
DELETE FROM job_queue
|
2023-12-29 19:10:31 +01:00
|
|
|
WHERE id=?`, id); err != nil {
|
|
|
|
logger.Error("could not mark job done", "error", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2024-01-18 07:38:32 +01:00
|
|
|
|
|
|
|
func (jq *JobQueue) List() ([]Job, error) {
|
|
|
|
rows, err := jq.db.Query(`
|
|
|
|
SELECT id, movie_id, action, status, created_at, updated_at
|
|
|
|
FROM job_queue
|
|
|
|
ORDER BY id DESC`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var jobs []Job
|
|
|
|
for rows.Next() {
|
|
|
|
var j Job
|
|
|
|
if err := rows.Scan(&j.ID, &j.MovieID, &j.Action, &j.Status, &j.Created, &j.Updated); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
jobs = append(jobs, j)
|
|
|
|
}
|
|
|
|
return jobs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jq *JobQueue) Delete(id string) error {
|
|
|
|
if _, err := jq.db.Exec(`
|
|
|
|
DELETE FROM job_queue
|
|
|
|
WHERE id=?`, id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-18 07:47:36 +01:00
|
|
|
|
|
|
|
func (jq *JobQueue) DeleteAll() error {
|
|
|
|
if _, err := jq.db.Exec(`
|
|
|
|
DELETE FROM job_queue`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|