Skip to content

Commit

Permalink
review fixes 3
Browse files Browse the repository at this point in the history
  • Loading branch information
aneustroev committed Jul 28, 2023
1 parent f66becd commit 9210256
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .changelog/17858.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
```release-note:feature
jobspec: Add 'crons' fileld for multiple `cron` entity's
jobspec: Add 'crons' fileld for multiple `cron` expressions
```
15 changes: 5 additions & 10 deletions api/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,27 +855,22 @@ func (p *PeriodicConfig) Canonicalize() {
// returned. The `time.Location` of the returned value matches that of the
// passed time.
func (p *PeriodicConfig) Next(fromTime time.Time) (time.Time, error) {
// Once spec parsing
// Single spec parsing
if p != nil && *p.SpecType == PeriodicSpecCron {
if p.Spec != nil && *p.Spec != "" {
return cronParseNext(fromTime, *p.Spec)
}
}

// multiple specs parsing
times := make([]time.Time, len(p.Specs))
var nextTime time.Time
var err error
for i, spec := range p.Specs {
times[i], err = cronParseNext(fromTime, spec)
for _, spec := range p.Specs {
t, err := cronParseNext(fromTime, spec)
if err != nil {
return time.Time{}, fmt.Errorf("failed parsing cron expression %s: %v", spec, err)
}
}
nextTime = times[0]
for _, next := range times {
if next.Before(nextTime) {
nextTime = next
if nextTime.IsZero() || t.Before(nextTime) {
nextTime = t
}
}
return nextTime, nil
Expand Down
23 changes: 12 additions & 11 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4797,6 +4797,12 @@ func (j *Job) Warnings() error {
mErr.Errors = append(mErr.Errors, err)
}

// cron -> crons
if j.Periodic.Spec != "" {
err := fmt.Errorf("cron is deprecated and may be removed in a future release. Use crons instead")
mErr.Errors = append(mErr.Errors, err)
}

return mErr.ErrorOrNil()
}

Expand Down Expand Up @@ -5685,25 +5691,20 @@ func CronParseNext(fromTime time.Time, spec string) (t time.Time, err error) {
func (p *PeriodicConfig) Next(fromTime time.Time) (time.Time, error) {
switch p.SpecType {
case PeriodicSpecCron:
// Once spec parsing
// Single spec parsing
if p.Spec != "" {
return CronParseNext(fromTime, p.Spec)
}

// multiple specs parsing
times := make([]time.Time, len(p.Specs))
var nextTime time.Time
var err error
for i, spec := range p.Specs {
times[i], err = CronParseNext(fromTime, spec)
for _, spec := range p.Specs {
t, err := CronParseNext(fromTime, spec)
if err != nil {
return times[i], err
return time.Time{}, fmt.Errorf("failed parsing cron expression %s: %v", spec, err)
}
}
nextTime = times[0]
for _, next := range times {
if next.Before(nextTime) {
nextTime = next
if nextTime.IsZero() || t.Before(nextTime) {
nextTime = t
}
}
return nextTime, nil
Expand Down
3 changes: 2 additions & 1 deletion website/content/api-docs/json-jobs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,8 @@ The `Job` object supports the following keys:
documentation of supported cron specs and the predefined expressions.

- `Specs` - A cron expressions configuring the interval the job is launched
at. Like `Spec`, but support multiple entity.
at. Like `Spec`, but support multiple expressions. The job will
run at the next time that matches any of the expressions.

- <a id="prohibit_overlap">`ProhibitOverlap`</a> - `ProhibitOverlap` can be set
to true to enforce that the periodic job doesn't spawn a new instance of the
Expand Down
3 changes: 2 additions & 1 deletion website/content/docs/job-specification/periodic.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ consistent evaluation when Nomad spans multiple time zones.
option also includes predefined expressions such as `@daily` or `@weekly`.

- `crons` - A cron expressions configuring the interval the job is launched
at. Like `cron`, but support multiple entity.
at. Like `cron`, but support expressions. The job will run at the next
time that matches any of the expressions.

- `prohibit_overlap` `(bool: false)` - Specifies if this job should wait until
previous instances of this job have completed. This only applies to this job;
Expand Down

0 comments on commit 9210256

Please sign in to comment.