Richathon
Richathon

Reputation: 101

Laravel scheduler by supervisord running multiple time

I'm trying to use Supervisord with Laravel scheduler. It's working BUT running multiple times, I don't know whether any configuration is wrong. But what i expected is just one time for each scheduler. If i have any experience about this situation please help me.

  1. This is supervisord conf file. sudo nano /etc/supervisord.conf
[program:laravel-scheduler]
client_loop: send disconnect: Broken pipeum)02d
command=php /var/www/project-api/artisan schedule:run --verbose --no-interaction
[Process completed]autostart=true
autorestart=true
numprocs=1
user=nginx
redirect_stderr=true
stdout_logfile=/var/www/project-api/storage/logs/schedule.log
stdout_logfile_maxbytes=10MB
logfile_backups=10
  1. This is laravel ConsoleKernel
protected function schedule(Schedule $schedule)
{
    $schedule->command('telescope:prune --hours=24')->daily();
    $schedule->command('horizon:snapshot')->everyFiveMinutes();
    $schedule->job(new HourlyJob(), QueueName::HOURLY_JOB)->hourly();
    $schedule->job(new MinuteJob(), QueueName::MINUTE_JOB)->everyTwoMinutes();
} 
  1. This is the schedule.log file

enter image description here

The scheduler is correctly running every two minutes, but the problem is Running scheduled command multiple. Please help me this issue. Thank you

Upvotes: 0

Views: 11486

Answers (2)

Raskul
Raskul

Reputation: 2199

For schedule you should go for cronjob, you can use crontab

but if you insist to use supervisor follow me:

you should use php artisan schedule:work not php artisan schedule:run because the run command will run it just one time.

my supervisor configuration for the schedule is working well so I want to share it here:

[program:laravel-worker-nordicstandard-schedule]
process_name=%(program_name)s_%(process_num)02d
directory=/home/nordicstandard.net/
command=php artisan schedule:work
autostart=true
autorestart=true
user=root
numprocs=1
redirect_stderr=true
stdout_logfile= /home/nordicstandard.net/storage/logs/worker1.log

remember to use directory=/home/nordicstandard.net/ to your project folder.

And that's it. it's working...

in sudo supervisorctl -> status you can see the output like:

laravel-worker-nordicstandard-schedule:laravel-worker-nordicstandard-schedule_00   RUNNING   pid 4084, uptime 0:06:55

and the last line is related to the schedule and the others are related to the queue.

note: you should be careful about numprocs=1 and it shouldn't be more than one.

note2: php artisan schedule:listen is like php artisan schedule:work but php artisan schedule:listen good for development, because it's listening to the latest changes in your code. but php artisan schedule:work will not consider your changes and uses the cache.

Upvotes: 10

whoasked
whoasked

Reputation: 420

Just ran into the same problem myself, and for me it turned out to be that supervisor was running cron in daemon mode (-b, also the default), and at the point it backgrounded supervisor thought it had exited so relaunched it. Supervisor repeated this four times before giving up, so we had four instances of cron running, and so four occurrences of each scheduled task. Running cron in foreground mode (-f) fixed it.

Upvotes: 0

Related Questions