Skip to content

Commit 1abd257

Browse files
authored
update cli (#21)
* add stop worker functionality * update readme with stop worker functionality * add stop and graceful stop worker to cli
1 parent af83c95 commit 1abd257

2 files changed

Lines changed: 18 additions & 33 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The job table contains only queued, scheduled and running tasks. The ended jobs
1414

1515
---
1616

17-
# 🛠️ Installation
17+
## 🛠️ Installation
1818

1919
To integrate the queuer package into your Go project, use the standard go get command:
2020

cli/cmd/cancel/cancelWorker.go

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type WorkerCommand struct {
1313
Cmd *cobra.Command
1414
RootFlags *model.RootFlags
1515
CancelFlags *CancelFlags
16+
Graceful bool
1617
}
1718

1819
func AddWorkerCommand(cmd *cobra.Command, cancelFlags *CancelFlags, rootFlags *model.RootFlags) {
@@ -23,20 +24,21 @@ func AddWorkerCommand(cmd *cobra.Command, cancelFlags *CancelFlags, rootFlags *m
2324
Long: `Cancel/shut down a worker using its RID (Resource ID).
2425
2526
This command will:
26-
- Signal the worker to stop accepting new jobs
27-
- Allow currently running jobs to complete
27+
- Allow currently running jobs to complete with graceful shutdown
2828
- Shut down the worker process
29-
- Update the worker status to offline
30-
- Cancel any jobs assigned to this worker that haven't started`,
29+
- Signal the worker to stop accepting new jobs`,
3130
Example: helper.FormatCmdExamples("cancel worker", []helper.CmdExample{
3231
{Cmd: `--rid "123e4567-e89b-12d3-a456-426614174000"`, Description: "Cancel worker by RID"},
32+
{Cmd: `--rid "123e4567-e89b-12d3-a456-426614174000" --graceful`, Description: "Gracefully cancel worker by RID allowing current jobs to complete"},
3333
}),
3434
},
3535
RootFlags: rootFlags,
3636
CancelFlags: cancelFlags,
3737
}
3838
workerCmd.Cmd.Run = workerCmd.RunWorkerCommand
3939

40+
workerCmd.Cmd.PersistentFlags().BoolVarP(&workerCmd.Graceful, "graceful", "g", false, "Allow currently running jobs to complete with graceful shutdown")
41+
4042
cmd.AddCommand(workerCmd.Cmd)
4143
}
4244

@@ -52,39 +54,22 @@ func (r *WorkerCommand) RunWorkerCommand(cmd *cobra.Command, args []string) {
5254
return
5355
}
5456

55-
// First, get the worker to verify it exists
56-
worker, err := r.RootFlags.QueuerInstance.GetWorker(parsedID)
57-
if err != nil {
58-
fmt.Printf("Error retrieving worker: %v\n", err)
59-
return
60-
}
61-
if worker == nil {
62-
fmt.Printf("No worker found with ID: %s\n", r.CancelFlags.RID)
57+
if r.Graceful {
58+
err = r.RootFlags.QueuerInstance.StopWorkerGracefully(parsedID)
59+
if err != nil {
60+
fmt.Printf("Error retrieving worker: %v\n", err)
61+
return
62+
}
63+
fmt.Printf("Successfully initiated graceful shutdown for worker: %s\n", r.CancelFlags.RID)
64+
fmt.Println("The worker will finish current jobs before shutting down.")
6365
return
6466
}
6567

66-
// Check if worker is already offline
67-
if worker.Status == "OFFLINE" {
68-
fmt.Printf("Worker %s is already offline\n", r.CancelFlags.RID)
69-
return
70-
}
71-
72-
// Cancel all jobs assigned to this worker
73-
err = r.RootFlags.QueuerInstance.CancelAllJobsByWorker(parsedID, 100) // Cancel up to 100 jobs
68+
err = r.RootFlags.QueuerInstance.StopWorker(parsedID)
7469
if err != nil {
75-
fmt.Printf("Error cancelling jobs for worker: %v\n", err)
70+
fmt.Printf("Error retrieving worker: %v\n", err)
7671
return
7772
}
78-
7973
fmt.Printf("Successfully initiated shutdown for worker: %s\n", r.CancelFlags.RID)
80-
fmt.Println("All assigned jobs have been cancelled.")
81-
82-
// Display the worker details
83-
helper.PrintTabbedLines([]helper.TabLine{
84-
{Key: "ID", Value: worker.ID},
85-
{Key: "RID", Value: worker.RID.String()},
86-
{Key: "Status", Value: worker.Status},
87-
{Key: "Last update", Value: worker.UpdatedAt.Format("2006-01-02 15:04:05")},
88-
{Key: "Created", Value: worker.CreatedAt.Format("2006-01-02 15:04:05")},
89-
})
74+
fmt.Println("All assigned jobs will be cancelled.")
9075
}

0 commit comments

Comments
 (0)