-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotifyCommand.php
100 lines (87 loc) · 2.42 KB
/
NotifyCommand.php
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
declare(strict_types=1);
/**
* Copyright 2010 - 2024, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2024, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\QueueMonitor\Command;
use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Core\Configure;
use Cake\Log\LogTrait;
use CakeDC\QueueMonitor\Core\DisableTrait;
use CakeDC\QueueMonitor\Service\QueueMonitoringService;
use Exception;
use Psr\Log\LogLevel;
use function Cake\I18n\__;
/**
* QueueMonitoringNotify command.
*/
final class NotifyCommand extends Command
{
use DisableTrait;
use LogTrait;
private const DEFAULT_LONG_JOB_IN_MINUTES = 30;
/**
* Constructor
*/
public function __construct(
private readonly QueueMonitoringService $queueMonitoringService
) {
parent::__construct();
}
/**
* @inheritDoc
*/
public static function defaultName(): string
{
return 'queue-monitor notify';
}
/**
* @inheritDoc
*/
public static function getDescription(): string
{
return __('Queue Monitoring notifier');
}
/**
* @inheritDoc
*/
public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
return parent::buildOptionParser($parser)
->setDescription(self::getDescription());
}
/**
* @inheritDoc
*/
public function execute(Arguments $args, ConsoleIo $io)
{
if ($this->isDisabled()) {
$this->log(
'Notification were not sent because Queue Monitor is disabled.',
LogLevel::WARNING
);
return self::CODE_SUCCESS;
}
try {
$this->queueMonitoringService->notifyAboutLongRunningJobs(
(int)Configure::read(
'QueueMonitor.longJobInMinutes',
self::DEFAULT_LONG_JOB_IN_MINUTES
)
);
} catch (Exception $e) {
$this->log("Failed to send queue stuck notifications, reason: {$e->getMessage()}");
return self::CODE_ERROR;
}
return self::CODE_SUCCESS;
}
}