user2099451
user2099451

Reputation: 647

How to setup Laravel 5's task scheduling in Heroku?

I'm trying to follow the Laravel Documentation on how to Run Cron Jobs, and I want to add this

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

But I don't know how to add it to Heroku.

Upvotes: 8

Views: 3342

Answers (5)

Daniel Mesa
Daniel Mesa

Reputation: 586

A free alternative can be to use the free worker dyno and configure it as follows in the Procfile

web: vendor/bin/heroku-php-apache2 public/
worker: bash -c "while [ true ]; do (php artisan schedule:run &); sleep 60; done"

This command creates a while loop for run schedule:run every minute.

Then you must exec heroku ps:scale web=1 worker=1 to enable worker dyno.

Upvotes: 0

fagiani
fagiani

Reputation: 2351

It's been a while since this question was asked but recently I've had a similar issue and was able to overcome it using this blog post:

https://dev.to/autoidle/run-laravel-scheduled-jobs-on-heroku-2ah6

You can basically create a console command using this artisan generator:

php artisan make:command SchedulerDaemon

then you can edit app/Console/Commands/SchedulerDaemon.php and edit some lines as described below:

...

class SchedulerDaemon extends Command
{
    ...
    // Change $signature value
    protected $signature = 'schedule:daemon {--sleep=60}';

    ...
    // Change $description value
    protected $description = 'Triggers scheduler every minute or --sleep seconds interval'; 

    ...

    public function handle()
    {
        // Change handle() function body to this:
        while (true) {
            $this->info('Calling scheduler');      
            $this->call('schedule:run');
            sleep($this->option('sleep'));
        }
    }

...
}

Then, add this line to Procfile:

scheduler: php artisan schedule:daemon

And remember to enable scheduler process on heroku dashboard or run:

heroku ps:scale scheduler=1

I hope it becomes helpful to others in the future!

Upvotes: 2

SNeumann
SNeumann

Reputation: 1177

Cron To Go is an add-on that allows you to run Laravel's task scheduler every minute (* * * * * in cron). Simply install the add-on, add a job with * * * * * as the schedule and php artisan schedule:run as the command, sit back and relax! If your Laravel logs don't show up for the scheduler, there's a quick fix for log routing described here.

Upvotes: 0

Fester
Fester

Reputation: 883

Heroku has a cron scheduler addon that you can use for scheduled tasks.

You can install it like this:

$ heroku addons:create scheduler:standard

Have a look at this article for more information.

Upvotes: 2

Robert Norman
Robert Norman

Reputation: 191

I created this Scheduler which runs once a minute and is part of your own app.

https://gist.github.com/robbydooo/65bf341ea0f4081150b945bfb1d38d3c

It creates a new Dyno type called Scheduler which you start one of.

Make sure you run jobs on your queue to avoid this scheduler over running once per minute.

To use this scheduler with Laravel 5.4+ add this file to /app/Console/Commands/RunScheduler.php Register this file in app/Console/Kernel.php

protected $commands = [
…
Commands\RunScheduler::class
…
]

Add this line to your Procfile:

scheduler: php -d memory_limit=512M artisan schedule:cron

Upvotes: 8

Related Questions