You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 68be049
Browse filesBrowse the repository at this point in the historyBrowse files
Document runtime cron jobs and deployment behavior (#232)
## 💸 TL;DR
<!-- What's the three sentence summary of purpose of the PR -->
Explains which schedules survive deployments and how to create, update,
or remove them.
Starting with Devvit 0.14.6, recurring jobs created in app code are preserved across deployments. Jobs configured with `cron` in `devvit.json` are rescheduled on installation and upgrade.
86
+
87
+
| How the job is created | What happens on deployment |
| A task with `cron` in `devvit.json`| Devvit replaces configuration-managed recurring jobs with the configured schedules. |
90
+
|`scheduler.runJob({ name, cron, data })`| The job keeps its stored schedule and data until your app cancels it. |
91
+
92
+
### Create a recurring job at runtime
93
+
94
+
Register the task in `devvit.json` without a `cron` value, and add its server handler as shown in [Scheduling recurring jobs](#scheduling-recurring-jobs):
95
+
96
+
```json title="devvit.json"
97
+
{
98
+
"scheduler": {
99
+
"tasks": {
100
+
"refresh-summary": {
101
+
"endpoint": "/internal/scheduler/refresh-summary"
102
+
}
103
+
}
104
+
}
105
+
}
106
+
```
107
+
108
+
Use `scheduler.runJob()` to choose the schedule in your app code. Check for an existing job before creating another:
`ScheduledActionManager.USER` selects jobs managed by your app code. It does not select a Reddit user or change the identity that runs the task. Calling `listJobs()` without a filter returns jobs from all managers.
132
+
133
+
Call setup from a controlled application action. The check above prevents duplicate jobs on sequential calls; coordinate concurrent setup calls in your app to avoid creating duplicates.
134
+
135
+
### Update or remove a runtime schedule
136
+
137
+
Changing the code that originally created a job does not change its stored schedule or data. Cancel the job with `scheduler.cancelJob(jobId)` and create a replacement with `runJob()`. There is no update-in-place method. See [Cancel a scheduled job](#cancel-a-scheduled-job) for a complete example.
138
+
139
+
Keep task registrations and handlers available for jobs that survive upgrades. Avoid recreating the same runtime jobs on every installation or upgrade without first checking for existing jobs.
140
+
141
+
:::note
142
+
In Devvit 0.14.8, removing the final `cron` task from `devvit.json` does not automatically cancel the previously configured jobs. Explicitly cancel obsolete jobs when making that change.
143
+
:::
144
+
145
+
---
146
+
83
147
## Scheduling one-off jobs at runtime
84
148
85
149
One-off tasks must also be declared in `devvit.json`.
Starting with Devvit 0.14.6, recurring jobs created in app code are preserved across deployments. Jobs configured with `cron` in `devvit.json` are rescheduled on installation and upgrade.
86
+
87
+
| How the job is created | What happens on deployment |
| A task with `cron` in `devvit.json`| Devvit replaces configuration-managed recurring jobs with the configured schedules. |
90
+
|`scheduler.runJob({ name, cron, data })`| The job keeps its stored schedule and data until your app cancels it. |
91
+
92
+
### Create a recurring job at runtime
93
+
94
+
Register the task in `devvit.json` without a `cron` value, and add its server handler as shown in [Scheduling recurring jobs](#scheduling-recurring-jobs):
95
+
96
+
```json title="devvit.json"
97
+
{
98
+
"scheduler": {
99
+
"tasks": {
100
+
"refresh-summary": {
101
+
"endpoint": "/internal/scheduler/refresh-summary"
102
+
}
103
+
}
104
+
}
105
+
}
106
+
```
107
+
108
+
Use `scheduler.runJob()` to choose the schedule in your app code. Check for an existing job before creating another:
`ScheduledActionManager.USER` selects jobs managed by your app code. It does not select a Reddit user or change the identity that runs the task. Calling `listJobs()` without a filter returns jobs from all managers.
132
+
133
+
Call setup from a controlled application action. The check above prevents duplicate jobs on sequential calls; coordinate concurrent setup calls in your app to avoid creating duplicates.
134
+
135
+
### Update or remove a runtime schedule
136
+
137
+
Changing the code that originally created a job does not change its stored schedule or data. Cancel the job with `scheduler.cancelJob(jobId)` and create a replacement with `runJob()`. There is no update-in-place method. See [Cancel a scheduled job](#cancel-a-scheduled-job) for a complete example.
138
+
139
+
Keep task registrations and handlers available for jobs that survive upgrades. Avoid recreating the same runtime jobs on every installation or upgrade without first checking for existing jobs.
140
+
141
+
:::note
142
+
In Devvit 0.14.8, removing the final `cron` task from `devvit.json` does not automatically cancel the previously configured jobs. Explicitly cancel obsolete jobs when making that change.
143
+
:::
144
+
145
+
---
146
+
83
147
## Scheduling one-off jobs at runtime
84
148
85
149
One-off tasks must also be declared in `devvit.json`.
0 commit comments