fix(b28): 终态任务的结果作业不再被领取、锁停止续期
现网 FAILED 任务 32217/32220/32226/32229 的合批 job 仍在逐行申领, 在持锁续到 86 分钟。两条 claim 入口先读父任务状态:终态则拉满重试并打 终态回调,不再翻转 RUNNING;执行中由心跳复查,终态后停止看门狗续期 (不释放锁,仍由带 token 的 close 收尾)。父任务状态查询进 mapper XML, SQL 分层账本 128→129、等价账本 88→89 同步登记。
This commit is contained in:
+8
@@ -3,7 +3,15 @@ package com.nanri.aiimage.modules.task.mapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
@Mapper
|
||||
public interface TaskFileJobMapper extends BaseMapper<TaskFileJobEntity> {
|
||||
|
||||
/**
|
||||
* 只读父任务状态:结果作业的 taskId 落在 12 个客户端模块共用的 {@code biz_file_task}。
|
||||
* 品牌任务的 job 查不到行(返回 null),调用方按"非终态"处理,行为不变。
|
||||
* 语句在 {@code mapper/TaskFileJobMapper.xml}(本仓约定:自定义 SQL 一律进 XML)。
|
||||
*/
|
||||
String selectFileTaskStatus(@Param("taskId") Long taskId);
|
||||
}
|
||||
|
||||
+15
@@ -324,6 +324,21 @@ public class TaskDistributedLockService {
|
||||
this.heldLock = heldLock;
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止看门狗续期,但**不释放**锁:父任务已终态时,本节点不该再给这把锁续命;
|
||||
* 仍由 {@link #close()} 那次带 token 的 RELEASE 收尾,避免把接管者的锁误删。
|
||||
*/
|
||||
public void stopRenewal(String reason) {
|
||||
if (heldLock.closed.compareAndSet(false, true)) {
|
||||
cancelRenewal(heldLock);
|
||||
activeLocks.markRenewalLost(heldLock.lockName);
|
||||
log.warn("[task-lock] 停止续期 lockName={} heldMs={} reason={}",
|
||||
heldLock.lockName,
|
||||
System.currentTimeMillis() - heldLock.acquiredAtMillis,
|
||||
reason);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (closed) {
|
||||
|
||||
+32
@@ -221,6 +221,26 @@ public class TaskFileJobService {
|
||||
return claimCandidates(candidates);
|
||||
}
|
||||
|
||||
/**
|
||||
* 父任务是否已终态。终态任务的作业不得再被领取、锁不得再续期(B28):
|
||||
* 现网 FAILED 任务 32217/32220/32226/32229 的合批 job 仍在逐行申领,锁续到 86 分钟。
|
||||
*/
|
||||
public boolean isOwningTaskTerminal(Long taskId) {
|
||||
if (taskId == null || taskId <= 0) {
|
||||
return false;
|
||||
}
|
||||
String status = taskFileJobMapper.selectFileTaskStatus(taskId);
|
||||
return status != null && TaskStatuses.FileTask.TERMINAL.contains(status);
|
||||
}
|
||||
|
||||
/** 终态父任务的 job 直接拉满重试并打终态回调:不执行、不再被下一轮扫到。 */
|
||||
private void skipJobOfTerminalTask(TaskFileJobEntity job, String stage) {
|
||||
String message = "父任务已终态,结果作业不再执行";
|
||||
markFailedPermanent(job, message);
|
||||
log.info("[task-file-job] skipped because parent task is terminal jobId={} taskId={} moduleType={} stage={}",
|
||||
job.getId(), job.getTaskId(), job.getModuleType(), stage);
|
||||
}
|
||||
|
||||
private List<TaskFileJobEntity> claimCandidates(List<TaskFileJobEntity> candidates) {
|
||||
if (candidates == null || candidates.isEmpty()) {
|
||||
return List.of();
|
||||
@@ -230,6 +250,10 @@ public class TaskFileJobService {
|
||||
if (candidate == null || candidate.getId() == null) {
|
||||
continue;
|
||||
}
|
||||
if (isOwningTaskTerminal(candidate.getTaskId())) {
|
||||
skipJobOfTerminalTask(candidate, "CLAIM");
|
||||
continue;
|
||||
}
|
||||
if (markRunning(candidate.getId())) {
|
||||
claimedIds.add(candidate.getId());
|
||||
}
|
||||
@@ -547,6 +571,14 @@ public class TaskFileJobService {
|
||||
|
||||
/** Returns the persisted RUNNING row so queued execution can later fence itself with updatedAt. */
|
||||
public TaskFileJobEntity claimRunning(Long jobId) {
|
||||
if (jobId == null || jobId <= 0) {
|
||||
return null;
|
||||
}
|
||||
TaskFileJobEntity existing = taskFileJobMapper.selectById(jobId);
|
||||
if (existing != null && isOwningTaskTerminal(existing.getTaskId())) {
|
||||
skipJobOfTerminalTask(existing, "CLAIM_RUNNING");
|
||||
return null;
|
||||
}
|
||||
if (!markRunning(jobId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
+29
@@ -65,6 +65,10 @@ public class TaskResultFileJobWorker {
|
||||
return thread;
|
||||
});
|
||||
|
||||
/** 本进程正在执行的 job → 它持有的任务锁;心跳发现父任务终态时用它停续期(B28)。 */
|
||||
private final java.util.concurrent.ConcurrentHashMap<Long, TaskDistributedLockService.LockHandle> activeLockHandles =
|
||||
new java.util.concurrent.ConcurrentHashMap<>();
|
||||
|
||||
@Scheduled(fixedDelayString = "${aiimage.result-file-job.local-worker-delay-ms:15000}")
|
||||
public void runPendingJobs() {
|
||||
if (!localWorkerEnabled) {
|
||||
@@ -197,6 +201,14 @@ public class TaskResultFileJobWorker {
|
||||
long interval = Math.max(1000L, heartbeatIntervalMillis);
|
||||
return jobHeartbeatExecutor.scheduleWithFixedDelay(() -> {
|
||||
try {
|
||||
if (taskFileJobService.isOwningTaskTerminal(job.getTaskId())) {
|
||||
TaskDistributedLockService.LockHandle handle = activeLockHandles.get(job.getId());
|
||||
if (handle != null) {
|
||||
handle.stopRenewal("parent-task-terminal");
|
||||
log.info("[task-file-job] stopped lock renewal because parent task is terminal jobId={} taskId={} moduleType={} stage=TERMINAL_STOP_RENEW",
|
||||
job.getId(), job.getTaskId(), job.getModuleType());
|
||||
}
|
||||
}
|
||||
taskFileJobService.touchRunning(job.getId());
|
||||
} catch (Exception ex) {
|
||||
log.warn("[task-file-job] heartbeat failed jobId={} taskId={} moduleType={} msg={} stage=HEARTBEAT",
|
||||
@@ -206,6 +218,12 @@ public class TaskResultFileJobWorker {
|
||||
}
|
||||
|
||||
private void processClaimedWithHeartbeat(TaskFileJobEntity job, TaskFileJobEntity claim) {
|
||||
if (taskFileJobService.isOwningTaskTerminal(job.getTaskId())) {
|
||||
taskFileJobService.markFailedPermanent(job, "父任务已终态,结果作业不再执行");
|
||||
log.info("[task-file-job] skipped because parent task is terminal jobId={} taskId={} moduleType={} stage=TERMINAL_SKIP",
|
||||
job.getId(), job.getTaskId(), job.getModuleType());
|
||||
return;
|
||||
}
|
||||
if (!taskFileJobService.activateRunningClaim(claim)) {
|
||||
log.info("[task-file-job] queued claim expired before execution jobId={} taskId={} moduleType={} claimUpdatedAt={}",
|
||||
job.getId(), job.getTaskId(), job.getModuleType(), claim.getUpdatedAt());
|
||||
@@ -247,6 +265,15 @@ public class TaskResultFileJobWorker {
|
||||
job.getId(), job.getTaskId(), job.getModuleType(), job.getResultId());
|
||||
return;
|
||||
}
|
||||
if (taskFileJobService.isOwningTaskTerminal(job.getTaskId())) {
|
||||
try (lockHandle) {
|
||||
taskFileJobService.markFailedPermanent(job, "父任务已终态,结果作业不再执行");
|
||||
}
|
||||
log.info("[task-file-job] skipped after acquiring lock because parent task is terminal jobId={} taskId={} moduleType={} stage=TERMINAL_AFTER_LOCK",
|
||||
job.getId(), job.getTaskId(), job.getModuleType());
|
||||
return;
|
||||
}
|
||||
activeLockHandles.put(job.getId(), lockHandle);
|
||||
try (lockHandle) {
|
||||
latest = taskFileJobService.findById(job.getId());
|
||||
if (latest != null && "SUCCESS".equals(latest.getStatus())) {
|
||||
@@ -283,6 +310,8 @@ public class TaskResultFileJobWorker {
|
||||
job.getId(), job.getTaskId(), job.getModuleType(), job.getResultId(),
|
||||
System.currentTimeMillis() - startedAt, resultFileUrl);
|
||||
}
|
||||
} finally {
|
||||
activeLockHandles.remove(job.getId(), lockHandle);
|
||||
}
|
||||
// 成功后回调解耦(原 WITHDRAW 特判):时机与历史一致——锁已释放、markSuccess 之后;
|
||||
// 各模块需要"任务收尾"时实现 ResultFileJobHandler.onSuccess,task 侧不再 import 业务模块
|
||||
|
||||
Reference in New Issue
Block a user