|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Shippinno\Job\Infrastructure\Ui\Console\Laravel\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
use Interop\Queue\PsrContext; |
|
8
|
|
|
use LogicException; |
|
9
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
10
|
|
|
use Psr\Log\LoggerInterface; |
|
11
|
|
|
use Psr\Log\NullLogger; |
|
12
|
|
|
use Shippinno\Job\Application\Messaging\ConsumeStoredJobService; |
|
13
|
|
|
use Shippinno\Job\Domain\Model\JobRunnerNotRegisteredException; |
|
14
|
|
|
|
|
15
|
|
|
class JobConsume extends Command |
|
16
|
|
|
{ |
|
17
|
|
|
use LoggerAwareTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritdoc} |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $signature = 'job:consume'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var ConsumeStoredJobService |
|
26
|
|
|
*/ |
|
27
|
|
|
private $consumeStoredJobService; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var ManagerRegistry|null |
|
31
|
|
|
*/ |
|
32
|
|
|
private $managerRegistry; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param ConsumeStoredJobService $consumeStoredJobService |
|
36
|
|
|
* @param ManagerRegistry|null $managerRegistry |
|
37
|
|
|
* @param LoggerInterface|null $logger |
|
38
|
|
|
*/ |
|
39
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
40
|
|
|
ConsumeStoredJobService $consumeStoredJobService, |
|
41
|
|
|
ManagerRegistry $managerRegistry = null, |
|
42
|
|
|
LoggerInterface $logger = null |
|
43
|
|
|
) { |
|
44
|
|
|
parent::__construct(); |
|
45
|
|
|
$this->consumeStoredJobService = $consumeStoredJobService; |
|
46
|
|
|
$this->managerRegistry = $managerRegistry; |
|
47
|
|
|
$this->setLogger(null !== $logger ? $logger : new NullLogger); |
|
48
|
3 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function handle() |
|
51
|
|
|
{ |
|
52
|
|
|
$queueName = env('JOB_CONSUME_QUEUE'); |
|
53
|
|
|
if (!$queueName) { |
|
54
|
3 |
|
throw new LogicException('The env JOB_CONSUME_QUEUE is not defined'); |
|
55
|
3 |
|
} |
|
56
|
3 |
|
$forever = !env('JOB_TESTING', false); |
|
57
|
3 |
|
do { |
|
58
|
3 |
|
if (null !== $this->managerRegistry) { |
|
59
|
3 |
|
$this->managerRegistry->getManager()->clear(); |
|
60
|
|
|
} |
|
61
|
|
|
$this->consumeStoredJobService->execute($queueName); |
|
62
|
|
|
if (null !== $this->managerRegistry) { |
|
63
|
|
|
$this->managerRegistry->getManager()->flush(); |
|
64
|
|
|
} |
|
65
|
|
|
} while ($forever); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.