-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
service.go
176 lines (135 loc) · 3.94 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (c) 2021 - 2024, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package scheduler
import (
"sync"
"time"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/internal/logger"
"github.com/autobrr/autobrr/internal/notification"
"github.com/autobrr/autobrr/internal/update"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/robfig/cron/v3"
"github.com/rs/zerolog"
)
type Service interface {
Start()
Stop()
ScheduleJob(job cron.Job, interval time.Duration, identifier string) (int, error)
AddJob(job cron.Job, spec string, 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.ScheduleJob(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
}
// ScheduleJob takes a time duration and adds a job
func (s *service) ScheduleJob(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.ScheduleJob: 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
}
// AddJob takes a cron schedule and adds a job
func (s *service) AddJob(job cron.Job, spec string, identifier string) (int, error) {
id, err := s.cron.AddJob(spec, cron.NewChain(cron.SkipIfStillRunning(cron.DiscardLogger)).Then(job))
if err != nil {
return 0, errors.Wrap(err, "could not add job to cron")
}
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()
}