|
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
|
3 |
|
public function __construct( |
|
24
|
|
|
PublisherFactory $publisherFactory, |
|
25
|
|
|
QueueService $queueService, |
|
26
|
|
|
LoggerInterface $logger |
|
27
|
|
|
) { |
|
28
|
3 |
|
$this->publisherFactory = $publisherFactory; |
|
29
|
3 |
|
$this->queueService = $queueService; |
|
30
|
3 |
|
$this->logger = $logger; |
|
31
|
3 |
|
} |
|
32
|
|
|
|
|
33
|
3 |
|
public function republishQueues(int $batchSize): bool |
|
34
|
|
|
{ |
|
35
|
3 |
|
$i = 0; |
|
36
|
|
|
|
|
37
|
3 |
|
while (true) { |
|
38
|
3 |
|
$this->queueService->beginTransaction(); |
|
39
|
|
|
|
|
40
|
|
|
try { |
|
41
|
3 |
|
$queues = $this->queueService->getToRepublish($batchSize); |
|
42
|
|
|
|
|
43
|
2 |
|
if (0 !== $i && count($queues) < $batchSize) { |
|
44
|
1 |
|
$this->queueService->commit(); |
|
45
|
|
|
|
|
46
|
1 |
|
return true; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
$republishedQueueIds = []; |
|
50
|
|
|
|
|
51
|
2 |
|
foreach ($queues as $queue) { |
|
52
|
2 |
|
$this->publisherFactory->republish($queue); |
|
53
|
2 |
|
$this->queueService->flush($queue); |
|
54
|
2 |
|
$republishedQueueIds[] = $queue->getId(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
$this->queueService->commit(); |
|
58
|
2 |
|
$this->publisherFactory->releaseAll(); |
|
59
|
|
|
|
|
60
|
2 |
|
if ($republishedQueueIds) { |
|
|
|
|
|
|
61
|
2 |
|
$this->logger->info( |
|
62
|
2 |
|
ConstantMessage::QUEUE_SUCCESS_REPUBLISH, |
|
63
|
2 |
|
['queuesIds' => implode(', ', $republishedQueueIds)] |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
1 |
|
} catch (Throwable $exception) { |
|
67
|
1 |
|
$this->queueService->rollback(); |
|
68
|
|
|
|
|
69
|
1 |
|
$this->logger->error( |
|
70
|
1 |
|
ConstantMessage::QUEUE_CAN_NOT_REPUBLISH, |
|
71
|
|
|
[ |
|
72
|
1 |
|
'exception' => get_class($exception), |
|
73
|
1 |
|
'message' => $exception->getMessage(), |
|
74
|
|
|
] |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
1 |
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
if (0 === $i && count($queues) < $batchSize) { |
|
81
|
1 |
|
return true; |
|
82
|
|
|
} |
|
83
|
1 |
|
++$i; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
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.