From da992500f806bb87b06559d920ee12b7680955ee Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Thu, 16 Jun 2022 16:32:31 +0200 Subject: feat: Add worker Add a simple worker to manage a work queue. Right now, it is bit brittled and has no test coverage yet, but it works. Also moved from pico.css to bulma, I like the idea of classes approach of pico but for me bulma yields a better result. --- controller/controller.go | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'controller') diff --git a/controller/controller.go b/controller/controller.go index 252be73..3e029f0 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -1,24 +1,16 @@ package controller import ( - "context" "net/http" "git.sr.ht/~gabrielgio/midr/db" - "git.sr.ht/~gabrielgio/midr/yt" - work "git.sr.ht/~sircmpwn/dowork" + "git.sr.ht/~gabrielgio/midr/worker" "github.com/gin-gonic/gin" ) type Env struct { Entries db.EntryModel -} - -func spawnWorker(link string, output string) { - work.Submit(func(ctx context.Context) error { - yt.RunYtDlpProcess(link, output) - return nil - }) + Worker worker.Worker } func (e Env) GetEntries(c *gin.Context) { @@ -28,8 +20,12 @@ func (e Env) GetEntries(c *gin.Context) { func (e *Env) GetEntry(c *gin.Context) { id := c.Param("id") - entry := e.Entries.Find(id) - c.HTML(http.StatusOK, "entry", entry) + if id != "" { + entry := e.Entries.Find(id) + c.HTML(http.StatusOK, "entry", entry) + } else { + c.HTML(http.StatusOK, "entry", db.Entry{}) + } } func (e *Env) UpdateEntry(c *gin.Context) { @@ -43,7 +39,7 @@ func (e *Env) CreateEntry(c *gin.Context) { var entry db.Entry c.ShouldBind(&entry) e.Entries.Create(entry) - spawnWorker(entry.Link, entry.OutputFolder) + e.Worker.SpawnWorker(entry.ID, entry.Link, entry.OutputFolder) c.Redirect(http.StatusFound, "/") } -- cgit v1.2.3