使用pcntl_fork支持多进程点赞
This commit is contained in:
@@ -21,7 +21,7 @@ use app\api\controller\WechatFriendController;
|
|||||||
class WorkbenchJob
|
class WorkbenchJob
|
||||||
{
|
{
|
||||||
/************************************
|
/************************************
|
||||||
* 常量定义与核心队列处理
|
* 常量定义
|
||||||
************************************/
|
************************************/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -37,6 +37,10 @@ class WorkbenchJob
|
|||||||
*/
|
*/
|
||||||
const MAX_RETRY_ATTEMPTS = 3;
|
const MAX_RETRY_ATTEMPTS = 3;
|
||||||
|
|
||||||
|
/************************************
|
||||||
|
* 核心队列处理
|
||||||
|
************************************/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 队列任务处理
|
* 队列任务处理
|
||||||
* @param Job $job 队列任务
|
* @param Job $job 队列任务
|
||||||
@@ -62,59 +66,9 @@ class WorkbenchJob
|
|||||||
return $this->handleJobError($e, $job, $queueLockKey);
|
return $this->handleJobError($e, $job, $queueLockKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 记录任务开始
|
|
||||||
* @param string $jobId
|
|
||||||
* @param string $queueLockKey
|
|
||||||
*/
|
|
||||||
protected function logJobStart($jobId, $queueLockKey)
|
|
||||||
{
|
|
||||||
Log::info('开始处理工作台任务: ' . json_encode([
|
|
||||||
'jobId' => $jobId,
|
|
||||||
'queueLockKey' => $queueLockKey
|
|
||||||
]));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理任务成功
|
|
||||||
* @param Job $job
|
|
||||||
* @param string $queueLockKey
|
|
||||||
*/
|
|
||||||
protected function handleJobSuccess($job, $queueLockKey)
|
|
||||||
{
|
|
||||||
$job->delete();
|
|
||||||
Cache::rm($queueLockKey);
|
|
||||||
Log::info('工作台任务执行成功');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理任务错误
|
|
||||||
* @param \Exception $e
|
|
||||||
* @param Job $job
|
|
||||||
* @param string $queueLockKey
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
protected function handleJobError(\Exception $e, $job, $queueLockKey)
|
|
||||||
{
|
|
||||||
Log::error('工作台任务异常:' . $e->getMessage());
|
|
||||||
|
|
||||||
if (!empty($queueLockKey)) {
|
|
||||||
Cache::rm($queueLockKey);
|
|
||||||
Log::info("由于异常释放队列锁: {$queueLockKey}");
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($job->attempts() > self::MAX_RETRY_ATTEMPTS) {
|
|
||||||
$job->delete();
|
|
||||||
} else {
|
|
||||||
$job->release(Config::get('queue.failed_delay', 10));
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/************************************
|
/************************************
|
||||||
* 工作台处理核心逻辑
|
* 工作台基础功能
|
||||||
************************************/
|
************************************/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -129,18 +83,6 @@ class WorkbenchJob
|
|||||||
])->order('id DESC')->select();
|
])->order('id DESC')->select();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理空工作台情况
|
|
||||||
* @param Job $job
|
|
||||||
* @param string $queueLockKey
|
|
||||||
*/
|
|
||||||
protected function handleEmptyWorkbenches(Job $job, $queueLockKey)
|
|
||||||
{
|
|
||||||
Log::info('没有需要处理的工作台任务');
|
|
||||||
$job->delete();
|
|
||||||
Cache::rm($queueLockKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理工作台列表
|
* 处理工作台列表
|
||||||
* @param \think\Collection $workbenches
|
* @param \think\Collection $workbenches
|
||||||
@@ -227,18 +169,19 @@ class WorkbenchJob
|
|||||||
*/
|
*/
|
||||||
protected function handleAutoLike($workbench, $config)
|
protected function handleAutoLike($workbench, $config)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (!$this->validateAutoLikeConfig($workbench, $config)) {
|
if (!$this->validateAutoLikeConfig($workbench, $config)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证是否在点赞时间范围内
|
// 验证是否在点赞时间范围内
|
||||||
if (!$this->isWithinLikeTimeRange($config)) {
|
if (!$this->isWithinLikeTimeRange($config)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理分页获取好友列表
|
// 处理分页获取好友列表
|
||||||
$this->processAllFriends($workbench, $config);
|
$this->processAllFriends($workbench, $config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理所有好友分页
|
* 处理所有好友分页
|
||||||
* @param Workbench $workbench
|
* @param Workbench $workbench
|
||||||
@@ -252,81 +195,111 @@ class WorkbenchJob
|
|||||||
if (empty($friendList)) {
|
if (empty($friendList)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($friendList as $friend) {
|
// 将好友列表分成20组
|
||||||
// 验证是否达到点赞次数上限
|
$friendGroups = array_chunk($friendList, 20);
|
||||||
$likeCount = $this->getTodayLikeCount($workbench, $config, $friend['deviceId']);
|
$processes = [];
|
||||||
if ($likeCount >= $config['maxLikes']) {
|
|
||||||
Log::info("工作台 {$workbench->id} 点赞次数已达上限");
|
foreach ($friendGroups as $groupIndex => $friendGroup) {
|
||||||
return;
|
// 创建子进程
|
||||||
}
|
$pid = pcntl_fork();
|
||||||
// 验证是否达到好友点赞次数上限
|
|
||||||
$friendMaxLikes = Db::name('workbench_auto_like_item')
|
if ($pid == -1) {
|
||||||
->where('workbenchId', $workbench->id)
|
// 创建进程失败
|
||||||
->where('wechatFriendId', $friend['friendId'])
|
Log::error("工作台 {$workbench->id} 创建进程失败");
|
||||||
->count();
|
continue;
|
||||||
if ($friendMaxLikes < $config['friendMaxLikes']) {
|
} else if ($pid) {
|
||||||
$this->processFriendMoments($workbench, $config, $friend);
|
// 父进程
|
||||||
|
$processes[] = $pid;
|
||||||
|
} else {
|
||||||
|
// 子进程
|
||||||
|
try {
|
||||||
|
foreach ($friendGroup as $friend) {
|
||||||
|
// 验证是否达到点赞次数上限
|
||||||
|
$likeCount = $this->getTodayLikeCount($workbench, $config, $friend['deviceId']);
|
||||||
|
if ($likeCount >= $config['maxLikes']) {
|
||||||
|
Log::info("工作台 {$workbench->id} 点赞次数已达上限");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证是否达到好友点赞次数上限
|
||||||
|
$friendMaxLikes = Db::name('workbench_auto_like_item')
|
||||||
|
->where('workbenchId', $workbench->id)
|
||||||
|
->where('wechatFriendId', $friend['friendId'])
|
||||||
|
->count();
|
||||||
|
|
||||||
|
if ($friendMaxLikes < $config['friendMaxLikes']) {
|
||||||
|
$this->processFriendMoments($workbench, $config, $friend);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::error("工作台 {$workbench->id} 子进程异常: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 子进程执行完毕后退出
|
||||||
|
exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 等待所有子进程完成
|
||||||
|
foreach ($processes as $pid) {
|
||||||
|
pcntl_waitpid($pid, $status);
|
||||||
|
}
|
||||||
|
|
||||||
// 如果当前页数据量等于页大小,说明可能还有更多数据,继续处理下一页
|
// 如果当前页数据量等于页大小,说明可能还有更多数据,继续处理下一页
|
||||||
if (count($friendList) == $pageSize) {
|
if (count($friendList) == $pageSize) {
|
||||||
$this->processAllFriends($workbench, $config, $page + 1, $pageSize);
|
$this->processAllFriends($workbench, $config, $page + 1, $pageSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 验证自动点赞配置
|
|
||||||
* @param Workbench $workbench
|
|
||||||
* @param WorkbenchAutoLike $config
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
protected function validateAutoLikeConfig($workbench, $config)
|
|
||||||
{
|
|
||||||
$requiredFields = ['contentTypes', 'interval', 'maxLikes', 'startTime', 'endTime'];
|
|
||||||
foreach ($requiredFields as $field) {
|
|
||||||
if (empty($config[$field])) {
|
|
||||||
Log::error("工作台 {$workbench->id} 配置字段 {$field} 为空");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取今日点赞次数
|
* 获取好友列表
|
||||||
* @param Workbench $workbench
|
* @param WorkbenchAutoLike $config 配置
|
||||||
* @param WorkbenchAutoLike $config
|
* @param int $page 页码
|
||||||
* @return int
|
* @param int $pageSize 每页大小
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function getTodayLikeCount($workbench, $config, $deviceId)
|
protected function getFriendList($config, $page = 1, $pageSize = 100)
|
||||||
{
|
{
|
||||||
return Db::name('workbench_auto_like_item')
|
$friends = json_decode($config['friends'], true);
|
||||||
->where('workbenchId', $workbench->id)
|
$devices = json_decode($config['devices'], true);
|
||||||
->where('deviceId', $deviceId)
|
|
||||||
->whereTime('createTime', 'between', [
|
$list = Db::table('s2_company_account')
|
||||||
strtotime(date('Y-m-d') . ' ' . $config['startTime'] . ':00'),
|
->alias('ca')
|
||||||
strtotime(date('Y-m-d') . ' ' . $config['endTime'] . ':00')
|
->join(['s2_wechat_account' => 'wa'], 'ca.id = wa.deviceAccountId')
|
||||||
|
->join(['s2_wechat_friend' => 'wf'], 'ca.id = wf.accountId AND wf.wechatAccountId = wa.id')
|
||||||
|
->join('workbench_auto_like_item wali', 'wali.wechatFriendId = wf.id AND wali.workbenchId = ' . $config['workbenchId'], 'left')
|
||||||
|
->where([
|
||||||
|
'ca.status' => 0,
|
||||||
|
'wf.isDeleted' => 0,
|
||||||
|
'wa.deviceAlive' => 1,
|
||||||
|
'wa.wechatAlive' => 1
|
||||||
])
|
])
|
||||||
->count();
|
->whereIn('wa.currentDeviceId', $devices)
|
||||||
|
->field([
|
||||||
|
'ca.id as accountId',
|
||||||
|
'ca.userName',
|
||||||
|
'wf.id as friendId',
|
||||||
|
'wf.wechatId',
|
||||||
|
'wf.wechatAccountId',
|
||||||
|
'wa.wechatId as wechatAccountWechatId',
|
||||||
|
'wa.currentDeviceId as deviceId',
|
||||||
|
'COUNT(wali.id) as like_count'
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!empty($friends) && is_array($friends) && count($friends) > 0) {
|
||||||
|
$list = $list->whereIn('wf.id', $friends);
|
||||||
|
}
|
||||||
|
|
||||||
|
$list = $list->group('wf.wechatId')
|
||||||
|
->having('like_count < ' . $config['friendMaxLikes'])
|
||||||
|
->order('wf.id DESC')
|
||||||
|
->page($page, $pageSize)
|
||||||
|
->select();
|
||||||
|
|
||||||
|
return $list;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查是否在点赞时间范围内
|
|
||||||
* @param WorkbenchAutoLike $config
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
protected function isWithinLikeTimeRange($config)
|
|
||||||
{
|
|
||||||
$currentTime = date('H:i');
|
|
||||||
if ($currentTime < $config['startTime'] || $currentTime > $config['endTime']) {
|
|
||||||
Log::info("当前时间 {$currentTime} 不在点赞时间范围内 ({$config['startTime']} - {$config['endTime']})");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理好友朋友圈
|
* 处理好友朋友圈
|
||||||
* @param Workbench $workbench
|
* @param Workbench $workbench
|
||||||
@@ -385,28 +358,6 @@ class WorkbenchJob
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取好友标签
|
|
||||||
* @param int $friendId
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
protected function getFriendLabels($friend)
|
|
||||||
{
|
|
||||||
// 获取好友标签
|
|
||||||
$wechatFriendController = new WechatFriendController();
|
|
||||||
$result = $wechatFriendController->getlist([ 'friendKeyword' => $friend['wechatId'],'wechatAccountKeyword' => $friend['wechatAccountWechatId']],true);
|
|
||||||
$result = json_decode($result, true);
|
|
||||||
$labels = [];
|
|
||||||
if(!empty($result['data'])){
|
|
||||||
foreach($result['data'] as $item){
|
|
||||||
$labels = array_merge($labels, $item['labels']);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $labels;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取未点赞的朋友圈
|
* 获取未点赞的朋友圈
|
||||||
* @param int $friendId
|
* @param int $friendId
|
||||||
@@ -480,6 +431,82 @@ class WorkbenchJob
|
|||||||
Log::info("工作台 {$workbench->id} 点赞成功: {$moment['snsId']}");
|
Log::info("工作台 {$workbench->id} 点赞成功: {$moment['snsId']}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取好友标签
|
||||||
|
* @param array $friend
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getFriendLabels($friend)
|
||||||
|
{
|
||||||
|
$wechatFriendController = new WechatFriendController();
|
||||||
|
$result = $wechatFriendController->getlist([
|
||||||
|
'friendKeyword' => $friend['wechatId'],
|
||||||
|
'wechatAccountKeyword' => $friend['wechatAccountWechatId']
|
||||||
|
], true);
|
||||||
|
|
||||||
|
$result = json_decode($result, true);
|
||||||
|
$labels = [];
|
||||||
|
|
||||||
|
if(!empty($result['data'])){
|
||||||
|
foreach($result['data'] as $item){
|
||||||
|
$labels = array_merge($labels, $item['labels']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $labels;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证自动点赞配置
|
||||||
|
* @param Workbench $workbench
|
||||||
|
* @param WorkbenchAutoLike $config
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function validateAutoLikeConfig($workbench, $config)
|
||||||
|
{
|
||||||
|
$requiredFields = ['contentTypes', 'interval', 'maxLikes', 'startTime', 'endTime'];
|
||||||
|
foreach ($requiredFields as $field) {
|
||||||
|
if (empty($config[$field])) {
|
||||||
|
Log::error("工作台 {$workbench->id} 配置字段 {$field} 为空");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取今日点赞次数
|
||||||
|
* @param Workbench $workbench
|
||||||
|
* @param WorkbenchAutoLike $config
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
protected function getTodayLikeCount($workbench, $config, $deviceId)
|
||||||
|
{
|
||||||
|
return Db::name('workbench_auto_like_item')
|
||||||
|
->where('workbenchId', $workbench->id)
|
||||||
|
->where('deviceId', $deviceId)
|
||||||
|
->whereTime('createTime', 'between', [
|
||||||
|
strtotime(date('Y-m-d') . ' ' . $config['startTime'] . ':00'),
|
||||||
|
strtotime(date('Y-m-d') . ' ' . $config['endTime'] . ':00')
|
||||||
|
])
|
||||||
|
->count();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查是否在点赞时间范围内
|
||||||
|
* @param WorkbenchAutoLike $config
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function isWithinLikeTimeRange($config)
|
||||||
|
{
|
||||||
|
$currentTime = date('H:i');
|
||||||
|
if ($currentTime < $config['startTime'] || $currentTime > $config['endTime']) {
|
||||||
|
Log::info("当前时间 {$currentTime} 不在点赞时间范围内 ({$config['startTime']} - {$config['endTime']})");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/************************************
|
/************************************
|
||||||
* 朋友圈同步功能
|
* 朋友圈同步功能
|
||||||
************************************/
|
************************************/
|
||||||
@@ -524,53 +551,70 @@ class WorkbenchJob
|
|||||||
// TODO: 实现自动建群逻辑
|
// TODO: 实现自动建群逻辑
|
||||||
Log::info("处理自动建群任务: {$workbench->id}");
|
Log::info("处理自动建群任务: {$workbench->id}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/************************************
|
/************************************
|
||||||
* 辅助方法
|
* 任务处理辅助方法
|
||||||
************************************/
|
************************************/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 记录任务开始
|
||||||
|
* @param string $jobId
|
||||||
|
* @param string $queueLockKey
|
||||||
|
*/
|
||||||
|
protected function logJobStart($jobId, $queueLockKey)
|
||||||
|
{
|
||||||
|
Log::info('开始处理工作台任务: ' . json_encode([
|
||||||
|
'jobId' => $jobId,
|
||||||
|
'queueLockKey' => $queueLockKey
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取好友列表
|
* 处理任务成功
|
||||||
* @param WorkbenchAutoLike $config 配置
|
* @param Job $job
|
||||||
* @param int $page 页码
|
* @param string $queueLockKey
|
||||||
* @param int $pageSize 每页大小
|
|
||||||
* @return array
|
|
||||||
*/
|
*/
|
||||||
protected function getFriendList($config, $page = 1, $pageSize = 100)
|
protected function handleJobSuccess($job, $queueLockKey)
|
||||||
{
|
{
|
||||||
$friends = json_decode($config['friends'], true);
|
$job->delete();
|
||||||
$devices = json_decode($config['devices'], true);
|
Cache::rm($queueLockKey);
|
||||||
|
Log::info('工作台任务执行成功');
|
||||||
|
}
|
||||||
|
|
||||||
$list = Db::table('s2_company_account')
|
/**
|
||||||
->alias('ca')
|
* 处理任务错误
|
||||||
->join(['s2_wechat_account' => 'wa'], 'ca.id = wa.deviceAccountId')
|
* @param \Exception $e
|
||||||
->join(['s2_wechat_friend' => 'wf'], 'ca.id = wf.accountId AND wf.wechatAccountId = wa.id')
|
* @param Job $job
|
||||||
->where([
|
* @param string $queueLockKey
|
||||||
'ca.status' => 0,
|
* @return bool
|
||||||
'wf.isDeleted' => 0,
|
*/
|
||||||
'wa.deviceAlive' => 1,
|
protected function handleJobError(\Exception $e, $job, $queueLockKey)
|
||||||
'wa.wechatAlive' => 1
|
{
|
||||||
])
|
Log::error('工作台任务异常:' . $e->getMessage());
|
||||||
->whereIn('wa.currentDeviceId', $devices)
|
|
||||||
->field([
|
if (!empty($queueLockKey)) {
|
||||||
'ca.id as accountId',
|
Cache::rm($queueLockKey);
|
||||||
'ca.userName',
|
Log::info("由于异常释放队列锁: {$queueLockKey}");
|
||||||
'wf.id as friendId',
|
|
||||||
'wf.wechatId',
|
|
||||||
'wf.wechatAccountId',
|
|
||||||
'wa.wechatId as wechatAccountWechatId',
|
|
||||||
'wa.currentDeviceId as deviceId'
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (!empty($friends) && is_array($friends) && count($friends) > 0) {
|
|
||||||
$list = $list->whereIn('wf.id', $friends);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$list = $list->group('wf.wechatId')
|
if ($job->attempts() > self::MAX_RETRY_ATTEMPTS) {
|
||||||
->order('wf.id DESC')
|
$job->delete();
|
||||||
->page($page, $pageSize)
|
} else {
|
||||||
->select();
|
$job->release(Config::get('queue.failed_delay', 10));
|
||||||
|
}
|
||||||
return $list;
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理空工作台情况
|
||||||
|
* @param Job $job
|
||||||
|
* @param string $queueLockKey
|
||||||
|
*/
|
||||||
|
protected function handleEmptyWorkbenches(Job $job, $queueLockKey)
|
||||||
|
{
|
||||||
|
Log::info('没有需要处理的工作台任务');
|
||||||
|
$job->delete();
|
||||||
|
Cache::rm($queueLockKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user