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
Copy file name to clipboardExpand all lines: go/README.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,6 +22,7 @@
22
22
-**[Stateful Actors and State Machines](patterns-use-cases/README.md#stateful-actors-and-state-machines)**: Stateful Actor representing a machine in our factory. Track state transitions with automatic state persistence. [<imgsrc="https://raw.githubusercontent.com/restatedev/img/refs/heads/main/play-button.svg"width="16"height="16">](patterns-use-cases/src/statefulactors/machineoperator.go)
23
23
24
24
#### Scheduling
25
+
-**[Cron Jobs](patterns-use-cases/README.md#cron-jobs)**: Implement a cron service that executes tasks based on a cron expression. [<imgsrc="https://raw.githubusercontent.com/restatedev/img/refs/heads/main/play-button.svg"width="16"height="16">](patterns-use-cases/src/cron/cron.go)
25
26
-**[Scheduling Tasks](patterns-use-cases/README.md#scheduling-tasks)**: Restate as scheduler: Schedule tasks for later and ensure the task is triggered and executed. [<imgsrc="https://raw.githubusercontent.com/restatedev/img/refs/heads/main/play-button.svg"width="16"height="16">](patterns-use-cases/src/schedulingtasks/paymentreminders.go)
26
27
-**[Parallelizing Work](patterns-use-cases/README.md#parallelizing-work)**: Execute a list of tasks in parallel and then gather their result. [<imgsrc="https://raw.githubusercontent.com/restatedev/img/refs/heads/main/play-button.svg"width="16"height="16">](patterns-use-cases/src/parallelizework/fanoutworker.go)
Copy file name to clipboardExpand all lines: go/patterns-use-cases/README.md
+77Lines changed: 77 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,7 @@ Common tasks and patterns implemented with Restate:
13
13
-**[Stateful Actors and State Machines](README.md#stateful-actors-and-state-machines)**: Stateful Actor representing a machine in our factory. Track state transitions with automatic state persistence. [<imgsrc="https://raw.githubusercontent.com/restatedev/img/refs/heads/main/play-button.svg"width="16"height="16">](src/statefulactors/machineoperator.go)
14
14
15
15
#### Scheduling
16
+
-**[Cron Jobs](README.md#cron-jobs)**: Implement a cron service that executes tasks based on a cron expression. [<imgsrc="https://raw.githubusercontent.com/restatedev/img/refs/heads/main/play-button.svg"width="16"height="16">](src/cron/cron.go)
16
17
-**[Scheduling Tasks](README.md#scheduling-tasks)**: Restate as scheduler: Schedule tasks for later and ensure the task is triggered and executed. [<imgsrc="https://raw.githubusercontent.com/restatedev/img/refs/heads/main/play-button.svg"width="16"height="16">](src/schedulingtasks/paymentreminders.go)
17
18
-**[Parallelizing Work](README.md#parallelizing-work)**: Execute a list of tasks in parallel and then gather their result. [<imgsrc="https://raw.githubusercontent.com/restatedev/img/refs/heads/main/play-button.svg"width="16"height="16">](src/parallelizework/fanoutworker.go)
18
19
@@ -189,6 +190,82 @@ Have a look at the logs to see the cancellations of the flight and car booking i
Restate has no built-in functionality for cron jobs.
199
+
But Restate's durable building blocks make it easy to implement a service that does this for us.
200
+
And uses the guarantees Restate gives to make sure tasks get executed reliably.
201
+
202
+
We use the following Restate features to implement the cron service:
203
+
-**Durable timers**: Restate allows the schedule tasks to run at a specific time in the future. Restate ensures execution.
204
+
-**Task control**: Restate allows starting and cancelling tasks.
205
+
-**K/V state**: We store the details of the cron jobs in Restate, so we can retrieve them later.
206
+
207
+
The cron service schedules tasks based on a cron expression, lets you cancel jobs and retrieve information about them.
208
+
209
+
For example, we create two cron jobs. One executes every minute, and the other one executes at midnight.
210
+
We then see the following in the UI:
211
+
<imgsrc="img/cron_service_schedule.png"width="1200px"alt="Cron Service UI">
212
+
213
+
<imgsrc="img/cron_state_ui.png"width="1200px"alt="Cron Job State UI">
214
+
215
+
Note that this implementation is fully resilient, but you might need to make some adjustments to make this fit your use case:
216
+
- Take into account time zones.
217
+
- Adjust how you want to handle tasks that fail until the next task gets scheduled. Right now, you would have concurrent executions of the same cron job (one retrying and the other starting up).
218
+
- ...
219
+
220
+
<details>
221
+
<summary><strong>Running the example</strong></summary>
222
+
223
+
1.[Start the Restate Server](https://docs.restate.dev/develop/local_dev) in a separate shell: `restate-server`
224
+
2. Start the cron service and the task service:
225
+
```shell
226
+
go run ./src/cron
227
+
```
228
+
3. Register the services (with `--force` to override the endpoint during **development**): `restate -y deployments register --force localhost:9080`
229
+
230
+
Send a request to create a cron job that runs every minute:
0 commit comments