Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Add PID file support #1733

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions snapteld.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ var (
Usage: fmt.Sprintf("1-5 (Debug, Info, Warning, Error, Fatal; default: %v)", defaultLogLevel),
EnvVar: "SNAP_LOG_LEVEL",
}
flPIDFile = cli.StringFlag{
Name: "pid-file, pidfile, P",
Usage: "File to write PID of snapteld, if set.",
EnvVar: "SNAP_PID_FILE",
}
flConfig = cli.StringFlag{
Name: "config",
Usage: "A path to a config file",
Expand Down Expand Up @@ -127,6 +132,7 @@ type Config struct {
LogPath string `json:"log_path,omitempty"yaml:"log_path,omitempty"`
LogTruncate bool `json:"log_truncate,omitempty"yaml:"log_truncate,omitempty"`
LogColors bool `json:"log_colors,omitempty"yaml:"log_colors,omitempty"`
PIDFile string `json:"pid_file,omitempty"yaml:"pid_file,omitempty"`
Control *control.Config `json:"control,omitempty"yaml:"control,omitempty"`
Scheduler *scheduler.Config `json:"scheduler,omitempty"yaml:"scheduler,omitempty"`
RestAPI *rest.Config `json:"restapi,omitempty"yaml:"restapi,omitempty"`
Expand Down Expand Up @@ -162,6 +168,10 @@ const (
"type": "integer",
"minimum": 1
},
"pid_file": {
"description": "file for snpateld to write PID",
"type": "string"
},
"control": { "$ref": "#/definitions/control" },
"scheduler": { "$ref": "#/definitions/scheduler"},
"restapi" : { "$ref": "#/definitions/restapi"},
Expand Down Expand Up @@ -219,6 +229,7 @@ func main() {
flLogTruncate,
flLogColors,
flMaxProcs,
flPIDFile,
flConfig,
}
cliApp.Flags = append(cliApp.Flags, control.Flags...)
Expand Down Expand Up @@ -323,6 +334,25 @@ func action(ctx *cli.Context) error {
// Set Max Processors for snapteld.
setMaxProcs(cfg.GoMaxProcs)

// Write PID file, if configured.
log.Info("Config PID file", cfg.PIDFile)
if cfg.PIDFile != "" {
log.Info("Creating PID file", cfg.PIDFile)
f, err := os.OpenFile(cfg.PIDFile, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Error("Unable to create pidfile", err)
} else {
fmt.Fprintf(f, "%d\n", os.Getpid())
f.Close()
defer func() {
err := os.Remove(cfg.PIDFile)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I have a stupid question to ask.
Why delete the PIDFile?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most servers are expected to delete their PID file when the server shuts down — for example, Apache removes its PID file when shutting down.

However, since snapteld calls os.Exit() when it shuts down, I recently realized that this deferred function will not be executed, since os.Exit shuts down immediately without calling deferred functions.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cce
Thanks for your explanation!
Yes, I think the the actual behavior is not your meaning.
Could you please update this PR according your design when you are free?

if err != nil {
log.Error("Unable to remove pidfile", err)
}
}()
}
}

c := control.New(cfg.Control)
if c.Config.AutoDiscoverPath != "" && c.Config.IsTLSEnabled() {
log.Fatal("TLS security is not supported in autodiscovery mode")
Expand Down Expand Up @@ -843,6 +873,7 @@ func applyCmdLineFlags(cfg *Config, ctx runtimeFlagsContext) {
cfg.LogPath = setStringVal(cfg.LogPath, ctx, "log-path")
cfg.LogTruncate = setBoolVal(cfg.LogTruncate, ctx, "log-truncate")
cfg.LogColors = setBoolVal(cfg.LogColors, ctx, "log-colors")
cfg.PIDFile = setStringVal(cfg.PIDFile, ctx, "pid-file")
// next for the flags related to the control package
cfg.Control.MaxRunningPlugins = setIntVal(cfg.Control.MaxRunningPlugins, ctx, "max-running-plugins")
cfg.Control.PluginLoadTimeout = setIntVal(cfg.Control.PluginLoadTimeout, ctx, "plugin-load-timeout")
Expand Down
2 changes: 2 additions & 0 deletions snapteld_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var validCmdlineFlags_input = mockFlags{
"log-path": "/no/logs/allowed",
"log-truncate": "true",
"log-colors": "true",
"pid-file": "/no/pidfile/here",
"max-running-plugins": "12",
"plugin-load-timeout": "20",
"plugin-trust": "1",
Expand Down Expand Up @@ -118,6 +119,7 @@ var validCmdlineFlags_expected = &Config{
LogPath: "/no/logs/allowed",
LogTruncate: true,
LogColors: true,
PIDFile: "/no/pidfile/here",
}

func TestSnapConfig(t *testing.T) {
Expand Down