nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Nitro Tasks API

Open pi0 opened this issue 7 months ago β€’ 50 comments

Nitro tasks allow on-off operations in runtime.

Docs: https://nitro.unjs.io/guide/tasks

### Tasks
- [x] Initial Implementation (#1929)
- [x] Write documentation section (PR welcome!)
- [x] Stabilize API / return values
- [x] Integrate with Cron Tasks / Background Jobs
- [ ] Ability to run tasks using CLI against production build
- [ ] Task hooks (on dev start, production start and production build)
- [ ] Ability to schedule one time run of a task for the future
- [ ] Allow optionally allow running multiple instances of task
### Tasks

pi0 avatar Nov 30 '23 16:11 pi0

@imcm7 check out task list it is on the roadmap :) Nitro tasks API will have multiple use-cases and invocation possibilities.

pi0 avatar Dec 01 '23 10:12 pi0

@imcm7 Would you mind to move your question to a discussion or feel free to DM me in discord (pi0) i would be happy to answer πŸ™πŸΌ

But in short, we are introducing tasks API which as you could guess for the next step of supporting standard DB layer in Nitro and allow running migrations within the runtime without this issues as most of ORMs suffer from it.

pi0 avatar Dec 01 '23 11:12 pi0

Tasks should probably be async, currently the run method expects synchronous code and is not awaited.

Aareksio avatar Jan 07 '24 17:01 Aareksio

@Aareksio check https://github.com/unjs/nitro/issues/2021 ☺️

manniL avatar Jan 07 '24 18:01 manniL

If I use a nitro task in, let's say, Netlify will it leverage netlify background functions?

Rigo-m avatar Jan 15 '24 17:01 Rigo-m

I'm also interested on this. I would like to have for example my normal code on vercel-edge but run tasks on vercel lambda (5min timeout vs 25 seconds)

cosbgn avatar Jan 16 '24 09:01 cosbgn

How to disable it, because i use dir tasks for other stuff?

imcm7 avatar Jan 18 '24 17:01 imcm7

It is planned to support running these tasks from cli without running the server?

brendonmatos avatar Feb 22 '24 16:02 brendonmatos

Nitro already has a CLI preset too! However it is tricky to tell now when we can do support it this way now.

pi0 avatar Feb 22 '24 18:02 pi0

Any plan for support seconds scheduling?

robin-dongbin avatar Feb 27 '24 02:02 robin-dongbin

@robin-dongbin You mean this https://nitro.unjs.io/guide/tasks#scheduled-tasks or something different? (like a pattern for <1 minute schedule?)

pi0 avatar Feb 27 '24 08:02 pi0

@robin-dongbin You mean(意味着) this nitro.unjs.io/guide/tasks#scheduled-tasks or something different? (like a pattern(樑式) for <1 minute(εˆ†ι’Ÿ) schedule?)

Yes, that's what I want. Is it possible? I know that cron do not support that, but Laravel has support this last year.

robin-dongbin avatar Feb 27 '24 08:02 robin-dongbin

@pi0 ... I have just tested the experimental scheduled task feature. It works really well! Thanks!

Just one question: would it be possible to have a config setting to execute a scheduled task immediately after the cron is scheduled? Example: a task running every 10 minutes: executes once the server starts, and then every 10 minutes after that? I saw in the croner docs that there is a trigger method, but not sure how it would fit into the nitro tasks?

go4cas avatar Feb 27 '24 09:02 go4cas

@go4cas Maybe use a Nitro plugin to triger them on startup? (also init: / startup: tasks are planned to be supported to have similar effect in the future). Also you can execute a task from another.

@robin-dongbin I also wonder if you need to trigger something this frequently, maybe a setInterval inside nitro plugins to run task with runTask might be simpler method with giving full control on executation?

pi0 avatar Feb 27 '24 11:02 pi0

@go4cas Maybe use a Nitro plugin to triger them on startup? (also init: / startup: tasks are planned to be supported to have similar effect in the future). Also you can execute a task from another.

@robin-dongbin I also wonder if you need to trigger something this frequently, maybe a setInterval inside nitro plugins to run task with runTask might be simpler method with giving full control on executation?

But won’t there be conflicts if a node in a cluster with 8 processes and all 8 processes will cause a task, but this behavior should not happen? It turns out you need to find someone who has a free worker and give him a task from the master?

JCtapuk avatar Feb 27 '24 18:02 JCtapuk

For time being, scheduled tasks are not supported in cluster mode the exactly meet this requirement (share a safe lock among them) and it is doable.

Regardless inside your interval you can call runTask() in single node, it currently guarantees only it is running once and when we support cluster, it would be safe too.

pi0 avatar Feb 27 '24 18:02 pi0

I apologize if this isn't the correct forum. But how is supposed to run those tasks in Nuxt project from CLI? Nuxt CLI will be implemented to invoke those tasks from the URL or we should run directly from nitro cli?

