-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathrunner_tracker.go
63 lines (54 loc) · 1.54 KB
/
runner_tracker.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
59
60
61
62
63
package runner
import (
"github.com/project-flogo/core/app"
"github.com/project-flogo/core/support/log"
"sync"
"time"
)
func NewRunnerTracker() *RunnerTracker {
return &RunnerTracker{runnertrackerwg: &sync.WaitGroup{}}
}
type RunnerTracker struct {
runnertrackerwg *sync.WaitGroup
}
func (rt RunnerTracker) AddRunner() {
rt.runnertrackerwg.Add(1)
}
func (rt RunnerTracker) RemoveRunner() {
rt.runnertrackerwg.Done()
}
func (rt RunnerTracker) WaitForAllRunners() {
rt.runnertrackerwg.Wait()
}
func (rt RunnerTracker) WaitForRunnersCompletion(timeout time.Duration) bool {
c := make(chan struct{})
go func() {
defer close(c)
rt.WaitForAllRunners()
}()
select {
case <-c:
return false // actions completed
case <-time.After(timeout):
return true // timed out
}
}
func (rt RunnerTracker) gracefulStop() (runnercompleted bool) {
logger := log.RootLogger()
delayedStopInterval := app.GetDelayedStopInterval()
if delayedStopInterval != "" {
duration, err := time.ParseDuration(delayedStopInterval)
if err != nil {
logger.Errorf("Invalid interval - %s specified for delayed stop. It must suffix with time unit e.g. %sms, %ss", delayedStopInterval, delayedStopInterval, delayedStopInterval)
} else {
logger.Infof("Delaying application stop by max - %s", delayedStopInterval)
if isTimeout := rt.WaitForRunnersCompletion(duration); isTimeout {
logger.Info("All actions not completed before engine shutdown")
} else {
runnercompleted = true
logger.Info("All actions completed before engine shutdown")
}
}
}
return
}