-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicroservice_scheduler.go
58 lines (51 loc) · 1.41 KB
/
microservice_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
// Create and maintain by Chaiyapong Lapliengtrakul (chaiyapong@3dsinteractive.com), All right reserved (2021 - Present)
package main
import (
"sync"
"time"
)
// Schedule will run handler at timer period
func (ms *Microservice) Schedule(timer time.Duration, h ServiceHandleFunc) chan bool /*exit channel*/ {
// exitChan must be call exitChan <- true from caller to exit scheduler
exitChan := make(chan bool, 1)
go func() {
t := time.NewTicker(timer)
done := make(chan bool, 1)
isExit := false
isExitMutex := sync.Mutex{}
go func() {
<-exitChan
isExitMutex.Lock()
isExit = true
isExitMutex.Unlock()
// Stop Tick() and send done message to exit for loop below
// Ref: From the documentation http://golang.org/pkg/time/#Ticker.Stop
// Stop turns off a ticker. After Stop, no more ticks will be sent.
// Stop does not close the channel, to prevent a read from the channel succeeding incorrectly.
t.Stop()
done <- true
}()
for {
select {
case execTime := <-t.C:
isExitMutex.Lock()
if isExit {
isExitMutex.Unlock()
// Done in the next round
continue
}
isExitMutex.Unlock()
now := time.Now()
// The schedule that older than 10s, will be skip, because t.C is buffer at size 1
diff := now.Sub(execTime).Seconds()
if diff > 10 {
continue
}
h(NewSchedulerContext(ms))
case <-done:
return
}
}
}()
return exitChan
}