2025-08-07 09:22:39 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
namespace app\command;
|
|
|
|
|
|
|
|
|
|
|
|
use app\job\WorkbenchGroupPushJob;
|
|
|
|
|
|
use think\console\Command;
|
|
|
|
|
|
use think\console\Input;
|
|
|
|
|
|
use think\console\Output;
|
|
|
|
|
|
use think\console\input\Option;
|
|
|
|
|
|
use think\facade\Log;
|
|
|
|
|
|
use think\Queue;
|
|
|
|
|
|
use think\facade\Cache;
|
|
|
|
|
|
|
|
|
|
|
|
class WorkbenchGroupPushCommand extends Command
|
|
|
|
|
|
{
|
|
|
|
|
|
// 队列名称
|
|
|
|
|
|
protected $queueName = 'workbench_groupPush';
|
|
|
|
|
|
|
|
|
|
|
|
protected function configure()
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->setName('workbench:groupPush')
|
2025-08-07 11:56:04 +08:00
|
|
|
|
->setDescription('工作台群发同步任务队列')
|
2025-08-07 09:22:39 +08:00
|
|
|
|
->addOption('jobId', null, Option::VALUE_OPTIONAL, '任务ID,用于区分不同实例', date('YmdHis') . rand(1000, 9999));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected function execute(Input $input, Output $output)
|
|
|
|
|
|
{
|
2025-08-07 11:56:04 +08:00
|
|
|
|
$output->writeln('开始处理工作台群发同步任务...');
|
2025-08-07 09:22:39 +08:00
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 获取任务ID
|
|
|
|
|
|
$jobId = $input->getOption('jobId');
|
|
|
|
|
|
|
|
|
|
|
|
$output->writeln('任务ID: ' . $jobId);
|
|
|
|
|
|
|
|
|
|
|
|
// 检查队列是否已经在运行
|
|
|
|
|
|
$queueLockKey = "queue_lock:{$this->queueName}";
|
2025-11-07 15:25:50 +08:00
|
|
|
|
Cache::rm($queueLockKey);
|
2025-08-07 09:22:39 +08:00
|
|
|
|
if (Cache::get($queueLockKey)) {
|
|
|
|
|
|
$output->writeln("队列 {$this->queueName} 已经在运行中,跳过执行");
|
|
|
|
|
|
Log::warning("队列 {$this->queueName} 已经在运行中,跳过执行");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设置队列运行锁,有效期1小时
|
|
|
|
|
|
Cache::set($queueLockKey, $jobId, 3600);
|
|
|
|
|
|
$output->writeln("已设置队列运行锁,键名:{$queueLockKey},值:{$jobId},有效期:1小时");
|
|
|
|
|
|
|
|
|
|
|
|
// 将任务添加到队列
|
|
|
|
|
|
$this->addToQueue($jobId, $queueLockKey);
|
|
|
|
|
|
|
2025-08-07 11:56:04 +08:00
|
|
|
|
$output->writeln('工作台群发同步任务已添加到队列');
|
2025-08-07 09:22:39 +08:00
|
|
|
|
} catch (\Exception $e) {
|
2025-08-07 11:56:04 +08:00
|
|
|
|
Log::error('工作台群发同步任务添加失败:' . $e->getMessage());
|
|
|
|
|
|
$output->writeln('工作台群发同步任务添加失败:' . $e->getMessage());
|
2025-08-07 09:22:39 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 添加任务到队列
|
|
|
|
|
|
* @param string $jobId 任务ID
|
|
|
|
|
|
* @param string $queueLockKey 队列锁键名
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function addToQueue($jobId = '', $queueLockKey = '')
|
|
|
|
|
|
{
|
|
|
|
|
|
$data = [
|
|
|
|
|
|
'jobId' => $jobId,
|
|
|
|
|
|
'queueLockKey' => $queueLockKey
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
// 添加到队列,设置任务名为 workbench_groupPush
|
|
|
|
|
|
Queue::push(WorkbenchGroupPushJob::class, $data, $this->queueName);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|