diff options
author | Bo-Yi Wu <appleboy.tw@gmail.com> | 2022-07-13 07:44:14 +0200 |
---|---|---|
committer | Jason Song <i@wolfogre.com> | 2022-11-24 08:36:25 +0100 |
commit | e54de3724292fa3fc4a50f1e1186964ce1e7a816 (patch) | |
tree | d6f82f5f9ee5b15beb9056fcfcfa6b8fed48960e /main.go | |
parent | stash (diff) | |
download | forgejo-runner-e54de3724292fa3fc4a50f1e1186964ce1e7a816.tar.xz forgejo-runner-e54de3724292fa3fc4a50f1e1186964ce1e7a816.zip |
refactor: Add graceful shutdown signal notify func
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 23 |
1 files changed, 12 insertions, 11 deletions
@@ -9,25 +9,26 @@ import ( "gitea.com/gitea/act_runner/cmd" ) -func main() { - ctx := context.Background() +func withContextFunc(ctx context.Context, f func()) context.Context { ctx, cancel := context.WithCancel(ctx) - - // trap Ctrl+C and call cancel on the context - c := make(chan os.Signal, 1) - signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) - defer func() { - signal.Stop(c) - cancel() - }() 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() - case <-ctx.Done(): + f() } }() + return ctx +} + +func main() { + ctx := withContextFunc(context.Background(), func() {}) // run the command cmd.Execute(ctx) } |