【触客宝】 好友、群、设备队列优化
This commit is contained in:
@@ -29,6 +29,9 @@ class WechatFriendController extends BaseController
|
||||
}
|
||||
|
||||
try {
|
||||
// 初始化isUpdate标志为false
|
||||
$isUpdate = false;
|
||||
|
||||
// 根据isDel设置对应的isDeleted值
|
||||
$isDeleted = null; // 默认值
|
||||
if ($isDel === '0' || $isDel === 0) {
|
||||
@@ -67,13 +70,17 @@ class WechatFriendController extends BaseController
|
||||
|
||||
// 保存数据到数据库
|
||||
if (is_array($response)) {
|
||||
$isUpdate = false;
|
||||
foreach ($response as $item) {
|
||||
$this->saveFriend($item);
|
||||
$updated = $this->saveFriend($item);
|
||||
if($updated){
|
||||
$isUpdate = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($isJob) {
|
||||
return json_encode(['code' => 200, 'msg' => 'success', 'data' => $response]);
|
||||
return json_encode(['code' => 200, 'msg' => 'success', 'data' => $response, 'isUpdate' => $isUpdate]);
|
||||
} else {
|
||||
return successJson($response);
|
||||
}
|
||||
@@ -90,6 +97,7 @@ class WechatFriendController extends BaseController
|
||||
/**
|
||||
* 保存微信好友数据到数据库
|
||||
* @param array $item 微信好友数据
|
||||
* @return bool 是否创建或更新了记录
|
||||
*/
|
||||
private function saveFriend($item)
|
||||
{
|
||||
@@ -137,9 +145,11 @@ class WechatFriendController extends BaseController
|
||||
$friend = WechatFriendModel::where('id', $item['id'])->find();
|
||||
|
||||
if ($friend) {
|
||||
$friend->save($data);
|
||||
$result = $friend->save($data);
|
||||
return false;
|
||||
} else {
|
||||
WechatFriendModel::create($data);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,5 +21,6 @@ return [
|
||||
'department:list' => 'app\command\DepartmentListCommand', // 部门列表 √
|
||||
'content:sync' => 'app\command\SyncContentCommand', // 同步内容库 √
|
||||
'groupFriends:list' => 'app\command\GroupFriendsCommand', // 微信群好友列表
|
||||
|
||||
'allotFriends:run' => 'app\command\AllotFriendCommand', // 自动分配微信好友
|
||||
'allotChatroom:run' => 'app\command\AllotChatroomCommand', // 自动分配微信群聊
|
||||
];
|
||||
|
||||
@@ -81,7 +81,7 @@ class DeviceListCommand extends Command
|
||||
* @param string $cacheKey 缓存键名
|
||||
* @param string $queueLockKey 队列锁键名
|
||||
*/
|
||||
protected function addToQueue($pageIndex, $pageSize, $isDel = '', $jobId = '', $cacheKey = '', $queueLockKey = '')
|
||||
public function addToQueue($pageIndex, $pageSize, $isDel = '', $jobId = '', $cacheKey = '', $queueLockKey = '')
|
||||
{
|
||||
$data = [
|
||||
'pageIndex' => $pageIndex,
|
||||
|
||||
@@ -81,7 +81,7 @@ class WechatChatroomCommand extends Command
|
||||
* @param string $cacheKey 缓存键名
|
||||
* @param string $queueLockKey 队列锁键名
|
||||
*/
|
||||
protected function addToQueue($pageIndex, $pageSize, $isDel = '', $jobId = '', $cacheKey = '', $queueLockKey = '')
|
||||
public function addToQueue($pageIndex, $pageSize, $isDel = '', $jobId = '', $cacheKey = '', $queueLockKey = '')
|
||||
{
|
||||
$data = [
|
||||
'pageIndex' => $pageIndex,
|
||||
|
||||
@@ -88,7 +88,7 @@ class DeviceListJob
|
||||
} else {
|
||||
// 处理完所有页面,重置页码并释放队列锁
|
||||
Cache::set($cacheKey, 0, 86400);
|
||||
Cache::delete($queueLockKey);
|
||||
Cache::rm($queueLockKey);
|
||||
Log::info("所有设备列表页面处理完毕,重置页码为0,释放队列锁: {$queueLockKey}");
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ class DeviceListJob
|
||||
|
||||
if ($job->attempts() > 3) {
|
||||
// 超过重试次数,删除任务并释放队列锁
|
||||
Cache::delete($queueLockKey);
|
||||
Cache::rm($queueLockKey);
|
||||
Log::info("由于错误释放队列锁: {$queueLockKey}");
|
||||
$job->delete();
|
||||
} else {
|
||||
@@ -117,7 +117,7 @@ class DeviceListJob
|
||||
Log::error('设备列表任务处理异常: ' . $e->getMessage());
|
||||
|
||||
if (!empty($queueLockKey)) {
|
||||
Cache::delete($queueLockKey);
|
||||
Cache::rm($queueLockKey);
|
||||
Log::info("由于异常释放队列锁: {$queueLockKey}");
|
||||
}
|
||||
|
||||
|
||||
@@ -74,13 +74,13 @@ class WechatChatroomJob
|
||||
} else {
|
||||
// 处理完所有页面,重置页码并释放队列锁
|
||||
Cache::set($cacheKey, 0, 86400);
|
||||
Cache::delete($queueLockKey);
|
||||
Cache::rm($queueLockKey);
|
||||
Log::info("所有微信聊天室列表页面处理完毕,重置页码为0,释放队列锁: {$queueLockKey}");
|
||||
}
|
||||
} else {
|
||||
// API调用出错,记录错误并释放队列锁
|
||||
Log::error("微信聊天室列表获取失败: " . $result['msg']);
|
||||
Cache::delete($queueLockKey);
|
||||
Cache::rm($queueLockKey);
|
||||
Log::info("由于错误释放队列锁: {$queueLockKey}");
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ class WechatChatroomJob
|
||||
// 出现异常,记录错误并释放队列锁
|
||||
Log::error('微信聊天室列表任务处理异常: ' . $e->getMessage());
|
||||
if (!empty($queueLockKey)) {
|
||||
Cache::delete($queueLockKey);
|
||||
Cache::rm($queueLockKey);
|
||||
Log::info("由于异常释放队列锁: {$queueLockKey}");
|
||||
}
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ class WechatFriendJob
|
||||
$data = $response['data'];
|
||||
|
||||
// 判断是否有下一页
|
||||
if (!empty($data) && count($data) > 0) {
|
||||
if (!empty($data) && count($data) > 0 && empty($response['isUpdate'])) {
|
||||
// 获取最后一条记录的ID
|
||||
$lastFriendId = $data[count($data)-1]['id'];
|
||||
|
||||
@@ -100,7 +100,7 @@ class WechatFriendJob
|
||||
// 没有下一页,重置缓存并释放队列锁
|
||||
Cache::set($pageIndexCacheKey, 0, 86400);
|
||||
Cache::set($preFriendIdCacheKey, '', 86400);
|
||||
Cache::delete($queueLockKey);
|
||||
Cache::rm($queueLockKey);
|
||||
Log::info("所有微信好友列表页面处理完毕,重置页码为0,释放队列锁: {$queueLockKey}");
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ class WechatFriendJob
|
||||
|
||||
if ($job->attempts() > 3) {
|
||||
// 超过重试次数,删除任务并释放队列锁
|
||||
Cache::delete($queueLockKey);
|
||||
Cache::rm($queueLockKey);
|
||||
Log::info("由于错误释放队列锁: {$queueLockKey}");
|
||||
$job->delete();
|
||||
} else {
|
||||
@@ -130,7 +130,7 @@ class WechatFriendJob
|
||||
Log::error('微信好友列表任务异常:' . $e->getMessage());
|
||||
|
||||
if (!empty($queueLockKey)) {
|
||||
Cache::delete($queueLockKey);
|
||||
Cache::rm($queueLockKey);
|
||||
Log::info("由于异常释放队列锁: {$queueLockKey}");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user