From 04149fe218544b70d53f990e3d923c93c8c4e738 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 8 Dec 2024 20:22:35 -0500 Subject: [PATCH] cmd/stepd: PID file is properly removed now --- cmd/stepd/main.go | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/cmd/stepd/main.go b/cmd/stepd/main.go index cfa2b5b..b513c8e 100644 --- a/cmd/stepd/main.go +++ b/cmd/stepd/main.go @@ -7,6 +7,7 @@ import "time" import "errors" import "context" import "net/http" +import "path/filepath" import "git.tebibyte.media/sashakoshka/step" import "git.tebibyte.media/sashakoshka/go-cli" import "git.tebibyte.media/sashakoshka/step/providers" @@ -48,6 +49,21 @@ func main () { log.Println(`Scriptable Template Processor`) log.Println(`... initializing`) + // manage start and end of program + ctx, done := context.WithCancel(context.Background()) + daemon.OnSigint(done) + pidFileAbs, err := filepath.Abs(flagPidFile.Value) + if err != nil { log.Fatalln("XXX", err) } + pidFile := daemon.PidFile(pidFileAbs) + if !pidFile.Empty() { + err := pidFile.Start() + if err != nil { log.Println("!!! could not write pid:", err) } + defer func () { + err := pidFile.Close() + if err != nil { log.Println("!!! could not delete pidfile:", err) } + } () + } + // the single argument is for the directory to serve. we actually cd // there. if len(cmd.Args) == 1 { @@ -61,7 +77,7 @@ func main () { // set up the environment environment := step.Environment { } environment.FuncProviders = providers.All() - err := environment.Init(context.Background()) + err = environment.Init(context.Background()) if err != nil { log.Fatal(err) } // set up the HTTP handler @@ -88,19 +104,6 @@ func main () { } log.Println(`.// initialized.`) - - // be a daemon - ctx, done := context.WithCancel(context.Background()) - daemon.OnSigint(done) - pidfile := daemon.PidFile(flagPidFile.Value) - if !pidfile.Empty() { - err := pidfile.Start() - if err != nil { log.Println("!!! could not write pid:", err) } - defer func () { - err := pidfile.Close() - if err != nil { log.Println("!!! could not delete pidfile:", err) } - } () - } log.Printf("(i) listening on %s\n", httpServer.Addr) if err := manager.Run(ctx); err != nil && !errors.Is(err, context.Canceled) {