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