vspd: Run deferred tasks on os.Exit.

Any tasks deferred in the main func will not execute if os.Exit is
called. Moving application logic down into a new func which is called by
main works around this limitation.
This commit is contained in:
jholdstock 2023-09-08 11:17:16 +01:00 committed by Jamie Holdstock
parent 3967d9e24e
commit e38032ba2f

View File

@ -10,19 +10,24 @@ import (
)
func main() {
os.Exit(run())
}
// run is the real main function for vspd. It is necessary to work around the
// fact that deferred functions do not run when os.Exit() is called.
func run() int {
// Load config file and parse CLI args.
cfg, err := loadConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "loadConfig error: %v\n", err)
os.Exit(1)
return 1
}
vspd, err := newVspd(cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "newVspd error: %v\n", err)
os.Exit(1)
return 1
}
// Run until an exit code is returned.
os.Exit(vspd.run())
return vspd.run()
}