midr @ db7e822dda56d32135eca6d3e3211a50cf93d31a

feat: Add one more state

Add a new state to the worker so it can better report what is happening.
Also added a status report in the index page.
diff --git a/controller/controller.go b/controller/controller.go
index 3e029f05d736f699c76a95672b6575d4f64fda12..7cbee6a499fa7308f5251df609fdcce179979afd 100644
--- a/controller/controller.go
+++ b/controller/controller.go
@@ -49,3 +49,8 @@ 	id := c.Param("id")
 	e.Entries.Delete(id)
 	c.HTML(http.StatusOK, "entry", entry)
 }
+
+func (e *Env) GetJobs(c *gin.Context) {
+	jobs := e.Worker.GetJobs()
+	c.JSON(http.StatusOK, jobs)
+}
diff --git a/routes/routes.go b/routes/routes.go
index a609019a55130dcc4698cc758c37e2e1df9766a2..79264c5feebe65217e721bde51938d3c3f9942a1 100644
--- a/routes/routes.go
+++ b/routes/routes.go
@@ -29,5 +29,6 @@ 	r.POST("entries/", env.CreateEntry)
 	r.GET("entries/:id", env.GetEntry)
 	r.POST("entries/:id", env.UpdateEntry)
 	r.DELETE("entries/:id", env.DeleteEntry)
+	r.GET("jobs/", env.GetJobs)
 	r.Run(":8000")
 }
diff --git a/templates/_footer.tmpl b/templates/_footer.tmpl
index 07bc857b8d34b7ddeec05bcccfad2d690eacf5e9..97a5bf86321cb9dfb79e36f15c192f3bb67392ba 100644
--- a/templates/_footer.tmpl
+++ b/templates/_footer.tmpl
@@ -8,6 +8,20 @@         .then((res)=>{
             window.location.href = '/'
         });
     }
+
+  function getStatus() {
+    fetch("/jobs/", { method: 'GET'})
+        .then((res) => { return objs = res.json(); })
+        .then((objs) => {
+            for (i in objs) {
+                 span = document.getElementById("status_"+objs[i].Id);
+                 span.textContent = objs[i].Status;
+            }
+          });
+    }
+
+setInterval(getStatus, 1000);
+
 </script>
   </body>
 </html>
diff --git a/templates/_head.tmpl b/templates/_head.tmpl
index 2bb58c474a8361c1c872edd941179d79702174f0..b99510f9b7ebbd4035848c4735481ebd55ee3a5c 100644
--- a/templates/_head.tmpl
+++ b/templates/_head.tmpl
@@ -2,6 +2,7 @@ {{ define "_head" }}
 <!DOCTYPE html>
 <html>
   <head>
+    <meta name="viewport" content="width=device-width, initial-scale=1">
     <link rel="stylesheet" href="/assets/bulma.min.css">
   </head>
   <body>
diff --git a/templates/index.tmpl b/templates/index.tmpl
index 9bb7512f268db6190caae39503ad6aeac64e2caa..ae7f6747e8f506135ea1732727f54b8ab1886e4c 100644
--- a/templates/index.tmpl
+++ b/templates/index.tmpl
@@ -8,6 +8,7 @@       <th scope="col">ID</th>
       <th scope="col">Title</th>
       <th scope="col">Link</th>
       <th scope="col">Output</th>
+      <th scope="col">Status</th>
       <th scope="col"></th>
     </tr>
   </thead>
@@ -18,6 +19,9 @@       <td>{{ .ID }}</td>
       <td>{{ .Title }}</td>
       <td>{{ .Link }}</td>
       <td>{{ .OutputFolder }}</td>
+      <td>
+          <span id="status_{{ .ID }}" class="tag is-primary is-light"></span>
+      </td>
       <td>
         <a href="entries/{{ .ID }}" >Edit</a>
         </span>
diff --git a/worker/worker.go b/worker/worker.go
index 5e0c844f45cdbc8f98a369b418264d4ce9254d98..f56716f0b7426fcba4407da26eb8150f8dbd1986 100644
--- a/worker/worker.go
+++ b/worker/worker.go
@@ -10,11 +10,13 @@ 	work "git.sr.ht/~sircmpwn/dowork"
 )
 
 const (
-	statusStoped  = "STOPPED"
-	statusStarted = "STARTED"
+	statusNotQueued = "NOTQUEUED"
+	statusQueued    = "QUEUED"
+	statusStarted   = "RUNNING"
 
-	commandStart = "START"
-	commandStop  = "STOP"
+	commandStart   = "START"
+	commandEnqueue = "ENQUEUE"
+	commandDequeue = "DEQUEUE"
 )
 
 type command struct {
@@ -27,18 +29,29 @@ 	jobs map[uint]string
 	c    chan command
 }
 
+type Job struct {
+	Id     uint
+	Status string
+}
+
+func (w *Worker) CanEnqueue(index uint) bool {
+	v, found := w.jobs[index]
+	return !found || v == statusNotQueued
+}
+
 func (w *Worker) SpawnWorker(index uint, link string, output string) {
 
-	if v, found := w.jobs[index]; found && v == statusStarted {
+	if !w.CanEnqueue(index) {
 		return
 	}
 
-	w.c <- command{action: commandStart, index: index}
+	w.c <- command{action: commandEnqueue, index: index}
 	task := work.NewTask(func(ctx context.Context) error {
+		w.c <- command{action: commandStart, index: index}
 		yt.RunYtDlpProcess(link, output)
 		return nil
 	}).After(func(ctx context.Context, task *work.Task) {
-		w.c <- command{action: commandStop, index: index}
+		w.c <- command{action: commandDequeue, index: index}
 	})
 
 	work.Enqueue(task)
@@ -48,10 +61,12 @@ func (w *Worker) startReader() {
 	for true {
 		command := <-w.c
 
-		if command.action == commandStop {
-			w.jobs[command.index] = statusStoped
+		if command.action == commandEnqueue {
+			w.jobs[command.index] = statusQueued
 		} else if command.action == commandStart {
 			w.jobs[command.index] = statusStarted
+		} else if command.action == commandDequeue {
+			w.jobs[command.index] = statusNotQueued
 		} else {
 			panic(1)
 		}
@@ -74,3 +89,13 @@ 	w.jobs = make(map[uint]string)
 	go w.startReader()
 	go w.startScheduler(model)
 }
+
+func (w *Worker) GetJobs() []Job {
+	jobs := []Job{}
+
+	for k, v := range w.jobs {
+		jobs = append(jobs, Job{Id: k, Status: v})
+	}
+
+	return jobs
+}