blob: 5e0c844f45cdbc8f98a369b418264d4ce9254d98 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
package worker
import (
"context"
"time"
"git.sr.ht/~gabrielgio/midr/db"
"git.sr.ht/~gabrielgio/midr/yt"
work "git.sr.ht/~sircmpwn/dowork"
)
const (
statusStoped = "STOPPED"
statusStarted = "STARTED"
commandStart = "START"
commandStop = "STOP"
)
type command struct {
action string
index uint
}
type Worker struct {
jobs map[uint]string
c chan command
}
func (w *Worker) SpawnWorker(index uint, link string, output string) {
if v, found := w.jobs[index]; found && v == statusStarted {
return
}
w.c <- command{action: commandStart, index: index}
task := work.NewTask(func(ctx context.Context) error {
yt.RunYtDlpProcess(link, output)
return nil
}).After(func(ctx context.Context, task *work.Task) {
w.c <- command{action: commandStop, index: index}
})
work.Enqueue(task)
}
func (w *Worker) startReader() {
for true {
command := <-w.c
if command.action == commandStop {
w.jobs[command.index] = statusStoped
} else if command.action == commandStart {
w.jobs[command.index] = statusStarted
} else {
panic(1)
}
}
}
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)
go w.startReader()
go w.startScheduler(model)
}
|