brendonmatos avatar Feb 29 '24 13:02 brendonmatos

Yes.

pi0 avatar Feb 29 '24 14:02 pi0

With async tasks, and enforcing task payloads to be serializable early on it will be possible in the future to dispatch tasks to queues. Shamelessly taking the concept from Laravel: https://laravel.com/docs/10.x/queues

The nice thing about that way of working is that when queues are configurable (e.g. redis, some AWS service, etc.) then the dispatch mechanic becomes really usefull for dispatching work to different parts of your architecture.

passionate-bram avatar Mar 05 '24 08:03 passionate-bram

payload should be serializable indeed in preparation for future ideas like queues. and it is by design separated from context that is non seriazable for other stuff. PR welcome to clarify in the docs.

pi0 avatar Mar 05 '24 11:03 pi0

@pi0 This tasks API looks fantastic thank you so much for adding this to Nitro.

Anyone who is trying to get early access to this feature and using a workspace with multiple nuxt projects may run into issues trying to get nuxt to pickup the correct nitro-imports. In my case I managed to force it to work with an overrides in the project package.json:

  "pnpm": {
    "overrides": {
      "nitropack": "npm:nitropack-nightly@latest"
    }
  }

Thought I would just post this trips anyone else up πŸ˜„

GerryWilko avatar Mar 06 '24 12:03 GerryWilko

Nuxt issue

My dev server (yarn dev) is running but when i do yarn run nitro task list

then it results in Missing info file: /home/timmy/projects/private/rewrite/nuxt-project/.nitro/nitro.json (is dev server running?)

what bothers me is that it's looking in .nitro folder whilst nuxt generates a .nuxt/nitro.json. So it seems to me that it is looking in the wrong folder.

Or do i need to run yarn run nitro dev for the dev server?

does anyone have a fix?

update

i fixed it by symlinking .nuxt to .nitro now i can run my nuxt server and run commands in another terminal. The only thing that i need to figure out is how to get cloudflare d1 working in the tasks context

TMBL-DEV avatar Mar 19 '24 13:03 TMBL-DEV

@robin-dongbin You mean(意味着) this nitro.unjs.io/guide/tasks#scheduled-tasks or something different? (like a pattern(樑式) for <1 minute(εˆ†ι’Ÿ) schedule?)

Yes, that's what I want. Is it possible? I know that cron do not support that, but Laravel has support this last year.

@robin-dongbin Yes, it is supported. You can use cron expression like */15 * * * * * (six *, different from five * of classic cron), the first * means seconds.

ProgramRipper avatar Mar 26 '24 10:03 ProgramRipper

@robin-dongbin You mean(意味着) this nitro.unjs.io/guide/tasks#scheduled-tasks or something different? (like a pattern(樑式) for <1 minute(εˆ†ι’Ÿ) schedule?)

Yes, that's what I want. Is it possible? I know that cron do not support that, but Laravel has support this last year.

@robin-dongbin Yes, it is supported. You can use cron expression like */15 * * * * * (six *, different from five * of classic cron), the first * means seconds.

That's great, thank you!

robin-dongbin avatar Mar 27 '24 00:03 robin-dongbin

Hi, do you think in the future this feature could allow us to define scheduled function on gcp as example ?

flapili avatar Mar 27 '24 15:03 flapili

@flapili Sure πŸ‘πŸΌ if you can perhaps share an example project and some links to the docs i can help to do it faster :) (we might need a new preset that auto configured for gcp)

pi0 avatar Mar 27 '24 15:03 pi0

@flapili Sure πŸ‘πŸΌ if you can perhaps share an example project and some links to the docs i can help to do it faster :) (we might need a new preset that auto configured for gcp)

the usecase we encounter at work is about run every day a routine to check if point of sales are closed, if not send an email, after 6 day force close the POS (constraint from France law)

here an example from google directly : https://github.com/firebase/functions-samples/blob/a8b1ab89c503419fab90b0a44b61adee8ec9923b/Node/delete-unused-accounts-cron/functions/index.js#L37

flapili avatar Mar 27 '24 15:03 flapili

I see so we can probably integrate it firebase preset.

pi0 avatar Mar 27 '24 15:03 pi0

Really cool feature! Works like a charm in VPS environments for scheduledTasks. I have 2 questions:

If for example, the nitro app is running in a cluster (e.g. PM2). I am assuming the scheduled tasks will trigger in each instance on the cron schedule? What would you suggest here?

Will scheduled tasks hook into platform API's for jobs, e.g. Firebase has scheduled functions and Vercel has Cron Jobs? Both which accept the cron expressions.

Eckhardt-D avatar Mar 28 '24 07:03 Eckhardt-D

@Eckhardt-D pi0 wrote about cluster mode in this comment: https://github.com/unjs/nitro/issues/1974#issuecomment-1967387326

Niki2k1 avatar Mar 28 '24 13:03 Niki2k1