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

5

8

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.

Rivulet answered 2/2, 2016 at 10:46 Comment(0)
S
8

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
Studbook answered 8/3, 2017 at 22:13 Comment(1)
i tried the same , but not reflecting the memory limit issue. Meantime web is working fine with ProcfileFranckot
D
2

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.

Dharana answered 3/2, 2016 at 18:11 Comment(0)
H
2

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!

Hardin answered 12/10, 2021 at 22:45 Comment(0)
F
0

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.

Fortress answered 22/3, 2021 at 20:46 Comment(0)
D
0

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.

Darton answered 27/2, 2022 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.