cmd/stepd: Write PID file

Closes #9
This commit is contained in:
Sasha Koshka 2024-12-08 19:45:14 -05:00
parent deb3d7e935
commit d06b03bbfe

View File

@ -17,19 +17,26 @@ import "git.tebibyte.media/sashakoshka/go-service/routines"
func main () { func main () {
// parse command line arguments // parse command line arguments
flagPidFile := cli.NewInputFlag (
'p', "pid-file",
"Write the PID to the specified file",
"", cli.ValString)
flagAddress := cli.NewInputFlag ( flagAddress := cli.NewInputFlag (
'a', "address", 'a', "address",
"The address to host the server on", "The address to host the server on",
":8080", cli.ValString) ":8080", cli.ValString)
// TODO have in conf, override here
flagErrorDocument := cli.NewInputFlag ( flagErrorDocument := cli.NewInputFlag (
0, "error-document", 0, "error-document",
"The document to use for displaying errors", "The document to use for displaying errors",
"", cli.ValString) "", cli.ValString)
// TODO have in conf, override here
flagDirectories := cli.NewFlag ( flagDirectories := cli.NewFlag (
'd', "directories", 'd', "directories",
"Serve the contents of directories") "Serve the contents of directories")
cmd := cli.New ( cmd := cli.New (
"Run an HTTP server that automaticaly executes STEP files", "Run an HTTP server that automaticaly executes STEP files",
flagPidFile,
flagAddress, flagAddress,
flagErrorDocument, flagErrorDocument,
flagDirectories, flagDirectories,
@ -75,8 +82,18 @@ func main () {
log.Println(`.// initialized.`) log.Println(`.// initialized.`)
// be a daemon
ctx, done := context.WithCancel(context.Background()) ctx, done := context.WithCancel(context.Background())
daemon.OnSigint(done) 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) {