-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
53 lines (46 loc) · 1.51 KB
/
main.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
package main
import (
"context"
"os"
"os/signal"
"github.com/marema31/kamino/cmd"
)
func main() {
log := cmd.GetLogger()
// Synchro beetween the main goroutine and the Execution goroutine
end := make(chan error, 1)
// Create the application context for correct sub-task abortion if CTRL+C
ctx := context.Background()
// trap Ctrl+C
ctx, cancel := context.WithCancel(ctx)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt)
// At end of function we remove signal trapping
defer signal.Stop(sigChan)
//This function will stop itself at context cancellation or at end of main goroutine
go func() {
end <- cmd.Execute(ctx)
}()
//Waiting for signal on the channel and call cancel on the context
select {
case <-sigChan: //Received CTRL+C
cancel() // Cancellation of context, that will propagate to all function that listen ctx.Done
log.Warn("Aborting ....")
log.Info("Waiting for all sub task abortion...")
<-end // We wait for the main goroutine to end
log.Info("exiting on CTRL+C")
os.Exit(1)
case <-ctx.Done(): // the context has been cancelled (It should not happen since only the previous case fire a cancellation)
log.Info("Waiting for all sub task abortion...")
<-end // We wait for the main goroutine to end
log.Info("exiting on cancel")
os.Exit(1)
case executeError := <-end: //The goroutine executing the action is finished we can stop here
if executeError != nil {
log.Infof("exiting on error: %v", executeError)
os.Exit(1)
}
}
log.Info("bye")
os.Exit(0)
}