I have created an artisan command
that I want to run immediately after a method called. But the command contains a sleep();
command. I want to run that artisan command in background because the method need to return
immediately response to user. My sample code is a below:
In route file
Route::get('test', function(){
Artisan::queue('close:bidding', ['applicationId' => 1]);
return 'called close:bidding';
});
In close:bidding
command
public function handle()
{
$appId = $this->argument('applicationId');
//following code line is making the problem
sleep(1000 * 10);
//close bidding after certain close time
try{
Application::where('id', $appId)->update(['live_bidding_status' => 'closed']);
}catch (\PDOException $e){
$this->info($e->getMessage());//test purpose
}
$this->info($appId.": bid closed after 10 seconds of creation");
}
Problem
When I hit /test
url the return string called close:bidding
is being shown after 10 seconds browser loading, because there is a sleep(10 * 1000)
inside the command.
What I want
I want to run the command in background. I mean when I hit the /test
url it should immediately show up the called close:bidding
but close:bidding
command will be running in background. After 10 seconds it will update the application though the front-end user won't notice anything anything about it.
Partial Questions
Is it somehow related to
multi threading
?Is it something that cannot be solve using PHP, should I think differently?
Is there any way to solve this problem even using Laravel queue?
Create a job
To push job background queue
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
Note: May be you can try supervisord,beanstakd for manage queue
For more info refer this