Skip to content

Commit baa3698

Browse files
authored
Hotfix | Add support laravel 8 (#10)
* Fix for Laravel 8, because "stop()" does not exit the worker anymore. * CS refactor and better reason explanation
1 parent 73409d1 commit baa3698

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/Worker.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Symfony\Component\Debug\Exception\FatalThrowableError;
1414
use Throwable;
1515

16-
/*final*/ class Worker extends IlluminateWorker
16+
class Worker extends IlluminateWorker
1717
{
1818
/**
1919
* @var EntityManagerInterface
@@ -42,33 +42,39 @@ public function __construct(
4242
}
4343

4444
/**
45-
* Wrap parent::runJob to make sure we have a good EM.
45+
* Wrap parent::getNextJob to make sure we have a good EM before processing the next job.
46+
* This allow us to avoid incrementing the attempts on the job if the worker fails because of the EM.
4647
*
47-
* Most exception handling is done in the parent method, so we consider any new
48-
* exceptions to be a result of our setup.
48+
* Get the next job from the queue connection.
4949
*
50-
* @param \Illuminate\Contracts\Queue\Job $job
51-
* @param string $connectionName
52-
* @param WorkerOptions $options
50+
* @param \Illuminate\Contracts\Queue\Queue $connection
51+
* @param string $queue
52+
* @return \Illuminate\Contracts\Queue\Job|null
5353
*/
54-
protected function runJob($job, $connectionName, WorkerOptions $options)
54+
protected function getNextJob($connection, $queue)
5555
{
56+
$exception = null;
57+
5658
try {
5759
$this->assertEntityManagerOpen();
5860
$this->assertEntityManagerClear();
5961
$this->assertGoodDatabaseConnection();
60-
61-
parent::runJob($job, $connectionName, $options);
6262
} catch (EntityManagerClosedException $e) {
63-
$this->exceptions->report($e);
64-
$this->stop(1);
63+
$exception = $e;
6564
} catch (Exception $e) {
66-
$this->exceptions->report(new QueueSetupException("Error in queue setup while running a job", 0, $e));
67-
$this->stop(1);
65+
$exception = new QueueSetupException("Error in queue setup while getting next job", 0, $e);
6866
} catch (Throwable $e) {
69-
$this->exceptions->report(new QueueSetupException("Error in queue setup while running a job", 0, new FatalThrowableError($e)));
70-
$this->stop(1);
67+
$exception = new QueueSetupException("Error in queue setup while getting next job", 0, new FatalThrowableError($e));
68+
}
69+
70+
if ($exception) {
71+
$this->shouldQuit = true;
72+
$this->exceptions->report($exception);
73+
74+
return null;
7175
}
76+
77+
return parent::getNextJob($connection, $queue);
7278
}
7379

7480
/**

0 commit comments

Comments
 (0)