2025-03-24 14:59:19 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
2025-03-24 16:42:36 +08:00
|
|
|
|
namespace app\command;
|
2025-03-24 14:59:19 +08:00
|
|
|
|
|
|
|
|
|
|
use think\console\Command;
|
|
|
|
|
|
use think\console\Input;
|
|
|
|
|
|
use think\console\Output;
|
2025-04-23 18:13:01 +08:00
|
|
|
|
use think\console\input\Option;
|
2025-03-24 14:59:19 +08:00
|
|
|
|
use think\facade\Log;
|
|
|
|
|
|
use think\Queue;
|
2025-03-24 16:42:36 +08:00
|
|
|
|
use app\job\WechatChatroomJob;
|
2025-04-12 15:08:21 +08:00
|
|
|
|
use think\facade\Cache;
|
2025-03-24 14:59:19 +08:00
|
|
|
|
|
2025-03-24 16:42:36 +08:00
|
|
|
|
class WechatChatroomCommand extends Command
|
2025-03-24 14:59:19 +08:00
|
|
|
|
{
|
2025-04-23 18:13:01 +08:00
|
|
|
|
// 队列名称
|
|
|
|
|
|
protected $queueName = 'wechat_chatroom';
|
|
|
|
|
|
|
2025-03-24 14:59:19 +08:00
|
|
|
|
protected function configure()
|
|
|
|
|
|
{
|
2025-03-24 16:42:36 +08:00
|
|
|
|
$this->setName('wechatChatroom:list')
|
2025-04-23 18:13:01 +08:00
|
|
|
|
->setDescription('获取微信聊天室列表,并根据分页自动处理下一页')
|
|
|
|
|
|
->addOption('isDel', null, Option::VALUE_OPTIONAL, '删除状态: 0=未删除(false), 1=已删除(true)', '')
|
|
|
|
|
|
->addOption('jobId', null, Option::VALUE_OPTIONAL, '任务ID,用于区分不同实例', date('YmdHis') . rand(1000, 9999));
|
2025-03-24 14:59:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected function execute(Input $input, Output $output)
|
|
|
|
|
|
{
|
2025-03-24 16:42:36 +08:00
|
|
|
|
$output->writeln('开始处理微信聊天室列表任务...');
|
2025-03-24 14:59:19 +08:00
|
|
|
|
|
|
|
|
|
|
try {
|
2025-04-23 18:13:01 +08:00
|
|
|
|
// 获取是否删除参数和任务ID
|
|
|
|
|
|
$isDel = $input->getOption('isDel');
|
|
|
|
|
|
$jobId = $input->getOption('jobId');
|
|
|
|
|
|
|
|
|
|
|
|
$output->writeln('删除状态参数: ' . ($isDel === '' ? '全部' : ($isDel == 0 ? '未删除' : '已删除')));
|
|
|
|
|
|
$output->writeln('任务ID: ' . $jobId);
|
|
|
|
|
|
|
|
|
|
|
|
// 检查队列是否已经在运行
|
|
|
|
|
|
$queueLockKey = "queue_lock:{$this->queueName}:{$isDel}";
|
2025-10-16 11:42:59 +08:00
|
|
|
|
Cache::rm($queueLockKey);
|
2025-04-23 18:13:01 +08:00
|
|
|
|
if (Cache::get($queueLockKey)) {
|
|
|
|
|
|
$output->writeln("队列 {$this->queueName} 已经在运行中,删除状态:{$isDel},跳过执行");
|
|
|
|
|
|
Log::warning("队列 {$this->queueName} 已经在运行中,删除状态:{$isDel},跳过执行");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设置队列运行锁,有效期1小时
|
|
|
|
|
|
Cache::set($queueLockKey, $jobId, 3600);
|
|
|
|
|
|
$output->writeln("已设置队列运行锁,键名:{$queueLockKey},值:{$jobId},有效期:1小时");
|
|
|
|
|
|
|
|
|
|
|
|
// 为不同的删除状态和任务ID使用不同的缓存键名
|
|
|
|
|
|
$cacheKeyPrefix = "chatroomPage:{$jobId}";
|
|
|
|
|
|
$cacheKeySuffix = $isDel === '' ? '' : ":{$isDel}";
|
|
|
|
|
|
$cacheKey = $cacheKeyPrefix . $cacheKeySuffix;
|
|
|
|
|
|
|
|
|
|
|
|
// 从缓存获取初始页码,缓存有效期1天
|
|
|
|
|
|
$pageIndex = Cache::get($cacheKey, 0);
|
|
|
|
|
|
$output->writeln("从缓存获取页码: {$pageIndex}, 缓存键: {$cacheKey}");
|
2025-04-12 15:08:21 +08:00
|
|
|
|
|
2025-03-24 14:59:19 +08:00
|
|
|
|
$pageSize = 100; // 每页获取100条记录
|
|
|
|
|
|
|
2025-04-12 15:08:21 +08:00
|
|
|
|
// 将任务添加到队列
|
2025-04-23 18:13:01 +08:00
|
|
|
|
$this->addToQueue($pageIndex, $pageSize, $isDel, $jobId, $cacheKey, $queueLockKey);
|
2025-03-24 14:59:19 +08:00
|
|
|
|
|
2025-03-24 16:42:36 +08:00
|
|
|
|
$output->writeln('微信聊天室列表任务已添加到队列');
|
2025-03-24 14:59:19 +08:00
|
|
|
|
} catch (\Exception $e) {
|
2025-03-24 16:42:36 +08:00
|
|
|
|
Log::error('微信聊天室列表任务添加失败:' . $e->getMessage());
|
|
|
|
|
|
$output->writeln('微信聊天室列表任务添加失败:' . $e->getMessage());
|
2025-03-24 14:59:19 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 添加任务到队列
|
|
|
|
|
|
* @param int $pageIndex 页码
|
|
|
|
|
|
* @param int $pageSize 每页大小
|
2025-04-23 18:13:01 +08:00
|
|
|
|
* @param string $isDel 删除状态
|
|
|
|
|
|
* @param string $jobId 任务ID
|
|
|
|
|
|
* @param string $cacheKey 缓存键名
|
|
|
|
|
|
* @param string $queueLockKey 队列锁键名
|
2025-03-24 14:59:19 +08:00
|
|
|
|
*/
|
2025-04-25 09:31:11 +08:00
|
|
|
|
public function addToQueue($pageIndex, $pageSize, $isDel = '', $jobId = '', $cacheKey = '', $queueLockKey = '')
|
2025-03-24 14:59:19 +08:00
|
|
|
|
{
|
|
|
|
|
|
$data = [
|
|
|
|
|
|
'pageIndex' => $pageIndex,
|
2025-04-23 18:13:01 +08:00
|
|
|
|
'pageSize' => $pageSize,
|
|
|
|
|
|
'isDel' => $isDel,
|
|
|
|
|
|
'jobId' => $jobId,
|
|
|
|
|
|
'cacheKey' => $cacheKey,
|
|
|
|
|
|
'queueLockKey' => $queueLockKey
|
2025-03-24 14:59:19 +08:00
|
|
|
|
];
|
|
|
|
|
|
|
2025-04-12 15:08:21 +08:00
|
|
|
|
// 添加到队列,设置任务名为 wechat_chatroom
|
2025-04-23 18:13:01 +08:00
|
|
|
|
Queue::push(WechatChatroomJob::class, $data, $this->queueName);
|
2025-03-24 14:59:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|