Skip to content

Commit

Permalink
feat(jobs): add waiters (#3530)
Browse files Browse the repository at this point in the history
  • Loading branch information
Codelax authored Dec 12, 2023
1 parent bb31774 commit 7c6e297
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ARGS:

FLAGS:
-h, --help help for start
-w, --wait Wait until the job reach a stable state, use job definition timeout

GLOBAL FLAGS:
-c, --config string The path to the config file
Expand Down
1 change: 1 addition & 0 deletions cmd/scw/testdata/test-all-usage-jobs-run-usage.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ AVAILABLE COMMANDS:
get Get jobs resources
list List jobs resources
stop Stop jobs resources
wait Wait for a job run to reach a stable state

FLAGS:
-h, --help help for run
Expand Down
35 changes: 35 additions & 0 deletions cmd/scw/testdata/test-all-usage-jobs-run-wait-usage.cassette.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
version: 1
interactions:
- request:
body: '{"message":"authentication is denied","method":"api_key","reason":"not_found","type":"denied_authentication"}'
form: {}
headers:
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.21.1; linux; amd64) cli-e2e-test
url: https://api.scaleway.com/iam/v1alpha1/api-keys/SCWXXXXXXXXXXXXXXXXX
method: GET
response:
body: '{"message":"authentication is denied","method":"api_key","reason":"not_found","type":"denied_authentication"}'
headers:
Content-Length:
- "109"
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Mon, 11 Dec 2023 08:40:33 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- 535893cd-5245-4c83-876d-ee1f4f9ff93b
status: 401 Unauthorized
code: 401
duration: ""
19 changes: 19 additions & 0 deletions cmd/scw/testdata/test-all-usage-jobs-run-wait-usage.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
Wait for a job run to reach a stable state. This is similar to using --wait flag.

USAGE:
scw jobs run wait <job-run-id ...> [arg=value ...]

ARGS:
job-run-id
[region=fr-par] Region to target. If none is passed will use default region from the config (fr-par | nl-ams | pl-waw)

FLAGS:
-h, --help help for wait

GLOBAL FLAGS:
-c, --config string The path to the config file
-D, --debug Enable debug mode
-o, --output string Output format: json or human, see 'scw help output' for more info (default "human")
-p, --profile string The config profile to use
21 changes: 21 additions & 0 deletions docs/commands/jobs.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Serverless Jobs API.
- [Get jobs resources](#get-jobs-resources)
- [List jobs resources](#list-jobs-resources)
- [Stop jobs resources](#stop-jobs-resources)
- [Wait for a job run to reach a stable state](#wait-for-a-job-run-to-reach-a-stable-state)


##
Expand Down Expand Up @@ -224,3 +225,23 @@ scw jobs run stop <job-run-id ...> [arg=value ...]



### Wait for a job run to reach a stable state

Wait for a job run to reach a stable state. This is similar to using --wait flag.

**Usage:**

```
scw jobs run wait <job-run-id ...> [arg=value ...]
```


**Args:**

| Name | | Description |
|------|---|-------------|
| job-run-id | Required | |
| region | Default: `fr-par`<br />One of: `fr-par`, `nl-ams`, `pl-waw` | Region to target. If none is passed will use default region from the config |



6 changes: 6 additions & 0 deletions internal/namespaces/jobs/v1alpha1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ func GetCommands() *core.Commands {

human.RegisterMarshalerFunc(jobs.JobRunState(""), human.EnumMarshalFunc(jobRunStateMarshalSpecs))

cmds.Merge(core.NewCommands(
jobsRunWait(),
))

definitionStartBuilder(cmds.MustFind("jobs", "definition", "start"))

return cmds
}
36 changes: 36 additions & 0 deletions internal/namespaces/jobs/v1alpha1/custom_job_definition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package jobs

import (
"context"
"fmt"

"github.com/scaleway/scaleway-cli/v2/internal/core"
jobs "github.com/scaleway/scaleway-sdk-go/api/jobs/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func definitionStartBuilder(c *core.Command) *core.Command {
c.WaitUsage = "Wait until the job reach a stable state, use job definition timeout"
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
api := jobs.NewAPI(core.ExtractClient(ctx))
args := argsI.(*jobs.StartJobDefinitionRequest)
resp := respI.(*jobs.JobRun)

jobDefinition, err := api.GetJobDefinition(&jobs.GetJobDefinitionRequest{
Region: args.Region,
JobDefinitionID: args.JobDefinitionID,
}, scw.WithContext(ctx))
if err != nil {
return nil, fmt.Errorf("failed to fetch job definition for timeout: %w", err)
}

return api.WaitForJobRun(&jobs.WaitForJobRunRequest{
Region: args.Region,
JobRunID: resp.ID,
Timeout: jobDefinition.JobTimeout.ToTimeDuration(),
RetryInterval: core.DefaultRetryInterval,
})
}

return c
}
38 changes: 38 additions & 0 deletions internal/namespaces/jobs/v1alpha1/custom_job_run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package jobs

import (
"context"
"reflect"

"github.com/scaleway/scaleway-cli/v2/internal/core"
jobs "github.com/scaleway/scaleway-sdk-go/api/jobs/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func jobsRunWait() *core.Command {
return &core.Command{
Short: `Wait for a job run to reach a stable state`,
Long: `Wait for a job run to reach a stable state. This is similar to using --wait flag.`,
Namespace: "jobs",
Resource: "run",
Verb: "wait",
// Deprecated: false,
ArgsType: reflect.TypeOf(jobs.WaitForJobRunRequest{}),
ArgSpecs: core.ArgSpecs{
{
Name: "job-run-id",
Required: true,
Deprecated: false,
Positional: true,
},
core.RegionArgSpec(scw.RegionFrPar, scw.RegionNlAms, scw.RegionPlWaw),
},
Run: func(ctx context.Context, args interface{}) (i interface{}, e error) {
request := args.(*jobs.WaitForJobRunRequest)

client := core.ExtractClient(ctx)
api := jobs.NewAPI(client)
return api.WaitForJobRun(request)
},
}
}

0 comments on commit 7c6e297

Please sign in to comment.