-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessCommand.php
More file actions
77 lines (65 loc) · 2.27 KB
/
Copy pathProcessCommand.php
File metadata and controls
77 lines (65 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace ADT\BackgroundQueue\Console;
use ADT\BackgroundQueue\BackgroundQueue;
use ADT\BackgroundQueue\Entity\BackgroundJob;
use DateTime;
use Exception;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ProcessCommand extends Command
{
protected static $defaultName = 'background-queue:process';
private BackgroundQueue $backgroundQueue;
/**
* @throws Exception
*/
public function __construct(BackgroundQueue $backgroundQueue)
{
parent::__construct();
$this->backgroundQueue = $backgroundQueue;
}
protected function configure()
{
$this->setName('background-queue:process');
$this->setDescription('Processes all records in the READY or TEMPORARILY_FAILED state.');
}
/**
* @throws Exception
*/
protected function executeCommand(InputInterface $input, OutputInterface $output): int
{
$states = BackgroundJob::READY_TO_PROCESS_STATES;
if ($this->backgroundQueue->getConfig()['producer']) {
unset ($states[BackgroundJob::STATE_READY]);
unset ($states[BackgroundJob::STATE_TEMPORARILY_FAILED]);
unset ($states[BackgroundJob::STATE_WAITING]);
} else {
// Nemáme producera
unset ($states[BackgroundJob::STATE_BACK_TO_BROKER]);
}
$qb = $this->backgroundQueue->createQueryBuilder()
->andWhere('state IN (:state)')
->setParameter('state', $states);
/** @var BackgroundJob $_entity */
foreach ($this->backgroundQueue->fetchAll($qb) as $_entity) {
if (
$this->backgroundQueue->getConfig()['producer']
&&
$_entity->getState() !== BackgroundJob::STATE_BROKER_FAILED
) {
$_entity->setState(BackgroundJob::STATE_READY);
$this->backgroundQueue->save($_entity);
$this->backgroundQueue->publishToBroker($_entity);
} else {
if (!$_entity->getProcessedByBroker() && $_entity->getAvailableFrom() > new DateTime()) {
continue;
}
$_entity->setProcessedByBroker(false);
// Chceme použít prioritu na entitě. Může být využito na přeřazení do jiné priority.
// Chceme zařadit do stejné fronty jako původně bylo. Tedy musíme zohlednit co je nastaveno u callbacku.
$this->backgroundQueue->process($_entity, $this->backgroundQueue->getQueueForEntityIncludeCallback($_entity), $_entity->getPriority());
}
}
return 0;
}
}