-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
service.go
153 lines (119 loc) · 3.17 KB
/
service.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package scheduler
import (
"github.com/SyncYomi/SyncYomi/internal/domain"
"github.com/SyncYomi/SyncYomi/internal/logger"
"github.com/SyncYomi/SyncYomi/internal/notification"
"github.com/SyncYomi/SyncYomi/internal/update"
"github.com/robfig/cron/v3"
"github.com/rs/zerolog"
"sync"
"time"
)
type Service interface {
Start()
Stop()
AddJob(job cron.Job, interval time.Duration, identifier string) (int, error)
RemoveJobByIdentifier(id string) error
GetNextRun(id string) (time.Time, error)
}
type service struct {
log zerolog.Logger
config *domain.Config
version string
notificationSvc notification.Service
updateSvc *update.Service
cron *cron.Cron
jobs map[string]cron.EntryID
m sync.RWMutex
}
func NewService(log logger.Logger, config *domain.Config, notificationSvc notification.Service, updateSvc *update.Service) Service {
return &service{
log: log.With().Str("module", "scheduler").Logger(),
config: config,
notificationSvc: notificationSvc,
updateSvc: updateSvc,
cron: cron.New(cron.WithChain(
cron.Recover(cron.DefaultLogger),
)),
jobs: map[string]cron.EntryID{},
}
}
func (s *service) Start() {
s.log.Debug().Msg("scheduler.Start")
// start scheduler
s.cron.Start()
// init jobs
go s.addAppJobs()
return
}
func (s *service) addAppJobs() {
time.Sleep(5 * time.Second)
if s.config.CheckForUpdates {
checkUpdates := &CheckUpdatesJob{
Name: "app-check-updates",
Log: s.log.With().Str("job", "app-check-updates").Logger(),
Version: s.version,
NotifSvc: s.notificationSvc,
updateService: s.updateSvc,
lastCheckVersion: s.version,
}
if id, err := s.AddJob(checkUpdates, 2*time.Hour, "app-check-updates"); err != nil {
s.log.Error().Err(err).Msgf("scheduler.addAppJobs: error adding job: %v", id)
}
}
}
func (s *service) Stop() {
s.log.Debug().Msg("scheduler.Stop")
s.cron.Stop()
return
}
func (s *service) AddJob(job cron.Job, interval time.Duration, identifier string) (int, error) {
id := s.cron.Schedule(cron.Every(interval), cron.NewChain(
cron.SkipIfStillRunning(cron.DiscardLogger)).Then(job),
)
s.log.Debug().Msgf("scheduler.AddJob: job successfully added: %s id %d", identifier, id)
s.m.Lock()
// add to job map
s.jobs[identifier] = id
s.m.Unlock()
return int(id), nil
}
func (s *service) RemoveJobByIdentifier(id string) error {
s.m.Lock()
defer s.m.Unlock()
v, ok := s.jobs[id]
if !ok {
return nil
}
s.log.Debug().Msgf("scheduler.Remove: removing job: %v", id)
// remove from cron
s.cron.Remove(v)
// remove from jobs map
delete(s.jobs, id)
return nil
}
func (s *service) GetNextRun(id string) (time.Time, error) {
entry := s.getEntryById(id)
if !entry.Valid() {
return time.Time{}, nil
}
s.log.Debug().Msgf("scheduler.GetNextRun: %s next run: %s", id, entry.Next)
return entry.Next, nil
}
func (s *service) getEntryById(id string) cron.Entry {
s.m.Lock()
defer s.m.Unlock()
v, ok := s.jobs[id]
if !ok {
return cron.Entry{}
}
return s.cron.Entry(v)
}
type GenericJob struct {
Name string
Log zerolog.Logger
callback func()
}
func (j *GenericJob) Run() {
j.callback()
}