Files
cunkebao_v3/Server/application/command/FriendTaskCommand.php

60 lines
1.7 KiB
PHP
Raw Permalink Normal View History

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;
use think\facade\Log;
use think\Queue;
2025-03-24 16:42:36 +08:00
use app\job\FriendTaskJob;
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 FriendTaskCommand extends Command
2025-03-24 14:59:19 +08:00
{
protected function configure()
{
2025-03-24 16:42:36 +08:00
$this->setName('friend:task')
->setDescription('获取添加好友认为列表,并根据分页自动处理下一页');
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-12 15:08:21 +08:00
// 从缓存获取初始页码缓存10分钟有效
2025-04-14 09:21:01 +08:00
$pageIndex = Cache::get('friendTaskPage', 0);
2025-04-12 15:08:21 +08:00
$output->writeln('从缓存获取页码:' . $pageIndex);
2025-03-24 14:59:19 +08:00
2025-04-12 15:08:21 +08:00
$pageSize = 1000; // 每页获取1000条记录
// 将任务添加到队列
2025-03-24 14:59:19 +08:00
$this->addToQueue($pageIndex, $pageSize);
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 每页大小
*/
protected function addToQueue($pageIndex, $pageSize)
{
$data = [
'pageIndex' => $pageIndex,
'pageSize' => $pageSize
];
2025-03-24 16:42:36 +08:00
// 添加到队列,设置任务名为 friend_task
Queue::push(FriendTaskJob::class, $data, 'friend_task');
2025-03-24 14:59:19 +08:00
}
}