Skip to content

Commit 85582e1

Browse files
committed
upgraded functionality
1 parent 8f1ce17 commit 85582e1

File tree

3 files changed

+63
-54
lines changed

3 files changed

+63
-54
lines changed

main/main.exe

33.5 KB
Binary file not shown.

main/main.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
"sync"
65
"time"
76

87
"github.com/gbenroscience/scheduled-executor/utils"
@@ -15,17 +14,31 @@ func timeStampMillis() int {
1514
func main() {
1615

1716
totalCount := 0
17+
const MAX_CYCLES = 5
1818

19-
var wg *sync.WaitGroup = &sync.WaitGroup{}
19+
sc := utils.NewTimedExecutor(2*time.Second, 500*time.Millisecond)
2020

21-
utils.NewTimedExecutor(2*time.Second, 2*time.Second).Start(func() {
21+
sc.Start(func() {
2222
totalCount++
2323
fmt.Printf("%d.%4stime is %d\n", totalCount, " ", timeStampMillis())
24-
wg.Done()
24+
if totalCount > MAX_CYCLES {
25+
sc.Close()
26+
}
2527
}, true)
2628

27-
wg.Add(10)
29+
}
30+
31+
/*
32+
func main() {
33+
34+
totalCount := 0
35+
36+
utils.NewTimedExecutor(2*time.Second, 2*time.Second).Start(func() {
37+
totalCount++
38+
fmt.Printf("%d.%4stime is %d\n", totalCount, " ", timeStampMillis())
39+
}, true)
2840
29-
wg.Wait()
41+
time.Sleep(time.Minute)
3042
3143
}
44+
*/

utils/timedexecutor.go

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,82 @@
11
package utils
22

33
import (
4-
"fmt"
4+
"context"
55
"os"
66
"os/signal"
77
"syscall"
88
"time"
99
)
1010

1111
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
1516
}
1617

17-
const SHUT_DOWN = 1
18-
1918
func NewTimedExecutor(initialDelay time.Duration, delay time.Duration) ScheduledExecutor {
2019
return ScheduledExecutor{
2120
delay: delay,
2221
ticker: *time.NewTicker(initialDelay),
23-
quit: make(chan int),
2422
}
2523
}
2624

2725
// 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()
2930

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)
3234

3335
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+
}()
4439

45-
if firstExec {
46-
se.ticker.Stop()
47-
se.ticker = *time.NewTicker(se.delay)
48-
firstExec = false
49-
}
40+
firstExec := true
5041

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:
6352

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 {
6768
return
6869
}
6970
}
70-
fmt.Println("OUT of the loop - 4")
71-
72-
}()
73-
fmt.Println("OUT of goroutine - 5")
71+
}
7472

7573
}
7674

7775
func (se *ScheduledExecutor) Close() error {
78-
go func() {
79-
fmt.Println("Closing scheduler...")
80-
se.quit <- SHUT_DOWN
81-
}()
76+
se.shutdown = true
8277
return nil
8378
}
79+
8480
func (se *ScheduledExecutor) close() {
8581
se.ticker.Stop()
8682
}

0 commit comments

Comments
 (0)