diff --git a/controller/controller.go b/controller/controller.go
index 7fc874847299fe4623b4e2754425bca2b00cd806..e381bf81c9f1386fcbdb5ad9f45629b5bd1890d9 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 @@ Entries db.EntryModel
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 01d9b9f72f6dff2b08fdeac296ee8cd4cb054c3c..0a5ca98a33c029aa1291ab7bced124f13273c95b 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 79264c5feebe65217e721bde51938d3c3f9942a1..31384a710be0fbdfe0d26f286cd199a3315b5495 100644
--- a/routes/routes.go
+++ b/routes/routes.go
@@ -10,14 +10,14 @@
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/*")
diff --git a/worker/worker.go b/worker/worker.go
index 06fac3601335e04a918fb55124be04b218578df3..a8f15180e1093473163b8eb4ccf274786ea6d380 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"
)
@@ -32,6 +30,13 @@
type Job struct {
Id uint
Status string
+}
+
+func NewWorkder() Worker {
+ return Worker{
+ c: make(chan command, 10),
+ jobs: make(map[uint]string),
+ }
}
func (w *Worker) CanEnqueue(index uint) bool {
@@ -73,21 +78,8 @@ }
}
}
-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 {