From f110c2f879f79809b1eb902f7ee0341061b4c32a Mon Sep 17 00:00:00 2001 From: Jason Song Date: Tue, 4 Apr 2023 17:56:34 +0800 Subject: [PATCH] feat: NotifyContext --- main.go | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/main.go b/main.go index 95a34d0..4adbd13 100644 --- a/main.go +++ b/main.go @@ -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) }