aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2022-06-20 18:35:50 +0200
committerGabriel A. Giovanini <mail@gabrielgio.me>2022-06-20 18:35:50 +0200
commit4e5b2d9dfd9413ce084e64e048a57ad6e23356d3 (patch)
tree674754d58838a219871304fd8fc57b44d2193160
parenta4cd5de795926537318f94aa34c9f2579c29fc11 (diff)
downloadmdir-4e5b2d9dfd9413ce084e64e048a57ad6e23356d3.tar.gz
mdir-4e5b2d9dfd9413ce084e64e048a57ad6e23356d3.tar.bz2
mdir-4e5b2d9dfd9413ce084e64e048a57ad6e23356d3.zip
ref: Remove model dependency fom worker
It was a bit weird to have that dependency in first place.
-rw-r--r--controller/controller.go17
-rw-r--r--db/model.go10
-rw-r--r--routes/routes.go6
-rw-r--r--worker/worker.go24
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 {