How to sleep PHP(Laravel 5.2) in background

家住魔仙堡 提交于 2019-12-03 20:35:24

Create a job

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class JobTest implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    private $payload = [];
    public function __construct($payload)
    {
        $this->payload = $payload;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $appId = $this->payload('applicationId');
        //following code line is making the problem
        sleep(1000 * 10);
    }
}

To push job background queue

Route::get('test', function(){
    dispatch((new JobTest)->onQueue('queue_name'));
    return 'called close:bidding';
});

At this state we have job and you dispath the job to the queue. But it not processed yet. We need queue listener or worker to process those job in background

php artisan queue:listen --queue=queue_name --timeout=0  
OR
php artisan queue:work --queue=queue_name --timeout=0 //this will run forever

Note: May be you can try supervisord,beanstakd for manage queue

For more info refer this

If you don't need all advantages of proper queue, which come at a price, it may be sufficient to use terminate middleware. It will do the job after response was sent to the browser.

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