forked from ZacxDev/email-verifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.go
53 lines (47 loc) · 1.13 KB
/
schedule.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
package emailverifier
import (
"time"
)
// schedule represents a job schedule
type schedule struct {
stopCh chan struct{} // stop channel to control job
jobFunc interface{} // schedule job handle function
jobParams []interface{} // params of function
ticker *time.Ticker // ticker sends the time with a period specified by a duration
running bool // running indicates the current running state of schedule.
}
// newSchedule returns a new schedule instance
func newSchedule(period time.Duration, jobFunc interface{}, params ...interface{}) *schedule {
return &schedule{
stopCh: make(chan struct{}),
jobFunc: jobFunc,
jobParams: params,
ticker: time.NewTicker(period),
}
}
// start triggers the schedule job
func (s *schedule) start() {
if s.running {
return
}
s.running = true
go func() {
for {
select {
case <-s.ticker.C:
callJobFuncWithParams(s.jobFunc, s.jobParams)
case <-s.stopCh:
s.ticker.Stop()
return
}
}
}()
}
// stop will stop previously started schedule job
func (s *schedule) stop() {
if !s.running {
return
}
s.running = false
s.stopCh <- struct{}{}
}