refactor: Add graceful shutdown signal notify func
This commit is contained in:
parent
08282a519f
commit
e54de37242
21
main.go
21
main.go
|
@ -9,25 +9,26 @@ import (
|
||||||
"gitea.com/gitea/act_runner/cmd"
|
"gitea.com/gitea/act_runner/cmd"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func withContextFunc(ctx context.Context, f func()) context.Context {
|
||||||
ctx := context.Background()
|
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
go func() {
|
||||||
// trap Ctrl+C and call cancel on the context
|
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
||||||
defer func() {
|
defer signal.Stop(c)
|
||||||
signal.Stop(c)
|
|
||||||
cancel()
|
|
||||||
}()
|
|
||||||
go func() {
|
|
||||||
select {
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
case <-c:
|
case <-c:
|
||||||
cancel()
|
cancel()
|
||||||
case <-ctx.Done():
|
f()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx := withContextFunc(context.Background(), func() {})
|
||||||
// run the command
|
// run the command
|
||||||
cmd.Execute(ctx)
|
cmd.Execute(ctx)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue