【触客宝】 队列添加限制,并优化整体流程

This commit is contained in:
wong
2025-04-23 18:13:01 +08:00
parent a6273e45ea
commit 16a723bcaf
11 changed files with 491 additions and 298 deletions

View File

@@ -5,16 +5,23 @@ namespace app\command;
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 app\job\DeviceListJob;
use think\facade\Cache;
class DeviceListCommand extends Command
{
// 队列名称
protected $queueName = 'device_list';
protected function configure()
{
$this->setName('device:list')
->setDescription('获取设备列表,并根据分页自动处理下一页');
->setDescription('获取设备列表,并根据分页自动处理下一页')
->addOption('isDel', null, Option::VALUE_OPTIONAL, '删除状态: 0=未删除(unDeleted), 1=已删除(deleted), 2=已停用(deletedAndStop)', '')
->addOption('jobId', null, Option::VALUE_OPTIONAL, '任务ID用于区分不同实例', date('YmdHis') . rand(1000, 9999));
}
protected function execute(Input $input, Output $output)
@@ -22,12 +29,38 @@ class DeviceListCommand extends Command
$output->writeln('开始处理设备列表任务...');
try {
// 初始页码
$pageIndex = 0;
// 获取是否删除参数和任务ID
$isDel = $input->getOption('isDel');
$jobId = $input->getOption('jobId');
$output->writeln('删除状态参数: ' . ($isDel === '' ? '全部' : ($isDel == 0 ? '未删除' : ($isDel == 1 ? '已删除' : '已停用'))));
$output->writeln('任务ID: ' . $jobId);
// 检查队列是否已经在运行
$queueLockKey = "queue_lock:{$this->queueName}:{$isDel}";
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 = "devicePage:{$jobId}";
$cacheKeySuffix = $isDel === '' ? '' : ":{$isDel}";
$cacheKey = $cacheKeyPrefix . $cacheKeySuffix;
// 从缓存获取初始页码缓存有效期1天
$pageIndex = Cache::get($cacheKey, 0);
$output->writeln("从缓存获取页码: {$pageIndex}, 缓存键: {$cacheKey}");
$pageSize = 100; // 每页获取100条记录
// 将第一页任务添加到队列
$this->addToQueue($pageIndex, $pageSize);
// 将任务添加到队列
$this->addToQueue($pageIndex, $pageSize, $isDel, $jobId, $cacheKey, $queueLockKey);
$output->writeln('设备列表任务已添加到队列');
} catch (\Exception $e) {
@@ -43,15 +76,23 @@ class DeviceListCommand extends Command
* 添加任务到队列
* @param int $pageIndex 页码
* @param int $pageSize 每页大小
* @param string $isDel 删除状态
* @param string $jobId 任务ID
* @param string $cacheKey 缓存键名
* @param string $queueLockKey 队列锁键名
*/
protected function addToQueue($pageIndex, $pageSize)
protected function addToQueue($pageIndex, $pageSize, $isDel = '', $jobId = '', $cacheKey = '', $queueLockKey = '')
{
$data = [
'pageIndex' => $pageIndex,
'pageSize' => $pageSize
'pageSize' => $pageSize,
'isDel' => $isDel,
'jobId' => $jobId,
'cacheKey' => $cacheKey,
'queueLockKey' => $queueLockKey
];
// 添加到队列,设置任务名为 device_list
Queue::push(DeviceListJob::class, $data, 'device_list');
Queue::push(DeviceListJob::class, $data, $this->queueName);
}
}
}

View File

@@ -5,6 +5,7 @@ namespace app\command;
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 app\job\WechatChatroomJob;
@@ -12,10 +13,15 @@ use think\facade\Cache;
class WechatChatroomCommand extends Command
{
// 队列名称
protected $queueName = 'wechat_chatroom';
protected function configure()
{
$this->setName('wechatChatroom:list')
->setDescription('获取微信聊天室列表,并根据分页自动处理下一页');
->setDescription('获取微信聊天室列表,并根据分页自动处理下一页')
->addOption('isDel', null, Option::VALUE_OPTIONAL, '删除状态: 0=未删除(false), 1=已删除(true)', '')
->addOption('jobId', null, Option::VALUE_OPTIONAL, '任务ID用于区分不同实例', date('YmdHis') . rand(1000, 9999));
}
protected function execute(Input $input, Output $output)
@@ -23,14 +29,38 @@ class WechatChatroomCommand extends Command
$output->writeln('开始处理微信聊天室列表任务...');
try {
// 从缓存获取初始页码缓存10分钟有效
$pageIndex = Cache::get('chatroomPage', 0);
$output->writeln('从缓存获取页码:' . $pageIndex);
// 获取是否删除参数和任务ID
$isDel = $input->getOption('isDel');
$jobId = $input->getOption('jobId');
$output->writeln('删除状态参数: ' . ($isDel === '' ? '全部' : ($isDel == 0 ? '未删除' : '已删除')));
$output->writeln('任务ID: ' . $jobId);
// 检查队列是否已经在运行
$queueLockKey = "queue_lock:{$this->queueName}:{$isDel}";
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}");
$pageSize = 100; // 每页获取100条记录
// 将任务添加到队列
$this->addToQueue($pageIndex, $pageSize);
$this->addToQueue($pageIndex, $pageSize, $isDel, $jobId, $cacheKey, $queueLockKey);
$output->writeln('微信聊天室列表任务已添加到队列');
} catch (\Exception $e) {
@@ -46,15 +76,23 @@ class WechatChatroomCommand extends Command
* 添加任务到队列
* @param int $pageIndex 页码
* @param int $pageSize 每页大小
* @param string $isDel 删除状态
* @param string $jobId 任务ID
* @param string $cacheKey 缓存键名
* @param string $queueLockKey 队列锁键名
*/
protected function addToQueue($pageIndex, $pageSize)
protected function addToQueue($pageIndex, $pageSize, $isDel = '', $jobId = '', $cacheKey = '', $queueLockKey = '')
{
$data = [
'pageIndex' => $pageIndex,
'pageSize' => $pageSize
'pageSize' => $pageSize,
'isDel' => $isDel,
'jobId' => $jobId,
'cacheKey' => $cacheKey,
'queueLockKey' => $queueLockKey
];
// 添加到队列,设置任务名为 wechat_chatroom
Queue::push(WechatChatroomJob::class, $data, 'wechat_chatroom');
Queue::push(WechatChatroomJob::class, $data, $this->queueName);
}
}

