Skip to content

Commit 8f1ce17

Browse files
committed
fixing golang updates bugs in code
1 parent def6c96 commit 8f1ce17

3 files changed

Lines changed: 32 additions & 19 deletions

File tree

main/main.exe

2.37 MB
Binary file not shown.

main/main.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,30 @@ package main
22

33
import (
44
"fmt"
5-
"github.com/gbenroscience/scheduled-executor/utils"
5+
"sync"
66
"time"
7-
)
87

8+
"github.com/gbenroscience/scheduled-executor/utils"
9+
)
910

1011
func timeStampMillis() int {
1112
return int(time.Now().UnixNano() / 1000000)
1213
}
1314

14-
func main() {
15-
15+
func main() {
1616

1717
totalCount := 0
1818

19-
utils.NewTimedExecutor(2 * time.Second , 2 * time.Second).Start(func() {
20-
totalCount++
21-
fmt.Printf("%d.%4stime is %d\n" ,totalCount , " ", timeStampMillis())
22-
} , true)
23-
19+
var wg *sync.WaitGroup = &sync.WaitGroup{}
2420

25-
time.Sleep(time.Minute)
21+
utils.NewTimedExecutor(2*time.Second, 2*time.Second).Start(func() {
22+
totalCount++
23+
fmt.Printf("%d.%4stime is %d\n", totalCount, " ", timeStampMillis())
24+
wg.Done()
25+
}, true)
2626

27+
wg.Add(10)
2728

29+
wg.Wait()
2830

2931
}

utils/timedexecutor.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ type ScheduledExecutor struct {
1414
quit chan int
1515
}
1616

17+
const SHUT_DOWN = 1
18+
1719
func NewTimedExecutor(initialDelay time.Duration, delay time.Duration) ScheduledExecutor {
1820
return ScheduledExecutor{
1921
delay: delay,
@@ -22,20 +24,21 @@ func NewTimedExecutor(initialDelay time.Duration, delay time.Duration) Scheduled
2224
}
2325
}
2426

25-
26-
//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
27+
// 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
2728
func (se ScheduledExecutor) Start(task func(), runAsync bool) {
2829

2930
sigs := make(chan os.Signal, 1)
3031
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
3132

3233
go func() {
3334
defer func() {
35+
fmt.Println("Scheduler stopping...")
3436
se.close()
35-
fmt.Println("Scheduler stopped!!")
37+
fmt.Println("Scheduler stopped.")
3638
}()
3739
firstExec := true
3840
for {
41+
fmt.Println("IN the loop")
3942
select {
4043
case <-se.ticker.C:
4144

@@ -50,23 +53,31 @@ func (se ScheduledExecutor) Start(task func(), runAsync bool) {
5053
} else {
5154
task()
5255
}
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")
5363

54-
break
55-
case <-se.quit:
56-
return
5764
case <-sigs:
58-
_ = se.Close()
59-
break
65+
fmt.Println("AWW AWW AWW - 3")
66+
fmt.Println("breaking out of select here - 3")
67+
return
6068
}
6169
}
70+
fmt.Println("OUT of the loop - 4")
6271

6372
}()
73+
fmt.Println("OUT of goroutine - 5")
6474

6575
}
6676

6777
func (se *ScheduledExecutor) Close() error {
6878
go func() {
69-
se.quit <- 1
79+
fmt.Println("Closing scheduler...")
80+
se.quit <- SHUT_DOWN
7081
}()
7182
return nil
7283
}

0 commit comments

Comments
 (0)