Skip to content

Commit

Permalink
remove pidfile
Browse files Browse the repository at this point in the history
  • Loading branch information
dmachard committed May 31, 2024
1 parent 6c22ee7 commit 32a72a4
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions dnscollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func createPIDFile(pidFilePath string) (string, error) {
if _, err := os.Stat(pidFilePath); err == nil {
pidBytes, err := os.ReadFile(pidFilePath)
if err != nil {
return "", fmt.Errorf("failed to read PID file: %v", err)
return "", fmt.Errorf("failed to read PID file: %w", err)
}

pid, err := strconv.Atoi(string(pidBytes))
if err != nil {
return "", fmt.Errorf("invalid PID in PID file: %v", err)
return "", fmt.Errorf("invalid PID in PID file: %w", err)
}

if process, err := os.FindProcess(pid); err == nil {
Expand All @@ -69,11 +69,17 @@ func createPIDFile(pidFilePath string) (string, error) {
pidStr := strconv.Itoa(pid)
err := os.WriteFile(pidFilePath, []byte(pidStr), 0644)
if err != nil {
return "", fmt.Errorf("failed to write PID file: %v", err)
return "", fmt.Errorf("failed to write PID file: %w", err)
}
return pidStr, nil
}

func removePIDFile(config *pkgconfig.Config) {
if config.Global.PidFile != "" {
os.Remove(config.Global.PidFile)
}
}

func main() {
args := os.Args[1:] // Ignore the first argument (the program name)

Expand Down Expand Up @@ -138,7 +144,6 @@ func main() {
os.Exit(1)
}
logger.Info("main - write pid=%s to file=%s", pid, config.Global.PidFile)
defer os.Remove(config.Global.PidFile)
}

// init logger
Expand All @@ -163,6 +168,7 @@ func main() {
err := pkginit.InitPipelines(mapLoggers, mapCollectors, config, logger)
if err != nil {
logger.Error("main - %s", err.Error())
removePIDFile(config)
os.Exit(1)
}
}
Expand All @@ -183,7 +189,9 @@ func main() {
// read config
err := pkgconfig.ReloadConfig(configPath, config)
if err != nil {
panic(fmt.Sprintf("main - reload config error: %v", err))
logger.Error("main - reload config error: %v", err)
removePIDFile(config)
os.Exit(1)
}

// reload logger and multiplexer
Expand Down Expand Up @@ -215,6 +223,7 @@ func main() {
if testFlag {
// We've parsed the config and are ready to start, so the config is good enough
logger.Info("main - config OK!")
removePIDFile(config)
os.Exit(0)
}

Expand All @@ -229,5 +238,6 @@ func main() {
// block main
<-done

removePIDFile(config)
logger.Info("main - stopped")
}

0 comments on commit 32a72a4

Please sign in to comment.