-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClearFinishedCommand.php
More file actions
64 lines (54 loc) · 1.7 KB
/
Copy pathClearFinishedCommand.php
File metadata and controls
64 lines (54 loc) · 1.7 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
<?php
namespace ADT\BackgroundQueue\Console;
use ADT\BackgroundQueue\BackgroundQueue;
use ADT\BackgroundQueue\Entity\BackgroundJob;
use DateTime;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Schema\SchemaException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
#[AsCommand(name: 'background-queue:clear-finished')]
class ClearFinishedCommand extends Command
{
protected static $defaultName = 'background-queue:clear-finished';
private BackgroundQueue $backgroundQueue;
/**
* @throws Exception
*/
public function __construct(BackgroundQueue $backgroundQueue)
{
parent::__construct();
$this->backgroundQueue = $backgroundQueue;
}
protected function configure()
{
$this->setName('background-queue:clear-finished');
$this->addArgument(
"days",
InputArgument::OPTIONAL,
'Deletes finished records older than the specified number of days.',
1
);
$this->setDescription('Delete finished records.');
}
/**
* @throws Exception
* @throws SchemaException
* @throws \Exception
*/
protected function executeCommand(InputInterface $input, OutputInterface $output): int
{
$qb = $this->backgroundQueue->createQueryBuilder()
->delete($this->backgroundQueue->getConfig()['tableName'])
->andWhere('state = :state')
->setParameter('state', BackgroundJob::STATE_FINISHED);
if ($input->getArgument("days")) {
$qb->andWhere('created_at <= :ago')
->setParameter('ago', (new DateTime('midnight'))->modify('-' . $input->getArgument("days") . ' days')->format('Y-m-d H:i:s'));
}
$qb->executeStatement();
return 0;
}
}