feat: add ability to set maintenance mode for all cron tasks running for an app

Closes #6953
This commit is contained in:
Jose Diaz-Gonzalez committed 2025-06-08 22:34:47 -04:00
1 parent cc395b54dd
commit c0eb12f572
6 files changed
+70 -28

No files matched your search

+17 -17
View File
@@ -53,39 +53,39 @@ When running scheduled cron tasks, there are a few items to be aware of:
- Scheduled cron tasks are supported on a per-scheduler basis, and are currently only implemented by the `docker-local` scheduler. - Scheduled cron tasks are supported on a per-scheduler basis, and are currently only implemented by the `docker-local` scheduler.
- Tasks for _all_ apps managed by the `docker-local` scheduler are written to a single crontab file owned by the `dokku` user. The `dokku` user's crontab should be considered reserved for this purpose. - Tasks for _all_ apps managed by the `docker-local` scheduler are written to a single crontab file owned by the `dokku` user. The `dokku` user's crontab should be considered reserved for this purpose.
#### Specifying a MAILFROM value
> [!IMPORTANT] ### Changing cron management settings
> New as of 0.35.14
Users can specify a value for `MAILFROM` via the global `mailfrom` cron property by using the `cron:set` command. The `cron` plugin provides a number of settings that can be used to managed deployments on a per-app basis. The following table outlines ones not covered elsewhere:
| Name | Description | Level | Global Default |
|-----------------------|----------------------------------------------------------------|-------------|----------------|
| `mailfrom` | Sets the `MAILFROM` variable in a cron file for cron reporting | Global-only | empty string |
| `maintenance` | Whether to have cron running for the app or not. | App-only | `false` |
| `mailto` | Sets the `MAILTO` variable in a cron file for cron reporting | Global-only | empty string |
All settings can be set via the `ps:set` command. Using `maintenance` as an example:
```shell ```shell
dokku cron:set --global mailfrom example@example.com dokku ps:set node-js-app maintenance true
``` ```
All output for individual cron runs will be sent from the specified email. The default value may be set by passing an empty value for the option in question:
Cron emails can be reset to use the system email by running the `cron:set` command for the global `mailfrom` property with no value.
```shell ```shell
dokku cron:set --global mailfrom dokku ps:set node-js-app maintenance
``` ```
#### Specifying a MAILTO value If a property can be set globally - such as `mailto`, use the `--global` flag. If not set for an app, the global value will apply if it exists.
By default, cron tasks complete and do not perform any reporting. Users can specify a value for `MAILTO` via the global `mailto` cron property by using the `cron:set` command.
```shell ```shell
dokku cron:set --global mailto example@example.com dokku ps:set --global maintenance true
``` ```
All output for individual cron runs will be sent to the specified email. The global default value may be set by passing an empty value for the option.
Cron emails can be disabled by running the `cron:set` command for the global `mailto` property with no value.
```shell ```shell
dokku cron:set --global mailto dokku ps:set --global maintenance
``` ```
#### Listing Cron tasks #### Listing Cron tasks
+1 -1
View File
@@ -1,5 +1,5 @@
SUBCOMMANDS = subcommands/list subcommands/report subcommands/run subcommands/set SUBCOMMANDS = subcommands/list subcommands/report subcommands/run subcommands/set
TRIGGERS = triggers/cron-get-property TRIGGERS = triggers/cron-get-property triggers/install triggers/post-app-clone-setup triggers/post-app-rename-setup triggers/post-delete triggers/scheduler-stop
BUILD = commands subcommands triggers BUILD = commands subcommands triggers
PLUGIN_NAME = cron PLUGIN_NAME = cron
+10 -4
View File
@@ -12,14 +12,16 @@ import (
var ( var (
// DefaultProperties is a map of all valid cron properties with corresponding default property values // DefaultProperties is a map of all valid cron properties with corresponding default property values
DefaultProperties = map[string]string{ DefaultProperties = map[string]string{
"mailfrom": "", "mailfrom": "",
"mailto": "", "mailto": "",
"maintenance": "false",
} }
// GlobalProperties is a map of all valid global cron properties // GlobalProperties is a map of all valid global cron properties
GlobalProperties = map[string]bool{ GlobalProperties = map[string]bool{
"mailfrom": true, "mailfrom": true,
"mailto": true, "mailto": true,
"maintenance": true,
} }
) )
@@ -59,6 +61,10 @@ func (t TemplateCommand) CronCommand() string {
// FetchCronEntries returns a list of cron commands for a given app // FetchCronEntries returns a list of cron commands for a given app
func FetchCronEntries(appName string) ([]TemplateCommand, error) { func FetchCronEntries(appName string) ([]TemplateCommand, error) {
commands := []TemplateCommand{} commands := []TemplateCommand{}
if reportComputedMaintenance(appName) == "true" {
return commands, nil
}
appJSON, err := appjson.GetAppJSON(appName) appJSON, err := appjson.GetAppJSON(appName)
if err != nil { if err != nil {
return commands, fmt.Errorf("Unable to fetch app.json for app %s: %s", appName, err.Error()) return commands, fmt.Errorf("Unable to fetch app.json for app %s: %s", appName, err.Error())
+23 -3
View File
@@ -13,9 +13,12 @@ func ReportSingleApp(appName string, format string, infoFlag string) error {
} }
flags := map[string]common.ReportFunc{ flags := map[string]common.ReportFunc{
"--cron-mailfrom": reportMailfrom, "--cron-mailfrom": reportMailfrom,
"--cron-mailto": reportMailto, "--cron-mailto": reportMailto,
"--cron-task-count": reportTasks, "--cron-task-count": reportTasks,
"--cron-global-maintenance": reportGlobalMaintenance,
"--cron-computed-maintenance": reportComputedMaintenance,
"--cron-maintenance": reportMaintenance,
} }
flagKeys := []string{} flagKeys := []string{}
@@ -41,3 +44,20 @@ func reportTasks(appName string) string {
c, _ := FetchCronEntries(appName) c, _ := FetchCronEntries(appName)
return strconv.Itoa(len(c)) return strconv.Itoa(len(c))
} }
func reportGlobalMaintenance(_ string) string {
return common.PropertyGet("cron", "--global", "maintenance")
}
func reportComputedMaintenance(appName string) string {
maintenance := common.PropertyGet("cron", appName, "maintenance")
if maintenance == "true" {
return "true"
}
return common.PropertyGetDefault("cron", "--global", "maintenance", DefaultProperties["maintenance"])
}
func reportMaintenance(appName string) string {
return common.PropertyGet("cron", appName, "maintenance")
}
+5
View File
@@ -43,6 +43,11 @@ func main() {
case "report": case "report":
appName := flag.Arg(0) appName := flag.Arg(0)
err = cron.ReportSingleApp(appName, "", "") err = cron.ReportSingleApp(appName, "", "")
case "scheduler-stop":
scheduler := flag.Arg(0)
appName := flag.Arg(1)
removeContainers := flag.Arg(2)
err = cron.TriggerSchedulerStop(scheduler, appName, removeContainers)
default: default:
err = fmt.Errorf("Invalid plugin trigger call: %s", trigger) err = fmt.Errorf("Invalid plugin trigger call: %s", trigger)
} }
+14 -3
View File
@@ -10,14 +10,16 @@ import (
// TriggerCronGetProperty writes the cron key to stdout for a given app container // TriggerCronGetProperty writes the cron key to stdout for a given app container
func TriggerCronGetProperty(appName string, key string) error { func TriggerCronGetProperty(appName string, key string) error {
validProperties := map[string]bool{ validProperties := map[string]bool{
"mailfrom": true, "mailfrom": true,
"mailto": true, "mailto": true,
"maintenance": true,
} }
if !validProperties[key] { if !validProperties[key] {
return errors.New("Invalid cron property specified") return errors.New("Invalid cron property specified")
} }
fmt.Println(common.PropertyGet("cron", appName, key)) value := common.PropertyGetDefault("cron", appName, key, DefaultProperties[key])
fmt.Println(value)
return nil return nil
} }
@@ -57,3 +59,12 @@ func TriggerPostAppRenameSetup(oldAppName string, newAppName string) error {
func TriggerPostDelete(appName string) error { func TriggerPostDelete(appName string) error {
return common.PropertyDestroy("cron", appName) return common.PropertyDestroy("cron", appName)
} }
// TriggerSchedulerStop stops the scheduler for a given app container
func TriggerSchedulerStop(scheduler string, appName string, removeContainers string) error {
if scheduler != "docker-local" {
return nil
}
return nil
}