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

Commit abbe9e8

Browse files
committed
Fix: #878 add PID file support
1 parent bc738d5 commit abbe9e8

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

snapteld.go

+31
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ var (
7979
Usage: fmt.Sprintf("1-5 (Debug, Info, Warning, Error, Fatal; default: %v)", defaultLogLevel),
8080
EnvVar: "SNAP_LOG_LEVEL",
8181
}
82+
flPIDFile = cli.StringFlag{
83+
Name: "pid-file, pidfile, P",
84+
Usage: "File to write PID of snapteld, if set.",
85+
EnvVar: "SNAP_PID_FILE",
86+
}
8287
flConfig = cli.StringFlag{
8388
Name: "config",
8489
Usage: "A path to a config file",
@@ -127,6 +132,7 @@ type Config struct {
127132
LogPath string `json:"log_path,omitempty"yaml:"log_path,omitempty"`
128133
LogTruncate bool `json:"log_truncate,omitempty"yaml:"log_truncate,omitempty"`
129134
LogColors bool `json:"log_colors,omitempty"yaml:"log_colors,omitempty"`
135+
PIDFile string `json:"pid_file,omitempty"yaml:"pid_file,omitempty"`
130136
Control *control.Config `json:"control,omitempty"yaml:"control,omitempty"`
131137
Scheduler *scheduler.Config `json:"scheduler,omitempty"yaml:"scheduler,omitempty"`
132138
RestAPI *rest.Config `json:"restapi,omitempty"yaml:"restapi,omitempty"`
@@ -162,6 +168,10 @@ const (
162168
"type": "integer",
163169
"minimum": 1
164170
},
171+
"pid_file": {
172+
"description": "file for snpateld to write PID",
173+
"type": "string"
174+
},
165175
"control": { "$ref": "#/definitions/control" },
166176
"scheduler": { "$ref": "#/definitions/scheduler"},
167177
"restapi" : { "$ref": "#/definitions/restapi"},
@@ -219,6 +229,7 @@ func main() {
219229
flLogTruncate,
220230
flLogColors,
221231
flMaxProcs,
232+
flPIDFile,
222233
flConfig,
223234
}
224235
cliApp.Flags = append(cliApp.Flags, control.Flags...)
@@ -323,6 +334,25 @@ func action(ctx *cli.Context) error {
323334
// Set Max Processors for snapteld.
324335
setMaxProcs(cfg.GoMaxProcs)
325336

337+
// Write PID file, if configured.
338+
log.Info("Config PID file", cfg.PIDFile)
339+
if cfg.PIDFile != "" {
340+
log.Info("Creating PID file", cfg.PIDFile)
341+
f, err := os.OpenFile(cfg.PIDFile, os.O_CREATE|os.O_WRONLY, 0644)
342+
if err != nil {
343+
log.Error("Unable to create pidfile", err)
344+
} else {
345+
fmt.Fprintf(f, "%d\n", os.Getpid())
346+
f.Close()
347+
defer func() {
348+
err := os.Remove(cfg.PIDFile)
349+
if err != nil {
350+
log.Error("Unable to remove pidfile", err)
351+
}
352+
}()
353+
}
354+
}
355+
326356
c := control.New(cfg.Control)
327357
if c.Config.AutoDiscoverPath != "" && c.Config.IsTLSEnabled() {
328358
log.Fatal("TLS security is not supported in autodiscovery mode")
@@ -843,6 +873,7 @@ func applyCmdLineFlags(cfg *Config, ctx runtimeFlagsContext) {
843873
cfg.LogPath = setStringVal(cfg.LogPath, ctx, "log-path")
844874
cfg.LogTruncate = setBoolVal(cfg.LogTruncate, ctx, "log-truncate")
845875
cfg.LogColors = setBoolVal(cfg.LogColors, ctx, "log-colors")
876+
cfg.PIDFile = setStringVal(cfg.PIDFile, ctx, "pid-file")
846877
// next for the flags related to the control package
847878
cfg.Control.MaxRunningPlugins = setIntVal(cfg.Control.MaxRunningPlugins, ctx, "max-running-plugins")
848879
cfg.Control.PluginLoadTimeout = setIntVal(cfg.Control.PluginLoadTimeout, ctx, "plugin-load-timeout")

snapteld_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var validCmdlineFlags_input = mockFlags{
4343
"log-path": "/no/logs/allowed",
4444
"log-truncate": "true",
4545
"log-colors": "true",
46+
"pid-file": "/no/pidfile/here",
4647
"max-running-plugins": "12",
4748
"plugin-load-timeout": "20",
4849
"plugin-trust": "1",
@@ -118,6 +119,7 @@ var validCmdlineFlags_expected = &Config{
118119
LogPath: "/no/logs/allowed",
119120
LogTruncate: true,
120121
LogColors: true,
122+
PIDFile: "/no/pidfile/here",
121123
}
122124

123125
func TestSnapConfig(t *testing.T) {

0 commit comments

Comments
 (0)