Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supervise cron process for better logging/signal handling/environment management #229

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions bin/barman_cron
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# Identify the process id of the cron supervisor process
cron_pid=$(pgrep -f '/usr/sbin/cron')
if [ -z "$cron_pid" ]; then
# Send the error message to main process's stderr
echo "Failed to resolve cron process id" >> /proc/1/fd/2
exit 1
fi

barman_output=$(barman cron 2>&1)
barman_exit_code=$?

# Log the result of barman cron
if [ $barman_exit_code -ne 0 ]; then
echo "Barman cron failed with exit code $barman_exit_code: $barman_output" >> /proc/$cron_pid/fd/2
exit 1
fi
1 change: 1 addition & 0 deletions cmd/start/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func main() {
}

svisor := supervisor.New("flybarman", 1*time.Minute)
svisor.AddProcess("cron", "/usr/sbin/cron -f", supervisor.WithRestart(0, 5*time.Second))
svisor.AddProcess("barman", fmt.Sprintf("tail -f %s", node.LogFile))
svisor.AddProcess("admin", "/usr/local/bin/start_admin_server",
supervisor.WithRestart(0, 5*time.Second),
Expand Down
22 changes: 4 additions & 18 deletions internal/flybarman/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,12 @@ wal_retention_policy = main
return fmt.Errorf("failed to write file %s: %s", n.RootPasswordConfigPath, err)
}

if _, err := os.Stat(n.BarmanCronFile); os.IsNotExist(err) {
barmanCronFileContent := `* * * * * /usr/bin/barman cron
barmanCronFileContent := `* * * * * /usr/local/bin/barman_cron
`
if err := os.WriteFile(n.BarmanCronFile, []byte(barmanCronFileContent), 0644); err != nil {
return fmt.Errorf("failed write %s: %s", n.BarmanCronFile, err)
}

log.Println(n.BarmanCronFile + " created successfully.")
if err := os.WriteFile(n.BarmanCronFile, []byte(barmanCronFileContent), 0644); err != nil {
return fmt.Errorf("failed write %s: %s", n.BarmanCronFile, err)
}
log.Println(n.BarmanCronFile + " created successfully.")

if _, err := os.Stat(n.LogFile); os.IsNotExist(err) {
file, err := os.Create(n.LogFile)
Expand All @@ -167,17 +164,6 @@ wal_retention_policy = main

log.Println("Crontab updated")

serviceCmd := exec.Command("/usr/sbin/service", "--version")
if err := serviceCmd.Run(); err != nil {
log.Println("service command not found, skipping initializing cron service")
} else {
serviceCronStartCommand := exec.Command("service", "cron", "start")
if _, err := serviceCronStartCommand.Output(); err != nil {
return fmt.Errorf("failed starting cron service: %s", err)
}
log.Println("Started cron service")
}

switchWalCommand := exec.Command("barman", "switch-wal", "--archive", "--force", "pg")
if _, err := switchWalCommand.Output(); err != nil {
log.Println(fmt.Errorf("failed switching WAL: %s", err))
Expand Down
Loading