I've coded Laravel from 6 years ago and now I have to deal with a situation where the user can set the queue timeout because there is long-running process that needs to fail according to the user's timeout.
class MyQueue implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $userTimeout;
public function __construct($userTimeout)
{
$this->userTimeout = $userTimeout;
}
public function handle()
{
MyService::compute(); // very heavy process (hypotethically)
// the queue needs to fail if the above computation exceeds the $userTimeout.
// this following code won't run logically before the above code finishes
if ($userTimeoutIsReached) {
$this->fail();
}
}
}
I need to be able to do this from Controller
MyQueue::dispatch(request()->userTimeout);
Need some thoughts from you guys to get the bottom of this. I'm stuck. Please help
CodePudding user response:
Laravel already has timeout property on their job class.
just change userTimeout
to timeout
as their documentation
And also if you want to mark as fail after time out just add failOnTimeout
property also.
So the final code should be like:
class MyQueue implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $timeout = 120; //default timeout
public $failOnTimeout = true;
public function __construct($userTimeout)
{
$this->timeout = $userTimeout;
}
public function handle()
{
MyService::compute(); // very heavy process (hypotethically)
}
}