cmd/stepd: PID file is properly removed now

This commit is contained in:
Sasha Koshka 2024-12-08 20:22:35 -05:00
parent e79417ec9d
commit 04149fe218

View File

@ -7,6 +7,7 @@ import "time"
import "errors" import "errors"
import "context" import "context"
import "net/http" import "net/http"
import "path/filepath"
import "git.tebibyte.media/sashakoshka/step" import "git.tebibyte.media/sashakoshka/step"
import "git.tebibyte.media/sashakoshka/go-cli" import "git.tebibyte.media/sashakoshka/go-cli"
import "git.tebibyte.media/sashakoshka/step/providers" import "git.tebibyte.media/sashakoshka/step/providers"
@ -48,6 +49,21 @@ func main () {
log.Println(`Scriptable Template Processor`) log.Println(`Scriptable Template Processor`)
log.Println(`... initializing`) 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 // the single argument is for the directory to serve. we actually cd
// there. // there.
if len(cmd.Args) == 1 { if len(cmd.Args) == 1 {
@ -61,7 +77,7 @@ func main () {
// set up the environment // set up the environment
environment := step.Environment { } environment := step.Environment { }
environment.FuncProviders = providers.All() environment.FuncProviders = providers.All()
err := environment.Init(context.Background()) err = environment.Init(context.Background())
if err != nil { log.Fatal(err) } if err != nil { log.Fatal(err) }
// set up the HTTP handler // set up the HTTP handler
@ -88,19 +104,6 @@ func main () {
} }
log.Println(`.// initialized.`) 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) log.Printf("(i) listening on %s\n", httpServer.Addr)
if err := manager.Run(ctx); err != nil && !errors.Is(err, context.Canceled) { if err := manager.Run(ctx); err != nil && !errors.Is(err, context.Canceled) {