1C-Bitrix
25.700.0
Загрузка...
Поиск...
Не найдено
consumemessagescommand.php
См. документацию.
1
<?php
2
3
declare(strict_types=1);
4
5
namespace
Bitrix\Main\Cli\Command\Messenger
;
6
7
use
Bitrix\Main\Config\Configuration
;
8
use
Bitrix\Main\Messenger\Config\WorkerRunMode
;
9
use
Bitrix\Main\Messenger\Internals\Worker
;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Exception\InvalidOptionException;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
25
class
ConsumeMessagesCommand
extends
Command
26
{
27
protected
function
configure
(): void
28
{
29
$this
30
->setName(
'messenger:consume'
)
31
->setDescription(
'Run Messenger\'s worker'
)
32
->setDefinition(
33
[
34
new
InputArgument(
35
'queues'
,
36
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
37
'Names of the queues to consume'
38
),
39
// new InputOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Limit the number of received messages'),
40
new
InputOption(
41
'time-limit'
,
42
't'
,
43
InputOption::VALUE_REQUIRED,
44
'The time limit in seconds the worker can handle new messages'
45
),
46
new
InputOption(
47
'sleep'
,
48
null
,
49
InputOption::VALUE_REQUIRED,
50
'Seconds to sleep before asking for new messages after worker iteration'
,
51
1
52
),
53
]
54
)
55
;
56
}
57
58
protected
function
execute
(InputInterface $input, OutputInterface
$output
): int
59
{
60
$output
->writeln(
'Started '
. date(
'Y-m-d H:i:s'
) .
'. PID: '
. getmypid());
61
62
if
(
null
!== $timeLimit = $input->getOption(
'time-limit'
))
63
{
64
if
(!is_numeric($timeLimit) || $timeLimit < 1)
65
{
66
throw
new
InvalidOptionException(
67
\sprintf(
'Option "time-limit" must be a positive integer, "%s" passed.'
, $timeLimit)
68
);
69
}
70
}
71
72
$config
= Configuration::getValue(
'messenger'
);
73
74
if
(!
$config
)
75
{
76
return
self::SUCCESS;
77
}
78
79
if
(!empty(
$config
[
'run_mode'
]) &&
$config
[
'run_mode'
] ===
WorkerRunMode::Cli
->value)
80
{
81
$worker =
new
Worker
();
82
83
$options
= [
84
'sleep'
=> $input->getOption(
'sleep'
) * 1000000,
85
];
86
87
if
($queues = $input->getArgument(
'queues'
))
88
{
89
$options
[
'queues'
] = $queues;
90
}
91
92
$startTime
= time();
93
$endTime = $timeLimit ?
$startTime
+ $timeLimit :
null
;
94
95
while
(!$endTime || (time() < $endTime))
96
{
97
$worker->process(
$options
);
98
99
usleep(
$options
[
'sleep'
]);
100
}
101
}
102
else
103
{
104
$output
->writeln(
105
'Messenger not configured to run in CLI. Set "messenger.run_mode" to "cli" in .setting.php'
106
);
107
}
108
109
$output
->writeln(
'Stopped '
. date(
'Y-m-d H:i:s'
) .
'. PID: '
. getmypid());
110
111
return
self::SUCCESS;
112
}
113
}
Bitrix\Main\Cli\Command\Messenger\ConsumeMessagesCommand
Определения
consumemessagescommand.php:26
Bitrix\Main\Cli\Command\Messenger\ConsumeMessagesCommand\configure
configure()
Определения
consumemessagescommand.php:27
Bitrix\Main\Cli\Command\Messenger\ConsumeMessagesCommand\execute
execute(InputInterface $input, OutputInterface $output)
Определения
consumemessagescommand.php:58
Bitrix\Main\Config\Configuration
Определения
configuration.php:9
Bitrix\Main\Messenger\Internals\Worker
Определения
Worker.php:16
$startTime
$startTime
Определения
sync.php:69
$options
$options
Определения
commerceml2.php:49
$output
$output
Определения
options.php:436
Bitrix\Main\Cli\Command\Messenger
Определения
consumemessagescommand.php:5
Bitrix\Main\Messenger\Config\WorkerRunMode
WorkerRunMode
Определения
WorkerRunMode.php:8
Bitrix\Main\Messenger\Config\Cli
@ Cli
Определения
WorkerRunMode.php:10
$config
$config
Определения
quickway.php:69
bitrix
modules
main
lib
cli
command
messenger
consumemessagescommand.php
Создано системой
1.14.0