接口配置调整
This commit is contained in:
@@ -124,23 +124,38 @@ LUA; $redis = $this->connect();
|
||||
}
|
||||
}
|
||||
|
||||
// 任务结束时的批量操作
|
||||
public function finishJob($sRedisKey, $status, $expire)
|
||||
{
|
||||
/**
|
||||
* 标记任务状态并释放锁(带所有权验证)
|
||||
* @param string $sRedisKey 任务锁的键名
|
||||
* @param string $status 状态(completed/failed)
|
||||
* @param int $expire 状态的过期时间(秒)
|
||||
* @param string $sRedisValue 锁的值(用于验证所有权)
|
||||
* @return bool 是否执行成功
|
||||
*/
|
||||
public function finishJob($sRedisKey, $status, $expire, $sRedisValue){
|
||||
try {
|
||||
$redis = $this->connect();
|
||||
// 使用Lua脚本确保原子性
|
||||
// Lua 脚本:先验证锁所有权,再设置状态并删除锁
|
||||
$script = <<<LUA
|
||||
redis.call('SET', KEYS[1] .. ':status', ARGV[1], 'EX', ARGV[2])
|
||||
return redis.call('DEL', KEYS[1])
|
||||
if redis.call('GET', KEYS[1]) ~= ARGV[1] then
|
||||
return 0
|
||||
end
|
||||
|
||||
redis.call('SET', KEYS[1] .. ':status', ARGV[2], 'EX', ARGV[3])
|
||||
|
||||
redis.call('DEL', KEYS[1])
|
||||
|
||||
return 1
|
||||
LUA;
|
||||
return $redis->eval($script, [$sRedisKey, $status, $expire], 1) === 1;
|
||||
|
||||
// 执行脚本:参数依次为 键名列表、锁的值、状态、过期时间
|
||||
$result = $redis->eval($script, [$sRedisKey, $sRedisValue, $status, $expire], 1);
|
||||
return $result === 1;
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Redis完成任务失败: {$e->getMessage()}");
|
||||
Log::error("Redis完成任务失败: {$e->getMessage()} | 键: {$sRedisKey}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 记录处理进度
|
||||
public function recordProcessingStart($key, $totalQuestions)
|
||||
{
|
||||
@@ -163,6 +178,14 @@ LUA;
|
||||
{
|
||||
try {
|
||||
$redis = $this->connect();
|
||||
//判断是否执行
|
||||
$sStatus = $redis->hGet($key, 'status');
|
||||
if (!empty($sStatus) && $sStatus == 'completed') {
|
||||
return 2;
|
||||
}
|
||||
if (!empty($sStatus) && $sStatus == 'processing') {
|
||||
return 3;
|
||||
}
|
||||
$redis->hMSet($key, [
|
||||
'status' => 'processing',
|
||||
'total' => $totalQuestions,
|
||||
@@ -172,9 +195,9 @@ LUA;
|
||||
'queue_2_completed' => 0
|
||||
]);
|
||||
$redis->expire($key, 10800); // 6小时过期
|
||||
return true;
|
||||
return 1;
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
// 多问题按条件拆分成两个队列更新日志记录
|
||||
|
||||
Reference in New Issue
Block a user