View File

@@ -5,6 +5,7 @@ namespace app\command;
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 app\job\WechatFriendJob;
@@ -12,10 +13,15 @@ use think\facade\Cache;
class WechatFriendCommand extends Command
{
// 队列名称
protected $queueName = 'wechat_friends';
protected function configure()
{
$this->setName('wechatFriends:list')
->setDescription('获微信列表,并根据分页自动处理下一页');
->setDescription('获微信列表,并根据分页自动处理下一页')
->addOption('isDel', null, Option::VALUE_OPTIONAL, '删除状态: 0=未删除(false), 1=已删除(true)', '')
->addOption('jobId', null, Option::VALUE_OPTIONAL, '任务ID用于区分不同实例', date('YmdHis') . rand(1000, 9999));
}
protected function execute(Input $input, Output $output)
@@ -23,16 +29,42 @@ class WechatFriendCommand extends Command
$output->writeln('开始处理微信列表任务...');
try {
// 从缓存获取初始页码和上次处理的好友ID缓存10分钟有效
$pageIndex = Cache::get('friendsPage', 0);
$preFriendId = Cache::get('preFriendId', '');
// 获取是否删除参数和任务ID
$isDel = $input->getOption('isDel');
$jobId = $input->getOption('jobId');
$output->writeln('从缓存获取页码:' . $pageIndex . '上次处理的好友ID' . ($preFriendId ?: ''));
$output->writeln('删除状态参数: ' . ($isDel === '' ? '全部' : ($isDel == 0 ? '未删除' : '已删除')));
$output->writeln('任务ID: ' . $jobId);
$pageSize = 100; // 每页获取1000条记录
// 检查队列是否已经在运行
$queueLockKey = "queue_lock:{$this->queueName}:{$isDel}";
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 = "friendsPage:{$jobId}";
$cacheKeySuffix = $isDel === '' ? '' : ":{$isDel}";
$pageIndexCacheKey = $cacheKeyPrefix . $cacheKeySuffix;
$preFriendIdCacheKey = "preFriendId:{$jobId}" . $cacheKeySuffix;
// 从缓存获取初始页码和上次处理的好友ID
$pageIndex = Cache::get($pageIndexCacheKey, 0);
$preFriendId = Cache::get($preFriendIdCacheKey, '');
$output->writeln("从缓存获取页码: {$pageIndex}, 上次处理的好友ID: {$preFriendId}");
$output->writeln("缓存键: {$pageIndexCacheKey}, {$preFriendIdCacheKey}");
$pageSize = 100; // 每页获取100条记录
// 将任务添加到队列
$this->addToQueue($pageIndex, $pageSize, $preFriendId);
$this->addToQueue($pageIndex, $pageSize, $preFriendId, $isDel, $jobId, $pageIndexCacheKey, $preFriendIdCacheKey, $queueLockKey);
$output->writeln('微信列表任务已添加到队列');
} catch (\Exception $e) {
@@ -49,16 +81,26 @@ class WechatFriendCommand extends Command
* @param int $pageIndex 页码
* @param int $pageSize 每页大小
* @param string $preFriendId 上一个好友ID
* @param string $isDel 删除状态
* @param string $jobId 任务ID
* @param string $pageIndexCacheKey 页码缓存键名
* @param string $preFriendIdCacheKey 好友ID缓存键名
* @param string $queueLockKey 队列锁键名
*/
protected function addToQueue($pageIndex, $pageSize, $preFriendId = '')
public function addToQueue($pageIndex, $pageSize, $preFriendId = '', $isDel = '', $jobId = '', $pageIndexCacheKey = '', $preFriendIdCacheKey = '', $queueLockKey = '')
{
$data = [
'pageIndex' => $pageIndex,
'pageSize' => $pageSize,
'preFriendId' => $preFriendId
'preFriendId' => $preFriendId,
'isDel' => $isDel,
'jobId' => $jobId,
'pageIndexCacheKey' => $pageIndexCacheKey,
'preFriendIdCacheKey' => $preFriendIdCacheKey,
'queueLockKey' => $queueLockKey
];
// 添加到队列,设置任务名为 wechat_friends
Queue::push(WechatFriendJob::class, $data, 'wechat_friends');
Queue::push(WechatFriendJob::class, $data, $this->queueName);
}
}
}