Skip to content

feat(tasks): add one-time tasks - #3237

Open
schplitt wants to merge 2 commits into
nitrojs:mainfrom
schplitt:feat/scheduled_tasks
Open

schplitt wants to merge 2 commits into
nitrojs:mainfrom
schplitt:feat/scheduled_tasks

Conversation

@schplitt

@schplitt schplitt commented Mar 20, 2025 •

Copy link
Copy Markdown
Contributor

🔗 Linked issue

❓ Type of change

  • 📖 Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

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.

@schplitt
schplitt requested a review from pi0 as a code owner March 20, 2025 19:17
@schplitt schplitt mentioned this pull request Mar 20, 2025
5 of 10 tasks
Comment thread docs/1.guide/10.tasks.md
Comment on lines +111 to +128
## 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`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do this by the new Cloudflere Workflow API: https://developers.cloudflare.com/workflows/build/sleeping-and-retrying/

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be even benefit of using corner if we are setting a timer in same running process?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought having the same way of scheduling would be a good idea.
What would be better in your opinion?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we need to persist future tasks and use runtime timer to load them.

We also need this to be mapped to other providers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@pi0x pi0x added the enhancement New feature or improvement (PRs; issues use the Feature type) label Sep 2, 2026
@pi0x pi0x changed the title feat(tasks): one-time tasks feat(tasks): add one-time tasks Sep 2, 2026
@pi0x pi0x added the v3 label Sep 2, 2026
@pi0x pi0x mentioned this pull request Sep 7, 2026
3 of 8 tasks
@pi0x

pi0x commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Rebased this on latest main locally. A lot has moved since March: the dev task routes are now in src/runtime/internal/routes/dev-tasks.ts (and already parse the body safely), fixtures live under test/fixture/server/, and the docs page is docs/1.docs/50.tasks.md.

Two real bugs:

  • The dedupe guard is inverted. if (!runImmediately && __runningTasks__[name]) means normal runTask calls are no longer deduplicated at all.
  • TaskContext is empty on purpose. Adding a named field turns on excess-property checks, so every preset that passes context: { waitUntil } stops compiling.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or improvement (PRs; issues use the Feature type)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants