The ByteLab logo

How to run scheduled jobs more than once a minute with Laravel Vapor

Posted on 12 February 2022

Picture the scenario: your Laravel app is running serverless with Laravel Vapor and you want to execute a scheduled task more than once a minute at a regular interval. You may be thinking "but how do I accomplish this?"

Unfortunately, this is not possible with the package spatie/laravel-short-schedule due to the fact that it requires a worker to run in the background - something you cannot do with Vapor unless you want to say goodbye to your bank account.

The simple solution is to use Laravel's delay method, this will allow you to dispatch multiple instances of the same job every minute and stagger the execution, so it executes at regular intervals.

Want a quick example?

function handler(event) {
    MyJob::dispatch();
    MyJob::dispatch()->delay(now()->addSeconds(30));
}

This code snippet will dispatch two jobs, the first job will execute almost immediately and the second job will execute 30 seconds later. If you were to run this in a schedule once a minute, you would be running a job twice a minute at evenly-spaced intervals!

This is how to achieve super simple sub-minute job intervals with Laravel Vapor.

Further reading: