完善任务存储、权限及货源查询流程
This commit is contained in:
+73
-7
@@ -7,9 +7,12 @@ import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskScopeStateMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskScopeStateEntity;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
@@ -21,6 +24,7 @@ import java.util.Map;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class TaskScopePayloadStorageService {
|
||||
|
||||
private static final String LOCAL_BUFFER_DIR = "task-scope-buffer";
|
||||
@@ -28,8 +32,8 @@ public class TaskScopePayloadStorageService {
|
||||
private final TaskScopeStateMapper taskScopeStateMapper;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final TransientPayloadStorageService transientPayloadStorageService;
|
||||
private final PlatformTransactionManager transactionManager;
|
||||
|
||||
@Transactional
|
||||
public <T> void saveScopePayload(Long taskId, String moduleType, String scopeKey, T payload) {
|
||||
if (taskId == null || taskId <= 0 || isBlank(moduleType) || isBlank(scopeKey) || payload == null) {
|
||||
return;
|
||||
@@ -37,11 +41,60 @@ public class TaskScopePayloadStorageService {
|
||||
String normalizedScopeKey = normalize(scopeKey);
|
||||
String scopeHash = hash(normalizedScopeKey);
|
||||
String payloadJson = writeJson(payload, "写入任务范围载荷失败");
|
||||
String stateJson = transientPayloadStorageService.storeScopePayload(
|
||||
String stateJson = transientPayloadStorageService.storeScopePayloadVersioned(
|
||||
moduleType, taskId, scopeHash, payloadJson, true);
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
String replacedPayload;
|
||||
try {
|
||||
replacedPayload = inTransaction(() -> persistScopePayload(
|
||||
taskId, moduleType, normalizedScopeKey, scopeHash, stateJson));
|
||||
} catch (RuntimeException ex) {
|
||||
deleteUploadedPayloadIfUnreferenced(taskId, moduleType, scopeHash, stateJson);
|
||||
throw ex;
|
||||
}
|
||||
transientPayloadStorageService.deleteReplacedPayloadIfNeeded(replacedPayload, stateJson);
|
||||
}
|
||||
|
||||
TaskScopeStateEntity state = getScopeState(taskId, moduleType, scopeHash);
|
||||
private void deleteUploadedPayloadIfUnreferenced(Long taskId,
|
||||
String moduleType,
|
||||
String scopeHash,
|
||||
String uploadedPayload) {
|
||||
try {
|
||||
TaskScopeStateEntity currentState = getScopeState(taskId, moduleType, scopeHash);
|
||||
if (!isSafeToDelete(currentState, uploadedPayload)) {
|
||||
return;
|
||||
}
|
||||
} catch (RuntimeException ex) {
|
||||
log.warn("[task-scope-payload] skip failed-save cleanup because reference check failed taskId={} moduleType={} scopeHash={} err={}",
|
||||
taskId, moduleType, scopeHash, ex.getMessage());
|
||||
return;
|
||||
}
|
||||
transientPayloadStorageService.deletePayloadIfPresent(uploadedPayload);
|
||||
}
|
||||
|
||||
private boolean isSafeToDelete(TaskScopeStateEntity state, String payload) {
|
||||
String pointer = transientPayloadStorageService.extractPointer(payload);
|
||||
if (pointer == null) {
|
||||
return false;
|
||||
}
|
||||
if (state == null || isBlank(state.getStateJson())) {
|
||||
return true;
|
||||
}
|
||||
String currentPointer = transientPayloadStorageService.extractPointer(state.getStateJson());
|
||||
if (currentPointer == null) {
|
||||
log.warn("[task-scope-payload] skip failed-save cleanup because current state pointer is unreadable taskId={} moduleType={} scopeHash={}",
|
||||
state.getTaskId(), state.getModuleType(), state.getScopeHash());
|
||||
return false;
|
||||
}
|
||||
return !pointer.equals(currentPointer);
|
||||
}
|
||||
|
||||
private String persistScopePayload(Long taskId,
|
||||
String moduleType,
|
||||
String normalizedScopeKey,
|
||||
String scopeHash,
|
||||
String stateJson) {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
TaskScopeStateEntity state = getScopeStateForUpdate(taskId, moduleType, scopeHash);
|
||||
if (state == null) {
|
||||
TaskScopeStateEntity entity = new TaskScopeStateEntity();
|
||||
entity.setTaskId(taskId);
|
||||
@@ -58,21 +111,22 @@ public class TaskScopePayloadStorageService {
|
||||
entity.setUpdatedAt(now);
|
||||
try {
|
||||
taskScopeStateMapper.insert(entity);
|
||||
return;
|
||||
return null;
|
||||
} catch (DuplicateKeyException ignored) {
|
||||
state = getScopeState(taskId, moduleType, scopeHash);
|
||||
state = getScopeStateForUpdate(taskId, moduleType, scopeHash);
|
||||
}
|
||||
}
|
||||
if (state == null) {
|
||||
throw new BusinessException("写入任务范围载荷失败");
|
||||
}
|
||||
transientPayloadStorageService.deleteReplacedPayloadIfNeeded(state.getStateJson(), stateJson);
|
||||
String replacedPayload = state.getStateJson();
|
||||
taskScopeStateMapper.update(null, new LambdaUpdateWrapper<TaskScopeStateEntity>()
|
||||
.eq(TaskScopeStateEntity::getId, state.getId())
|
||||
.set(TaskScopeStateEntity::getScopeKey, normalizedScopeKey)
|
||||
.set(TaskScopeStateEntity::getStateJson, stateJson)
|
||||
.set(TaskScopeStateEntity::getLastChunkAt, now)
|
||||
.set(TaskScopeStateEntity::getUpdatedAt, now));
|
||||
return replacedPayload;
|
||||
}
|
||||
|
||||
public <T> T getScopePayload(Long taskId, String moduleType, String scopeKey, Class<T> clazz) {
|
||||
@@ -279,6 +333,10 @@ public class TaskScopePayloadStorageService {
|
||||
return false;
|
||||
}
|
||||
|
||||
private <T> T inTransaction(java.util.function.Supplier<T> action) {
|
||||
return new TransactionTemplate(transactionManager).execute(status -> action.get());
|
||||
}
|
||||
|
||||
public Path getBufferedPayloadRoot() {
|
||||
return Path.of(System.getProperty("java.io.tmpdir"), LOCAL_BUFFER_DIR);
|
||||
}
|
||||
@@ -291,6 +349,14 @@ public class TaskScopePayloadStorageService {
|
||||
.last("limit 1"));
|
||||
}
|
||||
|
||||
private TaskScopeStateEntity getScopeStateForUpdate(Long taskId, String moduleType, String scopeHash) {
|
||||
return taskScopeStateMapper.selectOne(new LambdaQueryWrapper<TaskScopeStateEntity>()
|
||||
.eq(TaskScopeStateEntity::getTaskId, taskId)
|
||||
.eq(TaskScopeStateEntity::getModuleType, moduleType)
|
||||
.eq(TaskScopeStateEntity::getScopeHash, scopeHash)
|
||||
.last("limit 1 FOR UPDATE"));
|
||||
}
|
||||
|
||||
private String resolveStatePayload(TaskScopeStateEntity state) {
|
||||
if (state == null || isBlank(state.getStateJson())) {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user