Running laravel queue:work on a shared hosting

依然范特西╮ 提交于 2019-12-04 13:32:39

As you mentioned you are using shared hosting, You follow the below steps.

Step 1. you need to setup your queue driver as database

Step 2 you need to setup a cron job with with the following command php /path/to/application/artisan queue:work --queue=high,default.

You can give a try. I hope it will work.

One more solution (I solved same problem in this way). You can make script like this:

# getting running processes with name "queue:work"
QUEUE_STATUS=$(ps aux | grep "queue:work")

# check is queue:work started, if no, start it
if $( echo $QUEUE_STATUS | grep --quiet 'artisan queue:work')
then
    exit;
else
    php ~/public_html/artisan queue:work
fi

and run it in CRON. I run every 10 min.

I figured a hack to accomplish this

On file Illuminate\Queue\Worker.php my current laravel version (5.3) is on line 151; on function runNextJob($connectionName, $queue, WorkerOptions $options) add else as below

if ($job) {
    return $this->process(
        $connectionName, $job, $options
    );
 } else {
    $this->stop();
}

Now create cron job that will run the number of times you like with command php artisan queue:work the moment the queue is exhausted, it will exit (but should be frequent as the process exists)

UPDATE: Using task schedular with withoutOverlapping() prevents further calls of the command if its already running, so this is a better option considering the previous one is a change you have to make everytime you composer install or composer update

This is the solution that worked for me after searching for days.

flock -n /tmp/latavel_queues.lockfile /usr/bin/php /path/to/laravel/artisan queue:listen

See https://laracasts.com/discuss/channels/servers/database-queue-on-shared-hosting

the best way is to set followin command on your tasks of panel (I'm using plesk control panel, it makes me able to set task there)

php artisan queue:work --once

Note: in my shared host, I must set following values because of their server configuration:

  1. php: /opt/plesk/php/7.2/bin/php -q
  2. artisan: /var/www/vhosts/t4f.ir/httpdocs/artisan
  3. my command: then I should write the command

so, the result would be like this:

/opt/plesk/php/7.2/bin/php -q /var/www/vhosts/t4f.ir/httpdocs/artisan queue:work --once

there is another option for runtime which I set to Cron type with value of: * * * * * which means, every minute this code will be executed. as I used --once in end of my commad, once it execute the command and job has been finished, it will be terminated. regarding to concurrent execution, I'm not worried about beacuase it's handling in queueing system and it's responsibility of this system.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!