-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcancel.go
More file actions
72 lines (62 loc) · 1.97 KB
/
Copy pathcancel.go
File metadata and controls
72 lines (62 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package cli
import (
"fmt"
"github.com/dvflw/mantle/internal/config"
"github.com/dvflw/mantle/internal/db"
"github.com/spf13/cobra"
)
func newCancelCommand() *cobra.Command {
return &cobra.Command{
Use: "cancel <execution-id>",
Short: "Cancel a running workflow",
Long: "Cancels a running workflow execution. The execution is marked as cancelled.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
execID := args[0]
cfg := config.FromContext(cmd.Context())
if cfg == nil {
return fmt.Errorf("config not loaded")
}
database, err := db.Open(cfg.Database)
if err != nil {
return fmt.Errorf("failed to connect to database: %w", err)
}
defer database.Close()
// Only cancel if currently pending, running, or queued.
result, err := database.ExecContext(cmd.Context(),
`UPDATE workflow_executions
SET status = 'cancelled', completed_at = NOW(), updated_at = NOW()
WHERE id = $1 AND status IN ('pending', 'running', 'queued')`,
execID,
)
if err != nil {
return fmt.Errorf("cancelling execution: %w", err)
}
rows, err := result.RowsAffected()
if err != nil {
return fmt.Errorf("checking result: %w", err)
}
if rows == 0 {
// Check if execution exists at all.
var status string
err := database.QueryRowContext(cmd.Context(),
`SELECT status FROM workflow_executions WHERE id = $1`, execID,
).Scan(&status)
if err != nil {
return fmt.Errorf("execution %q not found", execID)
}
fmt.Fprintf(cmd.OutOrStdout(), "Execution %s is already %s\n", execID, status)
return nil
}
// Also mark any running/pending steps as cancelled.
database.ExecContext(cmd.Context(),
`UPDATE step_executions
SET status = 'cancelled', completed_at = NOW(), updated_at = NOW()
WHERE execution_id = $1 AND status IN ('pending', 'running')`,
execID,
)
fmt.Fprintf(cmd.OutOrStdout(), "Cancelled execution %s\n", execID)
return nil
},
}
}