feat: NotifyContext

This commit is contained in:
Jason Song committed 2023-04-04 17:56:34 +08:00
1 parent 45d8e2d86d
commit f110c2f879
1 file changed
+2 -20
+2 -20
View File
@@ -5,33 +5,15 @@ package main
import (
"context"
"os"
"os/signal"
"syscall"
"gitea.com/gitea/act_runner/internal/app/cmd"
)
func withContextFunc(ctx context.Context, f func()) context.Context {
ctx, cancel := context.WithCancel(ctx)
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(c)
select {
case <-ctx.Done():
case <-c:
cancel()
f()
}
}()
return ctx
}
func main() {
ctx := withContextFunc(context.Background(), func() {})
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
// run the command
cmd.Execute(ctx)
}