-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.go
225 lines (198 loc) · 5.89 KB
/
scheduler.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package shutd
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
"github.com/sirupsen/logrus"
)
const (
shutdownTag = "shutdown"
snoozeNotificationTag = "snoozeNotification"
)
// Config for shutdown scheduler
type Config struct {
SnoozeInterval int
StartTime string
Notification struct {
Before int
Duration int
}
}
// Scheduler for auto shutdown the computer
type Scheduler struct {
scheduler *gocron.Scheduler
logger *logrus.Logger
config Config
shutdownJob *gocron.Job
snoozeNotificationJob *gocron.Job
shutdownTimeChangedChan chan time.Time
shutdownTask SchedulerTask
snoozeNotificationTask SchedulerTask
}
// SchedulerTask for scheduler to shutdown or notify for snooze
type SchedulerTask func(s *Scheduler) error
type option func(*Scheduler)
// WithLogger option to allow passing of custom logger
func WithLogger(logger *logrus.Logger) option {
return func(s *Scheduler) {
s.logger = logger
}
}
// WithShutdownTask option to allow passing of custom shutdown task
func WithShutdownTask(t SchedulerTask) option {
return func(s *Scheduler) {
s.shutdownTask = t
}
}
// WithSnoozeNotificationTask option to allow passing of custom snooze notification task, Scheduler.Snooze function can snooze it
func WithSnoozeNotificationTask(t SchedulerTask) option {
return func(s *Scheduler) {
s.snoozeNotificationTask = t
}
}
// NewScheduler to create scheduler to shutdown the computer
func NewScheduler(config Config, options ...option) (*Scheduler, error) {
s := gocron.NewScheduler(time.Local)
s.StartAsync()
s.TagsUnique()
scheduler := &Scheduler{
scheduler: s,
logger: logrus.New(),
shutdownTimeChangedChan: make(chan time.Time, 1),
shutdownTask: newShutdownTask(),
snoozeNotificationTask: newNotificationSnoozeTask(),
}
for _, o := range options {
o(scheduler)
}
err := scheduler.Configure(config)
if err != nil {
return nil, err
}
return scheduler, nil
}
// Configure scheduler for updated config
func (s *Scheduler) Configure(config Config) error {
s.config = config
s.logger.Infof("config: %+v", config)
var err error
err = s.scheduleShutdownJob(s.config.StartTime)
if err != nil {
return err
}
err = s.scheduleSnoozeNotificationJob()
if err != nil {
return err
}
s.printJobs()
return nil
}
// Config get config of the scheduler
func (s *Scheduler) Config() Config {
return s.config
}
// Logger get logger of the scheduler
func (s *Scheduler) Logger() *logrus.Logger {
return s.logger
}
// ShutdownTimeChangedChan get channel of latest shutdown time
func (s *Scheduler) ShutdownTimeChangedChan() chan time.Time {
return s.shutdownTimeChangedChan
}
// ShutdownTime get next shutdown time
func (s *Scheduler) ShutdownTime() (time.Time, error) {
if s.shutdownJob == nil {
return time.Time{}, fmt.Errorf("shutdown job is not scheduled")
}
return s.shutdownJob.ScheduledTime(), nil
}
// Snooze to delay shutdown time for the computer
func (s *Scheduler) Snooze() error {
if s.shutdownJob == nil {
return fmt.Errorf("shutdown job is not scheduled")
}
var err error
delayedTime := s.shutdownJob.ScheduledTime().Add(time.Duration(s.config.SnoozeInterval) * time.Minute)
err = s.scheduleShutdownJob(delayedTime)
if err != nil {
return err
}
err = s.scheduleSnoozeNotificationJob()
if err != nil {
return err
}
s.printJobs()
return nil
}
func (s *Scheduler) scheduleShutdownJob(shutdownTime interface{}) error {
if s.shutdownJob == nil {
j, err := s.scheduler.Every(1).Day().At(shutdownTime).Tag(shutdownTag).Do(func() {
s.Logger().Info("==========================")
s.Logger().Info("Shutdown")
s.Logger().Info("==========================")
err := s.shutdownTask(s)
if err != nil {
s.logger.Errorf("failed to execute shutdown task: %v", err)
}
})
if err != nil {
// not wrapping error to expose implementation details
return fmt.Errorf("failed to schedule shutdown job: %v", err)
}
s.shutdownJob = j
} else {
_, err := s.scheduler.Job(s.shutdownJob).At(shutdownTime).Update()
if err != nil {
// not wrapping error to expose implementation details
return fmt.Errorf("failed to update scheduled shutdown job: %v", err)
}
}
select {
case s.shutdownTimeChangedChan <- s.shutdownJob.ScheduledTime():
default:
// in case no one is waiting for the channel
}
return nil
}
func (s *Scheduler) scheduleSnoozeNotificationJob() error {
if s.shutdownJob == nil {
return fmt.Errorf("shutdown job is not scheduled")
}
notifyTime := s.shutdownJob.ScheduledTime().Add(-time.Duration(s.config.Notification.Before) * time.Minute)
if s.snoozeNotificationJob == nil {
s.scheduler.Every(1).Day().StartAt(notifyTime).Tag(snoozeNotificationTag)
} else {
s.scheduler.Job(s.snoozeNotificationJob).StartAt(notifyTime)
}
if time.Now().After(notifyTime) {
s.scheduler.StartImmediately()
}
if s.snoozeNotificationJob == nil {
j, err := s.scheduler.Do(func() {
s.Logger().Info("==========================")
s.Logger().Info("Snooze notification")
s.Logger().Info("==========================")
err := s.snoozeNotificationTask(s)
if err != nil {
s.logger.Errorf("failed to execute snooze notification task: %v", err)
}
})
if err != nil {
// not wrapping error to expose implementation details
return fmt.Errorf("failed to schedule snooze notification job: %v", err)
}
s.snoozeNotificationJob = j
} else {
_, err := s.scheduler.Update()
if err != nil {
// not wrapping error to expose implementation details
return fmt.Errorf("failed to update scheduled snooze notification job: %v", err)
}
}
return nil
}
func (s *Scheduler) printJobs() {
for _, j := range s.scheduler.Jobs() {
s.logger.Infof("job: %v, scheduled: %v (%v), nextRun: %v (%v), count: %v", j.Tags(), j.ScheduledTime().Format("15:04"), j.ScheduledTime(), j.NextRun().Format("15:04"), j.NextRun(), j.RunCount())
}
}