adonisjs / rfcs

πŸ’¬ Sharing big changes with community to add them to the AdonisJs eco-system
52 stars 6 forks source link

Scheduled tasks #25

Closed Xstoudi closed 3 years ago

Xstoudi commented 4 years ago

Brief history

Scheduling tasks is probably as old as computers : cron is 45 years old. I don't think I need to write more about it.

What problem does it solve?

Today, the way I schedule tasks on Adonis is the following : I open an SSH command line and create a cron task that execute an ace command (node ace mytask in example).

The main problem is that I can't version control task scheduling as I need to edit them manually on the server.

Proposal

I'll propose two ways to implement it, don't hesitate to be sceptical about it and to provide other ideas.

Task are classes

We can easily imagine that the command node ace make:schedule Archive would create the following file in app/Tasks/ArchiveTask.ts:

export default class ArchiveTask extends BaseTask {
    public static schedule = '0 0 1 * *'

    public async run() {
        // Do what you want
    }
}

Then when Adonis boots, it begin to check which time it is and run the ArchiveTask's run method when it is 00:00 on every 1st day of the month.

Schedule variable is crontab format:

# β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ minute (0 - 59)
# β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ hour (0 - 23)
# β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ day of the month (1 - 31)
# β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ month (1 - 12)
# β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ day of the week (0 - 6) (Sunday to Saturday;
# β”‚ β”‚ β”‚ β”‚ β”‚                                   7 is also Sunday on some systems)
# β”‚ β”‚ β”‚ β”‚ β”‚
# β”‚ β”‚ β”‚ β”‚ β”‚
# * * * * * <command to execute>

Tasks are scheduled Commands

The other way to think about it is to imagine an optional field on BaseCommand that allow the user to pass crontab format. Again, Adonis then check what he needs to execute at the current time. Exemple:

export default class Archive extends BaseCommand {
  public static commandName = 'archive'
  public static description = 'Archive needed entries'
  public static schedule = '0 0 1 * *'   

  public static handle () {
    // ...
  }
}

Relevant links

CRON Scheduling in Laravel

Are you willing to work on it?

Yeah, with some little guidance.

thetutlage commented 4 years ago

@Xstoudi Thanks a lot for creating the RFC. Appreciate it. There are some key details missing right now. So let's work towards completing them.

There are a few more doubts, but I believe, we should first try to get answers to these

targos commented 4 years ago

Another point to consider: one may want to execute the scheduled task in a sub-process or in a worker because it may be CPU-intensive.

Xstoudi commented 4 years ago

Cron is a UNIX-specific program. I don't think we should rely on it. Scheduled tasks should be handled by Node.js via Adonis. About the consideration of multiples processes : the simplest way for us to implement it is 10 process = task ran 10 times. What do you think about it?

I agree with @targos and furthermore I'd say Task are always child processes.

thetutlage commented 4 years ago

Another quick question

the simplest way for us to implement it is 10 process = task ran 10 times

Yes, the simplest approach. But maybe not the expected approach. Infact, I was looking at the Laravel docs https://laravel.com/docs/7.x/scheduling#running-tasks-on-one-server and they indeed prevent a task from running onto multiple servers. I would expect that too from a well designed scheduler

Xstoudi commented 4 years ago

If we want a complete scheduler, we surely needs a multi-driver storage options. For status tracing as well as for 10 process = task ran 1 single time.

thetutlage commented 4 years ago

Yup. That's the plan. If we are planning to add a feature, then it should be mature enough to handle commonly known scenarios.

RodolfoSilva commented 4 years ago

We can use the node-cron package: https://github.com/kelektiv/node-cron

avivronen commented 3 years ago

Any updated regarding Scheduled tasks?