From 4e5b2d9dfd9413ce084e64e048a57ad6e23356d3 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Mon, 20 Jun 2022 18:35:50 +0200 Subject: ref: Remove model dependency fom worker It was a bit weird to have that dependency in first place. --- controller/controller.go | 17 ++++++++++++++++- db/model.go | 10 +++++----- routes/routes.go | 6 +++--- worker/worker.go | 24 ++++++++---------------- 4 files changed, 32 insertions(+), 25 deletions(-) diff --git a/controller/controller.go b/controller/controller.go index 7fc8748..e381bf8 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -2,6 +2,7 @@ package controller import ( "net/http" + "time" "git.sr.ht/~gabrielgio/midr/db" "git.sr.ht/~gabrielgio/midr/worker" @@ -13,7 +14,7 @@ type Env struct { Worker worker.Worker } -func (e Env) GetEntries(c *gin.Context) { +func (e *Env) GetEntries(c *gin.Context) { entries := e.Entries.All() c.HTML(http.StatusOK, "index", entries) } @@ -54,3 +55,17 @@ func (e *Env) GetJobs(c *gin.Context) { jobs := e.Worker.GetJobs() c.JSON(http.StatusOK, jobs) } + +func (e *Env) StartScheduler() { + e.Worker.StartReader() + go func() { + for true { + entries := e.Entries.All() + + for _, entry := range entries { + e.Worker.SpawnWorker(entry.ID, entry.Link, entry.OutputFolder) + } + time.Sleep(30 * time.Minute) + } + }() +} diff --git a/db/model.go b/db/model.go index 01d9b9f..0a5ca98 100644 --- a/db/model.go +++ b/db/model.go @@ -16,27 +16,27 @@ type EntryModel struct { DB *gorm.DB } -func (m EntryModel) Find(id string) Entry { +func (m *EntryModel) Find(id string) Entry { var entry Entry where := "id = " + id m.DB.Where(where).FirstOrInit(&entry) return entry } -func (m EntryModel) All() []Entry { +func (m *EntryModel) All() []Entry { var entries []Entry m.DB.Find(&entries) return entries } -func (m EntryModel) Create(entry *Entry) { +func (m *EntryModel) Create(entry *Entry) { m.DB.Create(entry) } -func (m EntryModel) Update(entry Entry) { +func (m *EntryModel) Update(entry Entry) { m.DB.Save(&entry) } -func (m EntryModel) Delete(id string) { +func (m *EntryModel) Delete(id string) { m.DB.Delete(&Entry{}, id) } diff --git a/routes/routes.go b/routes/routes.go index 79264c5..31384a7 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -10,15 +10,15 @@ import ( func HandleRequests() { models := db.EntryModel{DB: db.DB} - worker := worker.Worker{} - - worker.StartWorker(models) + worker := worker.NewWorkder() env := &controller.Env{ Entries: models, Worker: worker, } + env.StartScheduler() + r := gin.Default() r.LoadHTMLGlob("templates/*") r.Static("/assets", "./assets") diff --git a/worker/worker.go b/worker/worker.go index 06fac36..a8f1518 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -2,9 +2,7 @@ package worker import ( "context" - "time" - "git.sr.ht/~gabrielgio/midr/db" "git.sr.ht/~gabrielgio/midr/yt" work "git.sr.ht/~sircmpwn/dowork" ) @@ -34,6 +32,13 @@ type Job struct { Status string } +func NewWorkder() Worker { + return Worker{ + c: make(chan command, 10), + jobs: make(map[uint]string), + } +} + func (w *Worker) CanEnqueue(index uint) bool { v, found := w.jobs[index] return !found || v == statusNotQueued @@ -73,21 +78,8 @@ func (w *Worker) startReader() { } } -func (w *Worker) startScheduler(model db.EntryModel) { - for true { - entries := model.All() - for _, e := range entries { - w.SpawnWorker(e.ID, e.Link, e.OutputFolder) - } - time.Sleep(30 * time.Minute) - } -} - -func (w *Worker) StartWorker(model db.EntryModel) { - w.c = make(chan command, 10) - w.jobs = make(map[uint]string) +func (w *Worker) StartReader() { go w.startReader() - go w.startScheduler(model) } func (w *Worker) GetJobs() []Job { -- cgit v1.2.3