Merge remote-tracking branch 'origin/main'
This commit is contained in:
@@ -20,6 +20,25 @@ type TaskCancelResult struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func DescribeTaskCancellation(task store.GatewayTask) TaskCancelResult {
|
||||
if taskCancelTerminalStatus(task.Status) {
|
||||
return taskCancelUnavailable(task, "任务已结束,无法取消")
|
||||
}
|
||||
if strings.TrimSpace(task.RemoteTaskID) != "" {
|
||||
return taskCancelUnavailable(task, "任务已提交上游,当前不可取消,请继续查询结果")
|
||||
}
|
||||
if strings.TrimSpace(task.Status) != "queued" {
|
||||
return taskCancelUnavailable(task, "任务已开始执行,当前阶段不可取消,请继续查询结果")
|
||||
}
|
||||
return TaskCancelResult{
|
||||
TaskID: task.ID,
|
||||
Cancelled: false,
|
||||
Cancellable: true,
|
||||
Submitted: false,
|
||||
Message: "任务仍在本地队列中,可取消",
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) CancelTask(ctx context.Context, taskID string, user *auth.User) (TaskCancelResult, error) {
|
||||
task, err := s.store.GetTask(ctx, taskID)
|
||||
if err != nil {
|
||||
|
||||
@@ -76,3 +76,26 @@ func TestTaskCancelUnavailableReportsSubmittedFromRemoteTaskID(t *testing.T) {
|
||||
t.Fatalf("remote task id should report submitted: %+v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDescribeTaskCancellationReportsQueuedTaskAsCancellable(t *testing.T) {
|
||||
result := DescribeTaskCancellation(store.GatewayTask{ID: "task-queued", Status: "queued"})
|
||||
|
||||
if result.TaskID != "task-queued" {
|
||||
t.Fatalf("unexpected task id: %+v", result)
|
||||
}
|
||||
if result.Cancelled || !result.Cancellable || result.Submitted {
|
||||
t.Fatalf("queued local task should be cancellable and not submitted: %+v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDescribeTaskCancellationReportsRemoteTaskAsSubmitted(t *testing.T) {
|
||||
result := DescribeTaskCancellation(store.GatewayTask{
|
||||
ID: "task-remote",
|
||||
Status: "running",
|
||||
RemoteTaskID: "remote-1",
|
||||
})
|
||||
|
||||
if result.Cancelled || result.Cancellable || !result.Submitted {
|
||||
t.Fatalf("remote task should be submitted and non-cancellable: %+v", result)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user