loco-rs / loco

🚂 🦀 The one-person framework for Rust for side-projects and startups
https://loco.rs
Apache License 2.0
3.64k stars 153 forks source link

Cron/Scheduler support? #528

Open exzachlyvv opened 3 months ago

exzachlyvv commented 3 months ago

First off, THANK YOU for loco.rs :)

I've been looking for a Rails/Laravel equivalent in Rust for a while.

Is there a plan to add a cron/scheduler system?

Laravels API is incredible and could be inspired.

...some scheduler somewhere...

scheduler.every_minute(MyTaskToRunEveryMinute);
scheduler.hourly(MyTaskToRunEveryHour);
...
kaplanelad commented 3 months ago

Hey @exzachlyvv We are not supporting Cron or the scheduler. you can trigger the a task with your preferred operation

jondot commented 3 months ago

@exzachlyvv with rails, historically, there were 3 ways to run scheduled jobs:

  1. always-on daemon, that keeps a schedule as well. usually there was special care for expressing a schedule in plain english.
    schedule "every 2 weeks" do 
    # some code
    end
  2. Creating a watchdog / task that "wakes up", checks if there are tasks to run, and runs all pending tasks per schedule. Usually this "watchdog" task was run every 5 minutes with Cron. This also means maintaining state ("what are the tasks that should run next?") persisted somewhere
  3. Having a queue with delayed job, where the delayed job worker is run periodically like (2) every constant time window.

Which technique would you be referring to?

exzachlyvv commented 3 months ago

Any of these would work :)

My use case is simply that I need to run some code every X minutes. There are no requirements for a specific API.

Is this functionally Loco is interested in supporting (happy to help with it)? If yes, we can continue to discuss the ideal design for loco.

Simple idea: expose some endpoint /run-my-task and have an external service ping it every X minutes.

But ideally Loco can solve this problem :)

ShallmentMo commented 3 months ago

Loco depends on sidekiq-rs which support Periodic Jobs. Maybe loco can expose the worker process in hooks(something like after_boot?) to support this?

jondot commented 3 months ago

@ShallmentMo that's a great idea. I'm wondering how to integrate that seamlessly. On the face of it it looks like a "forever-running" await and so it needs a dedicated thread or process. So, if we create a Loco task and tell the user: "run this task in any way you like, it will hang and run the periodic jobs" it might work. On the other hand it will be 1 more extra process. I'd welcome more ideas to satisfy: minimum user friction, and minimum resources neeed

ShallmentMo commented 3 months ago

I don't know wether it might help or not. But I use sidekiq-cron to support cron job when using Rails and Sidekiq