fix: reset MAILTO after a cron entry that sets its own

A task with its own `MAILTO` is now written in place, followed by a line resetting `MAILTO` to the global value, or to the `dokku` user when none is set, rather than grouped at the end of the crontab, so no other task is mailed to its recipient whatever order the tasks are written in.
This commit is contained in:
Jose Diaz-Gonzalez committed 2026-09-30 17:47:27 -04:00
1 parent f91b1f7fc7
commit b158f482ef
6 files changed
+89 -73

No files matched your search

+18 -38
View File
@@ -4,9 +4,7 @@ import (
_ "embed"
"encoding/json"
"fmt"
"maps"
"os"
"slices"
"strings"
"text/template"
@@ -19,6 +17,9 @@ import (
//go:embed templates/cron.tmpl
var cronTemplate string
// crontabUser is the user whose crontab the host-cron tasks are written to
const crontabUser = "dokku"
// usesHostCron reports whether the given scheduler writes its cron tasks to the
// host crontab (as opposed to managing its own cron backend). An empty scheduler
// or an unimplemented scheduler-uses-host-cron trigger is treated as false.
@@ -281,7 +282,7 @@ func writeCronTab() error {
result, err := common.CallExecCommand(common.ExecCommandInput{
Command: "crontab",
Args: []string{"-u", "dokku", tmpFile.Name()},
Args: []string{"-u", crontabUser, tmpFile.Name()},
})
if err != nil {
return fmt.Errorf("Unable to update schedule file: %w", err)
@@ -295,43 +296,22 @@ func writeCronTab() error {
return nil
}
// cronMailtoGroup is the set of tasks written to the crontab under a MAILTO of
// their own
type cronMailtoGroup struct {
// Mailto is the MAILTO value the tasks are written under
Mailto string
// Tasks are the tasks written under the MAILTO value
Tasks []CronTask
}
// cronTemplateData builds the data the cron template is executed with. Tasks
// that set their own MAILTO are grouped by it and written after every other
// task, so the global MAILTO applies to all of the tasks before them.
// cronTemplateData builds the data the cron template is executed with. A task
// that sets its own MAILTO is written between a MAILTO line of its own and one
// resetting it, so every task after it goes back to the global MAILTO. Without
// a global MAILTO it is reset to the crontab's owner, which is who cron mails
// when MAILTO is unset - an empty MAILTO would stop cron mailing anyone.
func cronTemplateData(tasks []CronTask, mailfrom string, mailto string) map[string]interface{} {
globalTasks := []CronTask{}
groups := map[string][]CronTask{}
for _, task := range tasks {
if task.Mailto == "" {
globalTasks = append(globalTasks, task)
continue
}
groups[task.Mailto] = append(groups[task.Mailto], task)
}
mailtoGroups := []cronMailtoGroup{}
for _, groupMailto := range slices.Sorted(maps.Keys(groups)) {
mailtoGroups = append(mailtoGroups, cronMailtoGroup{
Mailto: groupMailto,
Tasks: groups[groupMailto],
})
resetMailto := mailto
if resetMailto == "" {
resetMailto = crontabUser
}
return map[string]interface{}{
"Tasks": globalTasks,
"MailtoGroups": mailtoGroups,
"Mailfrom": mailfrom,
"Mailto": mailto,
"Tasks": tasks,
"Mailfrom": mailfrom,
"Mailto": mailto,
"ResetMailto": resetMailto,
}
}
@@ -339,7 +319,7 @@ func cronTemplateData(tasks []CronTask, mailfrom string, mailto string) map[stri
func deleteCrontab() error {
result, err := common.CallExecCommand(common.ExecCommandInput{
Command: "crontab",
Args: []string{"-l", "-u", "dokku"},
Args: []string{"-l", "-u", crontabUser},
})
if err != nil || result.ExitCode != 0 {
return nil
@@ -347,7 +327,7 @@ func deleteCrontab() error {
result, err = common.CallExecCommand(common.ExecCommandInput{
Command: "crontab",
Args: []string{"-r", "-u", "dokku"},
Args: []string{"-r", "-u", crontabUser},
})
if err != nil {
return fmt.Errorf("Unable to remove schedule file: %w", err)
+57 -28
View File
@@ -153,16 +153,10 @@ func TestParseInjectedCronEntriesKeepsTextIDs(t *testing.T) {
}
}
// TestCronTemplateGroupsTasksByMailto pins that tasks with their own MAILTO are
// written after every task that uses the global MAILTO, grouped under a MAILTO
// line per recipient.
func TestCronTemplateGroupsTasksByMailto(t *testing.T) {
tasks := []CronTask{
{Schedule: "@daily", AltCommand: "/bin/zeta", LogFile: "/var/log/dokku/zeta.log", Mailto: "zeta@example.com"},
{Schedule: "@daily", AltCommand: "/bin/true", LogFile: "/var/log/dokku/log.log"},
{Schedule: "@hourly", AltCommand: "/bin/alpha", LogFile: "/var/log/dokku/alpha.log", Mailto: "alpha@example.com"},
{Schedule: "@weekly", AltCommand: "/bin/zeta-weekly", Mailto: "zeta@example.com"},
}
// renderCronTemplate renders the cron template for the given tasks and global
// MAILTO
func renderCronTemplate(t *testing.T, tasks []CronTask, mailto string) string {
t.Helper()
tmpl, err := getCronTemplate()
if err != nil {
@@ -170,46 +164,81 @@ func TestCronTemplateGroupsTasksByMailto(t *testing.T) {
}
var got bytes.Buffer
if err := tmpl.Execute(&got, cronTemplateData(tasks, "", "global@example.com")); err != nil {
if err := tmpl.Execute(&got, cronTemplateData(tasks, "", mailto)); err != nil {
t.Fatalf("Execute() returned an error: %v", err)
}
return got.String()
}
// TestCronTemplateResetsMailtoAfterATaskWithItsOwn pins that a task with its own
// MAILTO is written in place between a MAILTO line of its own and one resetting
// it to the global MAILTO, so no task after it is mailed to its recipient.
func TestCronTemplateResetsMailtoAfterATaskWithItsOwn(t *testing.T) {
tasks := []CronTask{
{Schedule: "@daily", AltCommand: "/bin/true", LogFile: "/var/log/dokku/log.log"},
{Schedule: "@hourly", AltCommand: "/bin/alpha", LogFile: "/var/log/dokku/alpha.log", Mailto: "alpha@example.com"},
{Schedule: "@daily", AltCommand: "/bin/false"},
{Schedule: "@daily", AltCommand: "/bin/zeta", LogFile: "/var/log/dokku/zeta.log", Mailto: "zeta@example.com"},
{Schedule: "@weekly", AltCommand: "/bin/zeta-weekly", Mailto: "zeta@example.com"},
}
want := strings.Join([]string{
"MAILTO=global@example.com",
"PATH=/usr/local/bin:/usr/bin:/bin",
"SHELL=/bin/bash",
"",
"@daily /bin/true &>> /var/log/dokku/log.log",
"",
"MAILTO=alpha@example.com",
"@hourly /bin/alpha 2>&1 | tee -a /var/log/dokku/alpha.log",
"",
"MAILTO=global@example.com",
"@daily /bin/false",
"MAILTO=zeta@example.com",
"@daily /bin/zeta 2>&1 | tee -a /var/log/dokku/zeta.log",
"MAILTO=global@example.com",
"MAILTO=zeta@example.com",
"@weekly /bin/zeta-weekly",
"MAILTO=global@example.com",
"",
}, "\n")
if got.String() != want {
t.Errorf("rendered crontab =\n%s\nwant\n%s", got.String(), want)
if got := renderCronTemplate(t, tasks, "global@example.com"); got != want {
t.Errorf("rendered crontab =\n%s\nwant\n%s", got, want)
}
}
// TestCronTemplateWithoutMailtoGroups pins that a crontab without tasks that set
// TestCronTemplateResetsMailtoToTheCrontabOwner pins that without a global
// MAILTO, a task with its own is followed by a MAILTO naming the crontab's
// owner, who cron mails when MAILTO is unset. An empty MAILTO would stop cron
// mailing anyone about the tasks after it.
func TestCronTemplateResetsMailtoToTheCrontabOwner(t *testing.T) {
tasks := []CronTask{
{Schedule: "@hourly", AltCommand: "/bin/alpha", LogFile: "/var/log/dokku/alpha.log", Mailto: "alpha@example.com"},
{Schedule: "@daily", AltCommand: "/bin/true", LogFile: "/var/log/dokku/log.log"},
}
want := strings.Join([]string{
"PATH=/usr/local/bin:/usr/bin:/bin",
"SHELL=/bin/bash",
"",
"MAILTO=alpha@example.com",
"@hourly /bin/alpha 2>&1 | tee -a /var/log/dokku/alpha.log",
"MAILTO=dokku",
"@daily /bin/true &>> /var/log/dokku/log.log",
"",
}, "\n")
if got := renderCronTemplate(t, tasks, ""); got != want {
t.Errorf("rendered crontab =\n%s\nwant\n%s", got, want)
}
}
// TestCronTemplateWithoutTaskMailto pins that a crontab without tasks that set
// their own MAILTO renders as it did before per-task MAILTO values existed.
func TestCronTemplateWithoutMailtoGroups(t *testing.T) {
func TestCronTemplateWithoutTaskMailto(t *testing.T) {
tasks := []CronTask{
{Schedule: "@daily", AltCommand: "/bin/true", LogFile: "/var/log/dokku/log.log"},
}
tmpl, err := getCronTemplate()
if err != nil {
t.Fatalf("getCronTemplate() returned an error: %v", err)
}
var got bytes.Buffer
if err := tmpl.Execute(&got, cronTemplateData(tasks, "", "")); err != nil {
t.Fatalf("Execute() returned an error: %v", err)
}
got := renderCronTemplate(t, tasks, "")
want := strings.Join([]string{
"PATH=/usr/local/bin:/usr/bin:/bin",
@@ -218,7 +247,7 @@ func TestCronTemplateWithoutMailtoGroups(t *testing.T) {
"@daily /bin/true &>> /var/log/dokku/log.log",
"",
}, "\n")
if got.String() != want {
t.Errorf("rendered crontab =\n%s\nwant\n%s", got.String(), want)
if got != want {
t.Errorf("rendered crontab =\n%s\nwant\n%s", got, want)
}
}
+5 -5
View File
@@ -8,11 +8,11 @@ PATH=/usr/local/bin:/usr/bin:/bin
SHELL=/bin/bash
{{ range $task := .Tasks -}}
{{ if $task.Mailto -}}
MAILTO={{ $task.Mailto }}
{{ end -}}
{{ $task.Schedule }} {{ $task.DokkuRunCommand }}
{{ end -}}
{{ range $group := .MailtoGroups }}
MAILTO={{ $group.Mailto }}
{{ range $task := $group.Tasks -}}
{{ $task.Schedule }} {{ $task.DokkuRunCommand }}
{{ if $task.Mailto -}}
MAILTO={{ $.ResetMailto }}
{{ end -}}
{{ end -}}