|
1 | 1 | package utils |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
| 4 | + "context" |
5 | 5 | "os" |
6 | 6 | "os/signal" |
7 | 7 | "syscall" |
8 | 8 | "time" |
9 | 9 | ) |
10 | 10 |
|
11 | 11 | type ScheduledExecutor struct { |
12 | | - delay time.Duration |
13 | | - ticker time.Ticker |
14 | | - quit chan int |
| 12 | + delay time.Duration |
| 13 | + ticker time.Ticker |
| 14 | + sigs chan os.Signal |
| 15 | + shutdown bool |
15 | 16 | } |
16 | 17 |
|
17 | | -const SHUT_DOWN = 1 |
18 | | - |
19 | 18 | func NewTimedExecutor(initialDelay time.Duration, delay time.Duration) ScheduledExecutor { |
20 | 19 | return ScheduledExecutor{ |
21 | 20 | delay: delay, |
22 | 21 | ticker: *time.NewTicker(initialDelay), |
23 | | - quit: make(chan int), |
24 | 22 | } |
25 | 23 | } |
26 | 24 |
|
27 | 25 | // Start .. process() is the function to run periodically , runAsync detects if the function should block the executor when running or not. It blocks when false |
28 | | -func (se ScheduledExecutor) Start(task func(), runAsync bool) { |
| 26 | +func (se *ScheduledExecutor) Start(task func(), runAsync bool) { |
| 27 | + // Create a context that can be cancelled |
| 28 | + ctx, cancel := context.WithCancel(context.Background()) |
| 29 | + defer cancel() |
29 | 30 |
|
30 | | - sigs := make(chan os.Signal, 1) |
31 | | - signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) |
| 31 | + se.shutdown = false |
| 32 | + se.sigs = make(chan os.Signal, 1) |
| 33 | + signal.Notify(se.sigs, syscall.SIGINT, syscall.SIGTERM) |
32 | 34 |
|
33 | 35 | go func() { |
34 | | - defer func() { |
35 | | - fmt.Println("Scheduler stopping...") |
36 | | - se.close() |
37 | | - fmt.Println("Scheduler stopped.") |
38 | | - }() |
39 | | - firstExec := true |
40 | | - for { |
41 | | - fmt.Println("IN the loop") |
42 | | - select { |
43 | | - case <-se.ticker.C: |
| 36 | + <-se.sigs // Block until a signal is received |
| 37 | + cancel() |
| 38 | + }() |
44 | 39 |
|
45 | | - if firstExec { |
46 | | - se.ticker.Stop() |
47 | | - se.ticker = *time.NewTicker(se.delay) |
48 | | - firstExec = false |
49 | | - } |
| 40 | + firstExec := true |
50 | 41 |
|
51 | | - if runAsync { |
52 | | - go task() |
53 | | - } else { |
54 | | - task() |
55 | | - } |
56 | | - fmt.Println("case evaluated, other cases will be ignored for now - 1") |
57 | | - case a := <-se.quit: |
58 | | - if a == SHUT_DOWN { |
59 | | - fmt.Printf("returning here - 2, a= %d\n", a) |
60 | | - return |
61 | | - } |
62 | | - fmt.Println("keep idling sweet golang - 2") |
| 42 | + defer func() { |
| 43 | + se.close() |
| 44 | + close(se.sigs) |
| 45 | + }() |
| 46 | + for { |
| 47 | + if se.shutdown { |
| 48 | + return |
| 49 | + } |
| 50 | + select { |
| 51 | + case <-se.ticker.C: |
63 | 52 |
|
64 | | - case <-sigs: |
65 | | - fmt.Println("AWW AWW AWW - 3") |
66 | | - fmt.Println("breaking out of select here - 3") |
| 53 | + if firstExec { |
| 54 | + se.ticker.Stop() |
| 55 | + se.ticker = *time.NewTicker(se.delay) |
| 56 | + firstExec = false |
| 57 | + } |
| 58 | + |
| 59 | + if runAsync { |
| 60 | + go task() |
| 61 | + } else { |
| 62 | + task() |
| 63 | + } |
| 64 | + case <-ctx.Done(): |
| 65 | + return |
| 66 | + default: |
| 67 | + if se.shutdown { |
67 | 68 | return |
68 | 69 | } |
69 | 70 | } |
70 | | - fmt.Println("OUT of the loop - 4") |
71 | | - |
72 | | - }() |
73 | | - fmt.Println("OUT of goroutine - 5") |
| 71 | + } |
74 | 72 |
|
75 | 73 | } |
76 | 74 |
|
77 | 75 | func (se *ScheduledExecutor) Close() error { |
78 | | - go func() { |
79 | | - fmt.Println("Closing scheduler...") |
80 | | - se.quit <- SHUT_DOWN |
81 | | - }() |
| 76 | + se.shutdown = true |
82 | 77 | return nil |
83 | 78 | } |
| 79 | + |
84 | 80 | func (se *ScheduledExecutor) close() { |
85 | 81 | se.ticker.Stop() |
86 | 82 | } |
0 commit comments