From fc8c58a1e6426dac9279f0e83aea82d04be48b22 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 21 Jan 2021 00:17:32 -0500 Subject: [PATCH] refactor: ensure crontab files are properly deleted --- plugins/cron/functions.go | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/plugins/cron/functions.go b/plugins/cron/functions.go index d2c3bf1b7..8318856ce 100644 --- a/plugins/cron/functions.go +++ b/plugins/cron/functions.go @@ -67,10 +67,28 @@ func fetchCronEntries(appName string) ([]templateCommand, error) { return commands, nil } +func deleteCrontab() error { + command := common.NewShellCmd("crontab -l -u dokku") + command.ShowOutput = false + if !command.Execute() { + return nil + } + + command = common.NewShellCmd("crontab -r -u dokku") + command.ShowOutput = false + out, err := command.CombinedOutput() + if err != nil { + return fmt.Errorf("Unable to remove schedule file: %v", string(out)) + } + + common.LogInfo1("Removed") + return nil +} + func writeCronEntries() error { apps, err := common.DokkuApps() if err != nil { - return nil + return deleteCrontab() } commands := []templateCommand{} @@ -89,16 +107,7 @@ func writeCronEntries() error { } if len(commands) == 0 { - if !common.FileExists("/var/spool/cron/crontabs/dokku") { - return nil - } - - command := common.NewShellCmd("crontab -r -u dokku") - command.ShowOutput = false - out, err := command.CombinedOutput() - if err != nil { - return fmt.Errorf("Unable to remove schedule file: %v", out) - } + return deleteCrontab() } data := map[string]interface{}{