@@ -3,6 +3,7 @@ package main
33import (
44 "context"
55 "fmt"
6+ "strconv"
67 "strings"
78 "time"
89
@@ -28,6 +29,7 @@ var queueCmd = cli.Command{
2829func 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+
39125func 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 ),
0 commit comments