|
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 LogicException; |
|
8
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use Psr\Log\NullLogger; |
|
11
|
|
|
use Shippinno\Job\Application\Messaging\ConsumeStoredJobService; |
|
12
|
|
|
use Shippinno\Job\Infrastructure\Persistence\Doctrine\ManagerRegistryAwareTrait; |
|
13
|
|
|
|
|
14
|
|
|
class JobConsume extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
use ManagerRegistryAwareTrait; |
|
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
|
|
|
* @param ConsumeStoredJobService $consumeStoredJobService |
|
31
|
|
|
* @param ManagerRegistry|null $managerRegistry |
|
32
|
|
|
* @param LoggerInterface|null $logger |
|
33
|
|
|
*/ |
|
34
|
1 |
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
35
|
|
|
ConsumeStoredJobService $consumeStoredJobService, |
|
36
|
|
|
ManagerRegistry $managerRegistry = null, |
|
37
|
|
|
LoggerInterface $logger = null |
|
38
|
|
|
) { |
|
39
|
1 |
|
parent::__construct(); |
|
40
|
1 |
|
$this->consumeStoredJobService = $consumeStoredJobService; |
|
41
|
1 |
|
$this->setManagerRegistry($managerRegistry); |
|
42
|
1 |
|
$this->setLogger(null !== $logger ? $logger : new NullLogger); |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function handle() |
|
46
|
|
|
{ |
|
47
|
1 |
|
$queueName = $this->queueName(); |
|
48
|
|
|
$forever = !env('JOB_TESTING', false); |
|
49
|
|
|
do { |
|
50
|
|
|
$this->clear(); |
|
51
|
|
|
$this->consumeStoredJobService->execute($queueName); |
|
52
|
|
|
$this->flush(); |
|
53
|
|
|
} while ($forever); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
1 |
|
private function queueName(): string |
|
60
|
|
|
{ |
|
61
|
1 |
|
$queueName = env('JOB_CONSUME_QUEUE'); |
|
62
|
1 |
|
if (!$queueName) { |
|
63
|
1 |
|
throw new LogicException('The env JOB_CONSUME_QUEUE is not defined'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $queueName; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
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.