-
Notifications
You must be signed in to change notification settings - Fork 2.1k
command: initial batch queue cli implementation #27909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+231
−0
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e07c9a9
command: initial batch queue cli implementation
mismithhisler 1a8fad4
add copy header
mismithhisler 5165612
modern max calculation
mismithhisler 27287e9
remove leftover copy paste
mismithhisler 1f22de1
switch to using columnize and add status options
mismithhisler 5ac94aa
adds json option
mismithhisler 1f2a49d
remove delimiter
mismithhisler 0e6aef9
fix json err return
mismithhisler 5320bb5
use PerPage queryParam for limit
mismithhisler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Copyright IBM Corp. 2015, 2026 | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| package api | ||
|
|
||
| type Workload struct { | ||
| JobID string | ||
| Tenant string | ||
| Priority int | ||
| } | ||
|
|
||
| type BatchQueueStatusResponse struct { | ||
| Workloads []Workload | ||
| } | ||
|
|
||
| // BatchQueueStatus is used to query the current batch job queue. | ||
| func (j *Jobs) BatchQueueStatus(q *QueryOptions) (*BatchQueueStatusResponse, *QueryMeta, error) { | ||
|
mismithhisler marked this conversation as resolved.
Outdated
|
||
| var resp BatchQueueStatusResponse | ||
| qm, err := j.client.query("/v1/jobs/queue/status", &resp, q) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| return &resp, qm, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| // Copyright IBM Corp. 2015, 2026 | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| package command | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/hashicorp/nomad/api" | ||
| "github.com/posener/complete" | ||
| ) | ||
|
|
||
| type JobQueueCommand struct { | ||
| Meta | ||
| forceRescheduling bool | ||
|
mismithhisler marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| func (c *JobQueueCommand) Help() string { | ||
| helpText := ` | ||
| Usage: nomad job queue [options] | ||
|
|
||
| View the current status of workloads queued in a batch job queue. | ||
|
|
||
| When ACLs are enabled, this command requires a token with either TBD | ||
| capabilities. Probably at least 'list-jobs'. | ||
|
|
||
| General Options: | ||
|
|
||
| ` + generalOptionsUsage(usageOptsDefault) + ` | ||
|
|
||
| Eval Options: | ||
|
|
||
| -limit | ||
|
mismithhisler marked this conversation as resolved.
|
||
| The maximum number of workloads to return | ||
|
|
||
| -verbose | ||
| Display full output | ||
|
|
||
| ` | ||
| return strings.TrimSpace(helpText) | ||
| } | ||
|
|
||
| func (c *JobQueueCommand) Synopsis() string { | ||
| return "View the status of a batch job queue" | ||
| } | ||
|
|
||
| func (c *JobQueueCommand) AutocompleteFlags() complete.Flags { | ||
| return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), | ||
| complete.Flags{ | ||
| "-verbose": complete.PredictNothing, | ||
| "-limit": complete.PredictNothing, | ||
| }) | ||
| } | ||
|
|
||
| func (c *JobQueueCommand) AutocompleteArgs() complete.Predictor { | ||
| return JobPredictor(c.Meta.Client) | ||
| } | ||
|
|
||
| func (c *JobQueueCommand) Name() string { return "job queue" } | ||
|
|
||
| func (c *JobQueueCommand) Run(args []string) int { | ||
| var verbose bool | ||
| var limit int | ||
| flags := c.Meta.FlagSet(c.Name(), FlagSetClient) | ||
| flags.Usage = func() { c.Ui.Output(c.Help()) } | ||
| flags.BoolVar(&verbose, "verbose", false, "") | ||
| flags.IntVar(&limit, "limit", 0, "") | ||
|
|
||
| if err := flags.Parse(args); err != nil { | ||
| return 1 | ||
| } | ||
|
|
||
| // Get the HTTP client | ||
| client, err := c.Meta.Client() | ||
| if err != nil { | ||
| c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) | ||
| return 255 | ||
| } | ||
|
|
||
| // Setup the options | ||
| opts := &api.QueryOptions{} | ||
|
|
||
| if limit > 0 { | ||
| opts.Params["limit"] = fmt.Sprintf("%d", limit) | ||
| } | ||
|
|
||
| // Submit the request | ||
| resp, _, err := client.Jobs().BatchQueueStatus(opts) | ||
| if err != nil { | ||
| c.Ui.Error(fmt.Sprintf("Error during batch queue request: %s", err)) | ||
| return 255 | ||
| } | ||
|
|
||
| c.printOutput(resp) | ||
| return 0 | ||
| } | ||
|
|
||
| func (c *JobQueueCommand) printOutput(resp *api.BatchQueueStatusResponse) { | ||
| if resp == nil { | ||
| c.Ui.Error("Empty batch queue response") | ||
| } | ||
|
|
||
| headers := []string{"JobID", "Tenant", "Priority"} | ||
|
|
||
| // compute column widths | ||
| col0, col1, col2 := len(headers[0]), len(headers[1]), len(headers[2]) | ||
| for _, r := range resp.Workloads { | ||
| if len(r.JobID) > col0 { | ||
| col0 = len(r.JobID) | ||
| } | ||
| if len(r.Tenant) > col1 { | ||
| col1 = len(r.Tenant) | ||
| } | ||
| // convert int to string for width calculation | ||
| l := len(fmt.Sprintf("%d", r.Priority)) | ||
| if l > col2 { | ||
| col2 = l | ||
| } | ||
| } | ||
|
mismithhisler marked this conversation as resolved.
Outdated
|
||
|
|
||
| headerFmt := fmt.Sprintf("%%-%ds | %%-%ds | %%-%ds\n", col0, col1, col2) | ||
| rowFmt := fmt.Sprintf("%%-%ds | %%-%ds | %%%dd\n", col0, col1, col2) | ||
|
|
||
| var output strings.Builder | ||
|
|
||
| // print header | ||
| fmt.Fprintf(&output, headerFmt, headers[0], headers[1], headers[2]) | ||
|
|
||
| // print separator | ||
| fmt.Fprintf(&output, "%s-+-%s-+-%s\n", | ||
| strings.Repeat("-", col0), | ||
| strings.Repeat("-", col1), | ||
| strings.Repeat("-", col2), | ||
| ) | ||
|
|
||
| // print rows | ||
| for _, w := range resp.Workloads { | ||
| fmt.Fprintf(&output, rowFmt, w.JobID, w.Tenant, w.Priority) | ||
| } | ||
|
|
||
| c.Ui.Output(output.String()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Copyright IBM Corp. 2015, 2026 | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| package command | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/cli" | ||
| "github.com/hashicorp/nomad/api" | ||
| "github.com/hashicorp/nomad/ci" | ||
| "github.com/shoenig/test/must" | ||
| ) | ||
|
|
||
| func TestJobQueue_Implements(t *testing.T) { | ||
| ci.Parallel(t) | ||
| var _ cli.Command = &JobQueueCommand{} | ||
| } | ||
|
|
||
| func TestJobQueue_printOutput(t *testing.T) { | ||
| ci.Parallel(t) | ||
| ui := cli.NewMockUi() | ||
| cmd := &JobQueueCommand{Meta: Meta{Ui: ui}} | ||
|
|
||
| testResp := &api.BatchQueueStatusResponse{ | ||
| Workloads: []api.Workload{ | ||
| { | ||
| JobID: "123", | ||
| Tenant: "testTenant1", | ||
| Priority: 5, | ||
| }, | ||
| }, | ||
| } | ||
| cmd.printOutput(testResp) | ||
|
|
||
| expect := "JobID | Tenant | Priority\n" + | ||
| "------+-------------+---------\n" + | ||
| "123 | testTenant1 | 5\n\n" | ||
|
|
||
| must.Eq(t, expect, ui.OutputWriter.String()) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.