作者 李宇航

合并分支 'lyh-server' 到 'master'

Lyh server



查看合并请求 !3097
... ... @@ -51,12 +51,13 @@ class GeoQuestionRes extends Command
sleep(300);
continue;
}
$lock_key = "geo_task_lock:$task_id";
$lock_key = "geo_task_lock_" . $task_id;
if (!Redis::setnx($lock_key, 1)) {
$this->output("任务 $task_id 已被其他进程锁定,跳过");
sleep(30); // 程序挂起, 避免最后一个任务 扫数据表和Redis
continue;
}
Redis::expire($lock_key, 1200); // 1小时自动解锁
Redis::expire($lock_key, 600); // 10自动解锁
$this->output('执行的任务ID:' . $task_id);
$geoQuestionModel = new GeoQuestion();
$taskInfo = $geoQuestionModel->read(['id'=>$task_id]);
... ... @@ -86,6 +87,7 @@ class GeoQuestionRes extends Command
$geoResultModel = new GeoQuestionResult();
$geoLogModel = new GeoQuestionLog();
foreach ($taskInfo['question'] as $question) {
Redis::expire($lock_key, 1200); // 一个问题执行时间可能会达到15-18分钟
$en_question = Translate::tran($question, 'zh') ?? '';
$this->output('项目ID:' . $taskInfo['project_id'] . ', 问题 开始:' . $question);
foreach ($platformsArr as $platform) {
... ... @@ -345,19 +347,28 @@ class GeoQuestionRes extends Command
$key = 'geo_task_list';
$task_id = Redis::rpop($key);
if(empty($task_id)){
$project_ids = GeoQuestion::where('status', GeoQuestion::STATUS_OPEN)->where('next_time', '<=', date('Y-m-d'))
->orderBy('next_time', 'asc')->pluck('project_id')->unique()->values()->toArray();
if(!empty($project_ids)){
foreach ($project_ids as $project_id){
$ids = GeoQuestion::where(['project_id' => $project_id, 'status' => GeoQuestion::STATUS_OPEN])->where('next_time', '<=', date('Y-m-d'))->pluck('id');
foreach ($ids as $id) {
//检查任务是否执行过
if (!Redis::exists("geo_task_lock:$id")) {
Redis::lpush($key, $id);
}
}
$task_id = Redis::rpop($key);
$lock_key = 'geo_task_generation_lock';
$lock_ttl = 60; // 锁时间大于当前 锁功能执行时间
// 尝试获取锁,非阻塞方式
$lock = Redis::set($lock_key, 1, 'EX', $lock_ttl, 'NX');
if (empty($lock)){
return $task_id;
}
$project_ids = GeoQuestion::where('status', GeoQuestion::STATUS_OPEN)->where('next_time', '<=', date('Y-m-d'))->pluck('project_id')->unique()->values()->toArray();
if(FALSE == empty($project_ids)){
$ids = GeoQuestion::where('status', GeoQuestion::STATUS_OPEN)
->whereIn('project_id', $project_ids)
->where(function ($query){
$query->where('current_time', '!=', date('Y-m-d'))
->orWhereNull('current_time');
})
->orderBy('project_id', 'asc')
->pluck('id');
foreach ($ids as $id) {
Redis::lpush($key, $id);
}
$task_id = Redis::rpop($key);
}
}
return $task_id;
... ...