Skip to content

Commit 5dec37d

Browse files
authored
feat(cdsctl): cdsctl queue stopall (#5401)
* feat(cdsctl): cdsctl queue stopall Signed-off-by: Yvonnick Esnault <[email protected]>
1 parent fa02229 commit 5dec37d

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

cli/cdsctl/queue.go

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"strconv"
67
"strings"
78
"time"
89

@@ -28,6 +29,7 @@ var queueCmd = cli.Command{
2829
func queue() *cobra.Command {
2930
return cli.NewListCommand(queueCmd, queueRun, []*cobra.Command{
3031
cli.NewCommand(queueUICmd, queueUIRun, nil, withAllCommandModifiers()...),
32+
cli.NewCommand(queueStopAllCmd, queueStopAllRun, nil, withAllCommandModifiers()...),
3133
})
3234
}
3335

@@ -36,6 +38,90 @@ var queueUICmd = cli.Command{
3638
Short: "Show the current queue",
3739
}
3840

41+
var queueStopAllCmd = cli.Command{
42+
Name: "stopall",
43+
Short: "Stop all job from the queue",
44+
Example: "cdsctl queue stopall",
45+
OptionalArgs: []cli.Arg{
46+
{Name: _ProjectKey},
47+
{Name: _WorkflowName},
48+
},
49+
Flags: []cli.Flag{
50+
{
51+
Name: "force",
52+
Usage: "if true, do not ask user before stopping all workflows",
53+
IsValid: func(s string) bool {
54+
if s != "true" && s != "false" {
55+
return false
56+
}
57+
return true
58+
},
59+
Default: "false",
60+
Type: cli.FlagBool,
61+
},
62+
},
63+
}
64+
65+
func queueStopAllRun(v cli.Values) error {
66+
wantToStopAll := v.GetBool("force") || cli.AskConfirm("Are you sure to want to stop all jobs in the queue?")
67+
if !wantToStopAll {
68+
return nil
69+
}
70+
71+
jobs, err := client.QueueWorkflowNodeJobRun(sdk.StatusWaiting, sdk.StatusBuilding)
72+
if err != nil {
73+
return err
74+
}
75+
76+
var nbToStop int64
77+
for _, jr := range jobs {
78+
projectKey := getVarsInPbj("cds.project", jr.Parameters)
79+
workflowName := getVarsInPbj("cds.workflow", jr.Parameters)
80+
81+
if v.GetString(_ProjectKey) != "" && projectKey != v.GetString(_ProjectKey) {
82+
continue
83+
}
84+
if v.GetString(_WorkflowName) != "" && workflowName != v.GetString(_WorkflowName) {
85+
continue
86+
}
87+
nbToStop++
88+
}
89+
90+
wantToStopAllSure := v.GetBool("force") || cli.AskConfirm(fmt.Sprintf("There are %d worfklows to stop, confirm stopping workflows?", nbToStop))
91+
if !wantToStopAllSure {
92+
return nil
93+
}
94+
95+
var stopped int64
96+
for _, jr := range jobs {
97+
run := getVarsInPbj("cds.run.number", jr.Parameters)
98+
projectKey := getVarsInPbj("cds.project", jr.Parameters)
99+
workflowName := getVarsInPbj("cds.workflow", jr.Parameters)
100+
101+
if v.GetString(_ProjectKey) != "" && projectKey != v.GetString(_ProjectKey) {
102+
continue
103+
}
104+
if v.GetString(_WorkflowName) != "" && workflowName != v.GetString(_WorkflowName) {
105+
continue
106+
}
107+
108+
runNumber, err := strconv.ParseInt(run, 10, 64)
109+
if err != nil {
110+
return fmt.Errorf("%s invalid: not a integer for a workflow run. err: %v", run, err)
111+
}
112+
113+
w, err := client.WorkflowStop(projectKey, workflowName, runNumber)
114+
if err != nil {
115+
fmt.Printf("ERROR while stopping Workflow %s #%d: %v\n", v.GetString(_WorkflowName), w.Number, err)
116+
continue
117+
}
118+
fmt.Printf("Workflow %s #%d has been stopped\n", v.GetString(_WorkflowName), w.Number)
119+
stopped++
120+
}
121+
fmt.Printf("Nb workflows stopped: %d\n", stopped)
122+
return nil
123+
}
124+
39125
func queueRun(v cli.Values) (cli.ListResult, error) {
40126
jobList, err := getJobQueue(sdk.StatusWaiting, sdk.StatusBuilding)
41127
if err != nil {
@@ -78,7 +164,7 @@ func getJobQueue(status ...string) ([]jobCLI, error) {
78164
NodeName: getVarsInPbj("cds.node", jr.Parameters),
79165
Status: jr.Status,
80166
URL: generateQueueJobURL(baseURL, jr.Parameters),
81-
Since: fmt.Sprintf(sdk.Round(time.Since(jr.Queued), time.Second).String()),
167+
Since: sdk.Round(time.Since(jr.Queued), time.Second).String(),
82168
Duration: time.Since(jr.Queued),
83169
BookedBy: jr.BookedBy.Name,
84170
TriggeredBy: getVarsInPbj("cds.triggered_by.username", jr.Parameters),

tests/01_queue_stopall.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: Queue stop all
2+
version: "2"
3+
testcases:
4+
- name: cdsctl queue stopall
5+
steps:
6+
- script: {{.cdsctl}} -f {{.cdsctl.config}} queue stopall --force

tests/test.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ initialization_tests() {
9898
${CMD} >01_signup_user.yml.output 2>&1
9999

100100
check_failure $? 01_signup_user.yml.output
101+
102+
CMD="${VENOM} run ${VENOM_OPTS} 01_queue_stopall.yml --var cdsctl.config=${CDSCTL_CONFIG}_admin --var cdsctl=${CDSCTL} --var api.url=${CDS_API_URL}"
103+
echo -e " ${YELLOW}01_queue_stopall.yml ${DARKGRAY}[${CMD}]${NOCOLOR}"
104+
${CMD} >01_queue_stopall.yml.output 2>&1
105+
check_failure $? 01_queue_stopall.yml.output
101106
}
102107

103108
smoke_tests_services() {
@@ -146,6 +151,12 @@ workflow_with_integration_tests() {
146151
}
147152

148153
workflow_with_third_parties() {
154+
echo "Stopping all jobs in queue:"
155+
CMD="${VENOM} run ${VENOM_OPTS} 01_queue_stopall.yml --var cdsctl.config=${CDSCTL_CONFIG}_admin --var cdsctl=${CDSCTL} --var api.url=${CDS_API_URL}"
156+
echo -e " ${YELLOW}01_queue_stopall.yml ${DARKGRAY}[${CMD}]${NOCOLOR}"
157+
${CMD} >01_queue_stopall.yml.output 2>&1
158+
check_failure $? 01_queue_stopall.yml.output
159+
149160
if [ -z "$CDS_MODEL_REQ" ]; then echo "missing CDS_MODEL_REQ variable"; exit 1; fi
150161
if [ -z "$CDS_REGION_REQ" ]; then echo "missing CDS_REGION_REQ variable"; exit 1; fi
151162
echo "Running Workflow with third parties:"

0 commit comments

Comments
 (0)