Confirm manual task stops and protect cancellation state
This commit is contained in:
1 parent
4f2cf2fc87
commit
fb82821dc7
4 files changed
+132
-14
No files matched your search
@@ -37,7 +37,7 @@ public class TaskController {
|
||||
public List<Map<String, Object>> list(@AuthenticationPrincipal SecurityConfig.Identity who) {
|
||||
return db.queryForList(
|
||||
"SELECT t.id,t.project_id,p.name AS"
|
||||
+ " project_name,t.model_id,t.prompt,t.status,t.step_count,t.error,t.created_at,t.admin_priority,t.run_next,t.paid_priority,t.started_at,r.amount"
|
||||
+ " project_name,t.model_id,t.prompt,t.status,t.requested_action,t.step_count,t.error,t.created_at,t.admin_priority,t.run_next,t.paid_priority,t.started_at,r.amount"
|
||||
+ " AS budget,r.spent FROM ai_tasks t JOIN projects p ON p.id=t.project_id JOIN"
|
||||
+ " wallet_reservations r ON r.id=t.reservation_id WHERE t.user_id=? ORDER BY"
|
||||
+ " t.created_at DESC LIMIT 100",
|
||||
|
||||
@@ -400,6 +400,12 @@ public class TaskService {
|
||||
@Transactional
|
||||
public void finish(UUID id, String lease, String status, String error, boolean release) {
|
||||
var t = locked(id, lease);
|
||||
if ("CANCEL".equals(t.get("requested_action"))
|
||||
&& !Set.of("CANCELLED", "REVIEW_REQUIRED").contains(status)) {
|
||||
status = "CANCELLED";
|
||||
error = "用户已停止任务,已结算产生的费用";
|
||||
release = true;
|
||||
}
|
||||
db.update(
|
||||
"UPDATE ai_tasks SET"
|
||||
+ " status=?,error=?,requested_action='',lease_token=NULL,lease_expires_at=NULL,updated_at=CURRENT_TIMESTAMP"
|
||||
@@ -419,6 +425,9 @@ public class TaskService {
|
||||
find(id, user);
|
||||
var t = db.queryForMap("SELECT * FROM ai_tasks WHERE id=? FOR UPDATE", id);
|
||||
String status = t.get("status").toString();
|
||||
boolean stopping = status.equals("RUNNING") && "CANCEL".equals(t.get("requested_action"));
|
||||
if (action.equals("cancel") && (stopping || status.equals("CANCELLED"))) return find(id, user);
|
||||
if (stopping) throw new ApiError.Failure(409, "任务正在停止");
|
||||
if (action.equals("resume") && status.equals("PAUSED")) {
|
||||
db.update(
|
||||
"UPDATE ai_tasks SET status='QUEUED',error='',requested_action='',max_steps=CASE WHEN"
|
||||
@@ -430,7 +439,10 @@ public class TaskService {
|
||||
else db.update("UPDATE ai_tasks SET requested_action='PAUSE' WHERE id=?", id);
|
||||
event(id, "PAUSE_REQUESTED", "当前步骤结束后暂停");
|
||||
} else if (action.equals("cancel") && (status.equals("QUEUED") || status.equals("PAUSED"))) {
|
||||
db.update("UPDATE ai_tasks SET status='CANCELLED' WHERE id=?", id);
|
||||
db.update(
|
||||
"UPDATE ai_tasks SET status='CANCELLED',requested_action='',updated_at=CURRENT_TIMESTAMP"
|
||||
+ " WHERE id=?",
|
||||
id);
|
||||
wallets.release(UUID.fromString(t.get("reservation_id").toString()));
|
||||
queue.refundUnused(id);
|
||||
event(id, "CANCELLED", "用户取消,未使用预留已释放");
|
||||
|
||||
Reference in new issue
Block a user