64 lines
2.0 KiB
PHP
64 lines
2.0 KiB
PHP
|
|
<?php
|
||
|
|
// application/common/command/QueueMonitor.php
|
||
|
|
|
||
|
|
namespace app\common\command;
|
||
|
|
|
||
|
|
use think\console\Command;
|
||
|
|
use think\console\Input;
|
||
|
|
use think\console\Output;
|
||
|
|
use think\console\input\Option;
|
||
|
|
use app\common\library\GiftQueue;
|
||
|
|
|
||
|
|
class QueueMonitor extends Command
|
||
|
|
{
|
||
|
|
protected function configure()
|
||
|
|
{
|
||
|
|
$this->setName('queue:monitor')
|
||
|
|
->setDescription('监控队列状态')
|
||
|
|
->addOption('interval', 'i', Option::VALUE_OPTIONAL, '监控间隔(秒)', 10);
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function execute(Input $input, Output $output)
|
||
|
|
{
|
||
|
|
$interval = $input->getOption('interval');
|
||
|
|
|
||
|
|
$output->writeln("开始监控送礼队列状态,间隔: {$interval}秒");
|
||
|
|
$output->writeln("按 Ctrl+C 停止监控");
|
||
|
|
|
||
|
|
$history = [];
|
||
|
|
$maxHistory = 60;
|
||
|
|
|
||
|
|
while (true) {
|
||
|
|
try {
|
||
|
|
$stats = GiftQueue::stats();
|
||
|
|
$timestamp = date('H:i:s');
|
||
|
|
|
||
|
|
$output->writeln("[{$timestamp}] 队列大小: {$stats['queue_size']}, 失败队列: {$stats['failed_size']}, 状态: {$stats['status']}");
|
||
|
|
|
||
|
|
// 记录历史数据
|
||
|
|
$history[] = [
|
||
|
|
'time' => $timestamp,
|
||
|
|
'size' => $stats['queue_size'],
|
||
|
|
'status' => $stats['status']
|
||
|
|
];
|
||
|
|
|
||
|
|
if (count($history) > $maxHistory) {
|
||
|
|
array_shift($history);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查队列是否积压
|
||
|
|
if ($stats['queue_size'] > 1000) {
|
||
|
|
$output->writeln("<error>警告:队列积压严重!</error>");
|
||
|
|
} elseif ($stats['queue_size'] > 100) {
|
||
|
|
$output->writeln("<comment>注意:队列正在积压</comment>");
|
||
|
|
}
|
||
|
|
|
||
|
|
sleep($interval);
|
||
|
|
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
$output->writeln("<error>监控异常: " . $e->getMessage() . "</error>");
|
||
|
|
sleep($interval);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|