1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shippinno\Job\Infrastructure\Ui\Console\Laravel\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use LogicException; |
9
|
|
|
use Psr\Log\LoggerAwareTrait; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
use Psr\Log\NullLogger; |
12
|
|
|
use Shippinno\Job\Application\Messaging\EnqueueStoredJobsService; |
13
|
|
|
use Shippinno\Job\Domain\Model\FailedToEnqueueStoredJobException; |
14
|
|
|
|
15
|
|
|
class JobEnqueue extends Command |
16
|
|
|
{ |
17
|
|
|
use LoggerAwareTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
protected $signature = 'job:enqueue'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var EnqueueStoredJobsService |
26
|
|
|
*/ |
27
|
|
|
private $service; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ManagerRegistry|null |
31
|
|
|
*/ |
32
|
|
|
private $managerRegistry; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param EnqueueStoredJobsService $service |
36
|
|
|
* @param ManagerRegistry|null $managerRegistry |
37
|
|
|
* @param LoggerInterface|null $logger |
38
|
|
|
*/ |
39
|
|
|
public function __construct( |
40
|
|
|
EnqueueStoredJobsService $service, |
41
|
|
|
ManagerRegistry $managerRegistry = null, |
42
|
|
|
LoggerInterface $logger = null |
43
|
|
|
) { |
44
|
|
|
parent::__construct(); |
45
|
|
|
$this->service = $service; |
46
|
|
|
$this->managerRegistry = $managerRegistry; |
47
|
|
|
$this->setLogger(null !== $logger ? $logger : new NullLogger); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function handle() |
51
|
|
|
{ |
52
|
|
|
$topic = env('JOB_ENQUEUE_TOPIC'); |
53
|
|
|
if (!$topic) { |
54
|
|
|
throw new LogicException('The env JOB_ENQUEUE_TOPIC is not defined'); |
55
|
|
|
} |
56
|
|
|
while (true) { |
57
|
|
|
if (null !== $this->managerRegistry) { |
58
|
|
|
$this->managerRegistry->getManager()->clear(); |
59
|
|
|
} |
60
|
|
|
try { |
61
|
|
|
$enqueuedMessagesCount = $this->service->execute($topic); |
62
|
|
|
if ($enqueuedMessagesCount > 0) { |
63
|
|
|
$this->logger->debug($enqueuedMessagesCount.' jobs enqueued.'); |
64
|
|
|
if (null !== $this->managerRegistry) { |
65
|
|
|
$this->entityManager->flush(); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} catch (FailedToEnqueueStoredJobException $e) { |
69
|
|
|
$this->logger->alert('Failed to enqueue stored job, retrying in 60 seconds.', [ |
70
|
|
|
'exception' => $e, |
71
|
|
|
]); |
72
|
|
|
sleep(60); |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: