【触客宝】 自动分配好友提交
This commit is contained in:
75
Server/application/job/AutoCreateAllotRulesJob.php
Normal file
75
Server/application/job/AutoCreateAllotRulesJob.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace app\job;
|
||||
|
||||
use think\queue\Job;
|
||||
use think\facade\Log;
|
||||
use think\Queue;
|
||||
use think\facade\Config;
|
||||
use app\api\controller\AllotRuleController;
|
||||
|
||||
class AutoCreateAllotRulesJob
|
||||
{
|
||||
/**
|
||||
* 队列任务处理
|
||||
* @param Job $job 队列任务
|
||||
* @param array $data 任务数据
|
||||
* @return void
|
||||
*/
|
||||
public function fire(Job $job, $data)
|
||||
{
|
||||
try {
|
||||
// 如果任务执行成功后删除任务
|
||||
if ($this->processAutoCreateAllotRules($data, $job->attempts())) {
|
||||
$job->delete();
|
||||
Log::info('自动创建分配规则任务执行成功,时间:' . date('Y-m-d H:i:s'));
|
||||
} else {
|
||||
if ($job->attempts() > 3) {
|
||||
// 超过重试次数,删除任务
|
||||
Log::error('自动创建分配规则任务执行失败,已超过重试次数');
|
||||
$job->delete();
|
||||
} else {
|
||||
// 任务失败,重新放回队列
|
||||
Log::warning('自动创建分配规则任务执行失败,重试次数:' . $job->attempts());
|
||||
$job->release(Config::get('queue.failed_delay', 10));
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// 出现异常,记录日志
|
||||
Log::error('自动创建分配规则任务异常:' . $e->getMessage());
|
||||
if ($job->attempts() > 3) {
|
||||
$job->delete();
|
||||
} else {
|
||||
$job->release(Config::get('queue.failed_delay', 10));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理自动创建分配规则
|
||||
* @param array $data 任务数据
|
||||
* @param int $attempts 重试次数
|
||||
* @return bool
|
||||
*/
|
||||
protected function processAutoCreateAllotRules($data, $attempts)
|
||||
{
|
||||
Log::info('开始执行自动创建分配规则任务,重试次数:' . $attempts);
|
||||
|
||||
// 实例化控制器
|
||||
$allotRuleController = new AllotRuleController();
|
||||
|
||||
// 调用自动创建分配规则方法
|
||||
$result = $allotRuleController->autoCreateAllotRules([], true);
|
||||
$response = json_decode($result, true);
|
||||
|
||||
// 判断是否成功
|
||||
if (isset($response['code']) && $response['code'] == 200) {
|
||||
Log::info('自动创建分配规则成功:' . json_encode($response['data']));
|
||||
return true;
|
||||
} else {
|
||||
$errorMsg = isset($response['msg']) ? $response['msg'] : '未知错误';
|
||||
Log::error('自动创建分配规则失败:' . $errorMsg);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user