-
Notifications
You must be signed in to change notification settings - Fork 1
/
job.go
87 lines (73 loc) · 1.93 KB
/
job.go
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
77
78
79
80
81
82
83
84
85
86
87
package archivar
import (
"context"
"fmt"
"os"
"os/signal"
"sync"
"time"
"github.com/rwese/archivar/archivar/archiver/archivers"
"github.com/rwese/archivar/archivar/filter/filters"
"github.com/rwese/archivar/archivar/job"
"github.com/rwese/archivar/archivar/processor/processors"
)
func (s *Archivar) runJob(job job.Job, ctx context.Context, stop context.CancelFunc, wg *sync.WaitGroup) {
defer wg.Done()
s.logger.Infof("Starting Job %s, every %d seconds", job.Name, job.Interval)
waitingTime := time.Duration(job.Interval) * time.Second
schedule := time.After(time.Second * 0)
if err := job.Connect(); err != nil {
s.logger.Warnf("%s: error %s", job.Name, err.Error())
stop()
}
for {
select {
case <-ctx.Done():
s.logger.Debugf("%s: Gracefully exit", job.Name)
return
case <-schedule:
s.logger.Debugf("%s: Run job", job.Name)
err := job.Download()
if err != nil {
s.logger.Warnf("%s: error %s", job.Name, err.Error())
stop()
}
}
if waitingTime <= 0 {
s.logger.Debugf("%s: Stopping after single run", job.Name)
break
}
time.Sleep(time.Second * 1)
// time.After allows the process to be stopped instantly which sleep doesn't
schedule = time.After(waitingTime)
}
s.logger.Debugf("%s: ended", job.Name)
}
func (s *Archivar) RunJobs() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
wg := new(sync.WaitGroup)
wg.Add(len(s.jobs))
for _, job := range s.jobs {
go s.runJob(job, ctx, stop, wg)
}
wg.Wait()
}
func (s *Archivar) Dump() {
fmt.Println("Archivers:")
for _, a := range archivers.ListArchivers() {
fmt.Printf("\t%s\n", a)
}
fmt.Println("Gatherers:")
for _, a := range archivers.ListGatherers() {
fmt.Printf("\t%s\n", a)
}
fmt.Println("Filters:")
for _, a := range filters.ListFilters() {
fmt.Printf("\t%s\n", a)
}
fmt.Println("Processors:")
for _, a := range processors.ListProcessors() {
fmt.Printf("\t%s\n", a)
}
}