Skip to content

Commit

Permalink
Allow specification of a custom job name/prefix for parameterized jobs (
Browse files Browse the repository at this point in the history
  • Loading branch information
geovannyAvelar committed Oct 6, 2022
1 parent a1c4c8b commit 2b9158b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .changelog/14631.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
cli: Added `-id-prefix-template` option to `nomad job dispatch`
```
16 changes: 9 additions & 7 deletions api/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,13 @@ func (j *Jobs) Summary(jobID string, q *QueryOptions) (*JobSummary, *QueryMeta,
}

func (j *Jobs) Dispatch(jobID string, meta map[string]string,
payload []byte, q *WriteOptions) (*JobDispatchResponse, *WriteMeta, error) {
payload []byte, idPrefixTemplate string, q *WriteOptions) (*JobDispatchResponse, *WriteMeta, error) {
var resp JobDispatchResponse
req := &JobDispatchRequest{
JobID: jobID,
Meta: meta,
Payload: payload,
JobID: jobID,
Meta: meta,
Payload: payload,
IdPrefixTemplate: idPrefixTemplate,
}
wm, err := j.client.write("/v1/job/"+url.PathEscape(jobID)+"/dispatch", req, &resp, q)
if err != nil {
Expand Down Expand Up @@ -1342,9 +1343,10 @@ type DesiredUpdates struct {
}

type JobDispatchRequest struct {
JobID string
Payload []byte
Meta map[string]string
JobID string
Payload []byte
Meta map[string]string
IdPrefixTemplate string
}

type JobDispatchResponse struct {
Expand Down
7 changes: 6 additions & 1 deletion command/job_dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ Dispatch Options:
Optional identifier used to prevent more than one instance of the job from
being dispatched.
-id-prefix-template
Optional prefix template for dispatched job IDs.
-verbose
Display full information.
`
Expand Down Expand Up @@ -107,13 +110,15 @@ func (c *JobDispatchCommand) Run(args []string) int {
var detach, verbose bool
var idempotencyToken string
var meta []string
var idPrefixTemplate string

flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&detach, "detach", false, "")
flags.BoolVar(&verbose, "verbose", false, "")
flags.StringVar(&idempotencyToken, "idempotency-token", "", "")
flags.Var((*flaghelper.StringFlag)(&meta), "meta", "")
flags.StringVar(&idPrefixTemplate, "id-prefix-template", "", "")

if err := flags.Parse(args); err != nil {
return 1
Expand Down Expand Up @@ -174,7 +179,7 @@ func (c *JobDispatchCommand) Run(args []string) int {
w := &api.WriteOptions{
IdempotencyToken: idempotencyToken,
}
resp, _, err := client.Jobs().Dispatch(job, metaMap, payload, w)
resp, _, err := client.Jobs().Dispatch(job, metaMap, payload, idPrefixTemplate, w)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to dispatch job: %s", err))
return 1
Expand Down
2 changes: 1 addition & 1 deletion e2e/scheduler_sysbatch/sysbatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (tc *SysBatchSchedulerTest) TestJobRunDispatch(f *framework.F) {
jobs := nomadClient.Jobs()
result, _, err := jobs.Dispatch(jobID, map[string]string{
"KEY": "value",
}, nil, nil)
}, nil, "", nil)
require.NoError(t, err)

// grab the new dispatched jobID
Expand Down
2 changes: 1 addition & 1 deletion nomad/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,7 @@ func (j *Job) Dispatch(args *structs.JobDispatchRequest, reply *structs.JobDispa

// Derive the child job and commit it via Raft - with initial status
dispatchJob := parameterizedJob.Copy()
dispatchJob.ID = structs.DispatchedID(parameterizedJob.ID, time.Now())
dispatchJob.ID = structs.DispatchedID(parameterizedJob.ID, args.IdPrefixTemplate, time.Now())
dispatchJob.ParentID = parameterizedJob.ID
dispatchJob.Name = dispatchJob.ID
dispatchJob.SetSubmitTime()
Expand Down
8 changes: 7 additions & 1 deletion nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ type JobDispatchRequest struct {
Payload []byte
Meta map[string]string
WriteRequest
IdPrefixTemplate string
}

// JobValidateRequest is used to validate a job
Expand Down Expand Up @@ -5367,8 +5368,13 @@ func (d *ParameterizedJobConfig) Copy() *ParameterizedJobConfig {

// DispatchedID returns an ID appropriate for a job dispatched against a
// particular parameterized job
func DispatchedID(templateID string, t time.Time) string {
func DispatchedID(templateID, idPrefixTemplate string, t time.Time) string {
u := uuid.Generate()[:8]

if idPrefixTemplate != "" {
return fmt.Sprintf("%s%s%s-%d-%s", templateID, DispatchLaunchSuffix, idPrefixTemplate, t.Unix(), u)
}

return fmt.Sprintf("%s%s%d-%s", templateID, DispatchLaunchSuffix, t.Unix(), u)
}

Expand Down
18 changes: 18 additions & 0 deletions website/content/docs/commands/job/dispatch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ dispatching parameterized jobs.
- `-idempotency-token`: Optional identifier used to prevent more than one
instance of the job from being dispatched.


- `-id-prefix-template`: Optional prefix template for dispatched job IDs.

- `-verbose`: Show full information.

## Examples
Expand Down Expand Up @@ -141,6 +144,21 @@ $ nomad job dispatch -idempotency-token=prod video-encode video-config.json
Job "video-encode/dispatch-1485379325-cb38d00d" already dispatched with idempotency token "prod".
```

Dispatch with an id prefix:

```shell-session
$ nomad job dispatch -id-prefix-template=config1 video-encode video-config1.json
Jb
Dispatched Job ID = video-encode/dispatch-config1-1485379325-cb38d00d
Evaluation ID = 31199841
==> Monitoring evaluation "31199841"
Evaluation triggered by job "example/dispatch-config1-1485379325-cb38d00d"
Allocation "8254b85f" created: node "82ff9c50", group "cache"
Evaluation status changed: "pending" -> "complete"
==> Evaluation "31199841" finished with status "complete"
```

[eval status]: /docs/commands/eval/status
[parameterized job]: /docs/job-specification/parameterized 'Nomad parameterized Job Specification'
[multiregion]: /docs/job-specification/multiregion#parameterized-dispatch

0 comments on commit 2b9158b

Please sign in to comment.