1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Lamoda\QueueBundle\Service; |
6
|
|
|
|
7
|
|
|
use Lamoda\QueueBundle\ConstantMessage; |
8
|
|
|
use Lamoda\QueueBundle\Factory\PublisherFactory; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use Throwable; |
11
|
|
|
|
12
|
|
|
class QueueRepublishService |
13
|
|
|
{ |
14
|
|
|
/** @var PublisherFactory */ |
15
|
|
|
protected $publisherFactory; |
16
|
|
|
|
17
|
|
|
/** @var QueueService */ |
18
|
|
|
protected $queueService; |
19
|
|
|
|
20
|
|
|
/** @var LoggerInterface */ |
21
|
|
|
protected $logger; |
22
|
|
|
|
23
|
4 |
|
public function __construct( |
24
|
|
|
PublisherFactory $publisherFactory, |
25
|
|
|
QueueService $queueService, |
26
|
|
|
LoggerInterface $logger |
27
|
|
|
) { |
28
|
4 |
|
$this->publisherFactory = $publisherFactory; |
29
|
4 |
|
$this->queueService = $queueService; |
30
|
4 |
|
$this->logger = $logger; |
31
|
4 |
|
} |
32
|
|
|
|
33
|
4 |
|
public function republishQueues(int $batchSize): bool |
34
|
|
|
{ |
35
|
4 |
|
$republishedQueueIds = []; |
36
|
|
|
do { |
37
|
4 |
|
$this->queueService->beginTransaction(); |
38
|
|
|
|
39
|
|
|
try { |
40
|
4 |
|
$queues = $this->queueService->getToRepublish($batchSize); |
41
|
3 |
|
if ($queues) { |
|
|
|
|
42
|
3 |
|
foreach ($queues as $queue) { |
43
|
3 |
|
$this->publisherFactory->republish($queue); |
44
|
3 |
|
$republishedQueueIds[] = $queue->getId(); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
$this->queueService->flush(); |
49
|
3 |
|
$this->queueService->commit(); |
50
|
3 |
|
$this->publisherFactory->releaseAll(); |
51
|
2 |
|
} catch (Throwable $exception) { |
52
|
2 |
|
if ($this->queueService->isTransactionActive()) { |
53
|
1 |
|
$this->queueService->rollback(); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
$this->logger->error( |
57
|
2 |
|
ConstantMessage::QUEUE_CAN_NOT_REPUBLISH, |
58
|
|
|
[ |
59
|
2 |
|
'exception' => get_class($exception), |
60
|
2 |
|
'message' => $exception->getMessage(), |
61
|
|
|
] |
62
|
|
|
); |
63
|
|
|
|
64
|
2 |
|
return false; |
65
|
|
|
} |
66
|
2 |
|
} while (count($queues) === $batchSize); |
67
|
|
|
|
68
|
2 |
|
if ($republishedQueueIds) { |
|
|
|
|
69
|
2 |
|
$this->logger->info( |
70
|
2 |
|
ConstantMessage::QUEUE_SUCCESS_REPUBLISH, |
71
|
2 |
|
['queuesIds' => implode(', ', $republishedQueueIds)] |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
return true; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.