Commit 15623e52 authored by Alex Dadgar's avatar Alex Dadgar
Browse files

Fix command line

parent 67df44cd
Showing with 20 additions and 13 deletions
+20 -13
......@@ -9,6 +9,7 @@ import (
"github.com/gorhill/cronexpr"
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/nomad/structs"
)
const (
......@@ -537,14 +538,14 @@ func (p *PeriodicConfig) Canonicalize() {
// passed time. If no matching instance exists, the zero value of time.Time is
// returned. The `time.Location` of the returned value matches that of the
// passed time.
func (p *PeriodicConfig) Next(fromTime time.Time) time.Time {
func (p *PeriodicConfig) Next(fromTime time.Time) (time.Time, error) {
if *p.SpecType == PeriodicSpecCron {
if e, err := cronexpr.Parse(*p.Spec); err == nil {
return e.Next(fromTime)
return structs.CronParseNext(e, fromTime, *p.Spec)
}
}
return time.Time{}
return time.Time{}, nil
}
func (p *PeriodicConfig) GetLocation() (*time.Location, error) {
......
......@@ -247,9 +247,13 @@ func (c *JobRunCommand) Run(args []string) int {
loc, err := job.Periodic.GetLocation()
if err == nil {
now := time.Now().In(loc)
next := job.Periodic.Next(now)
c.Ui.Output(fmt.Sprintf("Approximate next launch time: %s (%s from now)",
formatTime(next), formatTimeDifference(now, next, time.Second)))
next, err := job.Periodic.Next(now)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error determining next launch time: %v", err))
} else {
c.Ui.Output(fmt.Sprintf("Approximate next launch time: %s (%s from now)",
formatTime(next), formatTimeDifference(now, next, time.Second)))
}
}
} else if !paramjob {
c.Ui.Output("Evaluation ID: " + evalID)
......
......@@ -185,10 +185,12 @@ func (c *JobStatusCommand) Run(args []string) int {
location, err := job.Periodic.GetLocation()
if err == nil {
now := time.Now().In(location)
next := job.Periodic.Next(now)
basic = append(basic, fmt.Sprintf("Next Periodic Launch|%s",
fmt.Sprintf("%s (%s from now)",
formatTime(next), formatTimeDifference(now, next, time.Second))))
next, err := job.Periodic.Next(now)
if err == nil {
basic = append(basic, fmt.Sprintf("Next Periodic Launch|%s",
fmt.Sprintf("%s (%s from now)",
formatTime(next), formatTimeDifference(now, next, time.Second))))
}
}
}
}
......
......@@ -2657,9 +2657,9 @@ func (p *PeriodicConfig) Canonicalize() {
p.location = l
}
// cronParseNext is a helper that parses the next time for the given expression
// CronParseNext is a helper that parses the next time for the given expression
// but captures any panic that may occur in the underlying library.
func cronParseNext(e *cronexpr.Expression, fromTime time.Time, spec string) (t time.Time, err error) {
func CronParseNext(e *cronexpr.Expression, fromTime time.Time, spec string) (t time.Time, err error) {
defer func() {
if recover() != nil {
t = time.Time{}
......@@ -2678,7 +2678,7 @@ func (p *PeriodicConfig) Next(fromTime time.Time) (time.Time, error) {
switch p.SpecType {
case PeriodicSpecCron:
if e, err := cronexpr.Parse(p.Spec); err == nil {
return cronParseNext(e, fromTime, p.Spec)
return CronParseNext(e, fromTime, p.Spec)
}
case PeriodicSpecTest:
split := strings.Split(p.Spec, ",")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment