Repository navigation
Conversation
| ## Scheduled one time tasks | ||
|
|
||
| To run a scheduled task once in the future, you can use `runTask(name, { context: { waitFor: 10 }})` to run the task in 10 seconds. | ||
|
|
||
| **Example:** | ||
|
|
||
| ```ts [server/api/runOnce.ts] | ||
| export default eventHandler(async (event) => { | ||
| runTask("future", { payload, context: { waitFor: 10 } }); | ||
|
|
||
| return { result: "Scheduled to run in 10 seconds" }; | ||
| }); | ||
| ``` | ||
|
|
||
| > [!IMPORTANT] | ||
| > Scheduled one time tasks are only ever run on full seconds. | ||
| > If the actual time would be `YYYY-MM-DD HH:MM:10.250`, the task will be run on `YYYY-MM-DD HH:MM:11`. | ||
|
|
There was a problem hiding this comment.
Maybe I am dump, maybe this needs clarification :-)
Does it depend on some scheduled task configured in the Cloudflare? E.g. there needs to be configured scheduled task to run every minute and nitro will execute all one-time tasks scheduled to run since last execution to now.
Or of this depends on some external API it is good to link it.
There was a problem hiding this comment.
You are right, this isn't very clear.
I think there isn't really a way to have the current implementation work on any edge worker.
The implementation creates a cron job that will we executed after the given time. This works fine in dev, node-server, bun and deno-server as the instance is always running.
This however won't work in edge workers.
In edge workers it can't be assured that there is enough wall time to wait for a crown job to run.
I am not sure how to even tackle it with edge workers.
I will update the docs to specify that this only works in long running servers.
There was a problem hiding this comment.
You can do this by the new Cloudflere Workflow API: https://developers.cloudflare.com/workflows/build/sleeping-and-retrying/
There was a problem hiding this comment.
Maybe i am misunderstanding this time, so please confirm:
You mean we would deploy a workflow to invoke the task.
As an example: the main application being deployed as e.g. a worker and a workflow to handle (scheduled one-time) tasks?
| nextExecution.setSeconds(nextExecution.getSeconds() + context.waitFor!); | ||
|
|
||
| const p = new Promise<TaskResult<RT>>((resolve, reject) => { | ||
| const job = new Cron( |
There was a problem hiding this comment.
What would be even benefit of using corner if we are setting a timer in same running process?
There was a problem hiding this comment.
I thought having the same way of scheduling would be a good idea.
What would be better in your opinion?
There was a problem hiding this comment.
Ideally, we need to persist future tasks and use runtime timer to load them.
We also need this to be mapped to other providers.
There was a problem hiding this comment.
What are you referring to with runtime timer?
As to other providers, I would think for edge workers there would need to be a solution to let the provider schedule some kind of invokation to the task.
If you want I will remodel scheduling to have it persist and maybe have an internal handler for this to also cancel.
Any specifics regarding the implementation that would be necessary from your point of view?
|
Rebased this on latest main locally. A lot has moved since March: the dev task routes are now in Two real bugs:
On design, @pi0 asked for persisted tasks that map to each provider. The in-process croner timer is lost on restart and cannot work on edge, so this still needs rework. This comment was written by an AI assistant on behalf of the Nitro maintainers. Please double-check anything that looks wrong. |
🔗 Linked issue
❓ Type of change
📚 Description
📝 Checklist
Implements scheduled one time tasks for #1974
Croner only ever runs on full seconds, so the actual wait time may be slightly after the specified time.
I added a test that runs a scheduled task. Sadly, I didn't find a way to use Vitest's mock timers to artificially advance the Cron job.