periodic status

This commit is contained in:
Alex Dadgar 2016-01-07 14:24:25 -08:00
parent a0e52a406a
commit ce5a7b73ed
3 changed files with 90 additions and 49 deletions

View file

@ -151,21 +151,64 @@ func (c *StatusCommand) Run(args []string) int {
c.Ui.Output(formatKV(basic)) c.Ui.Output(formatKV(basic))
if !periodic && !short { // Exit early
if short {
return 0
}
// Print periodic job information
if periodic {
if err := c.outputPeriodicInfo(client, job); err != nil {
c.Ui.Error(err.Error())
return 1
}
return 0
}
if err := c.outputJobInfo(client, job); err != nil {
c.Ui.Error(err.Error())
return 1
}
return 0
}
func (c *StatusCommand) outputPeriodicInfo(client *api.Client, job *api.Job) error {
// Generate the prefix that matches launched jobs from the periodic job.
prefix := fmt.Sprintf("%s%s", job.ID, structs.PeriodicLaunchSuffix)
jobs, _, err := client.Jobs().PrefixList(prefix)
if err != nil {
return fmt.Errorf("Error querying job: %s", err)
}
out := make([]string, len(jobs)+1)
out[0] = "ID|Type|Priority|Status"
for i, job := range jobs {
out[i+1] = fmt.Sprintf("%s|%s|%d|%s",
job.ID,
job.Type,
job.Priority,
job.Status)
}
c.Ui.Output(fmt.Sprintf("\nPreviously launched jobs\n%s", formatList(out)))
return nil
}
func (c *StatusCommand) outputJobInfo(client *api.Client, job *api.Job) error {
var evals, allocs []string var evals, allocs []string
// Query the evaluations // Query the evaluations
jobEvals, _, err := client.Jobs().Evaluations(job.ID, nil) jobEvals, _, err := client.Jobs().Evaluations(job.ID, nil)
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying job evaluations: %s", err)) return fmt.Errorf("Error querying job evaluations: %s", err)
return 1
} }
// Query the allocations // Query the allocations
jobAllocs, _, err := client.Jobs().Allocations(job.ID, nil) jobAllocs, _, err := client.Jobs().Allocations(job.ID, nil)
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying job allocations: %s", err)) return fmt.Errorf("Error querying job allocations: %s", err)
return 1
} }
// Format the evals // Format the evals
@ -196,9 +239,7 @@ func (c *StatusCommand) Run(args []string) int {
c.Ui.Output(formatList(evals)) c.Ui.Output(formatList(evals))
c.Ui.Output("\n==> Allocations") c.Ui.Output("\n==> Allocations")
c.Ui.Output(formatList(allocs)) c.Ui.Output(formatList(allocs))
} return nil
return 0
} }
// convertApiJob is used to take a *api.Job and convert it to an *struct.Job. // convertApiJob is used to take a *api.Job and convert it to an *struct.Job.

View file

@ -12,12 +12,6 @@ import (
"github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/nomad/structs"
) )
const (
// The string appended to the periodic jobs ID when launching derived
// instances of it.
JobLaunchSuffix = "/periodic-"
)
// PeriodicDispatch is used to track and launch periodic jobs. It maintains the // PeriodicDispatch is used to track and launch periodic jobs. It maintains the
// set of periodic jobs and creates derived jobs and evaluations per // set of periodic jobs and creates derived jobs and evaluations per
// instantiation which is determined by the periodic spec. // instantiation which is determined by the periodic spec.
@ -418,18 +412,18 @@ func (p *PeriodicDispatch) deriveJob(periodicJob *structs.Job, time time.Time) (
// deriveJobID returns a job ID based on the parent periodic job and the launch // deriveJobID returns a job ID based on the parent periodic job and the launch
// time. // time.
func (p *PeriodicDispatch) derivedJobID(periodicJob *structs.Job, time time.Time) string { func (p *PeriodicDispatch) derivedJobID(periodicJob *structs.Job, time time.Time) string {
return fmt.Sprintf("%s%s%d", periodicJob.ID, JobLaunchSuffix, time.Unix()) return fmt.Sprintf("%s%s%d", periodicJob.ID, structs.PeriodicLaunchSuffix, time.Unix())
} }
// LaunchTime returns the launch time of the job. This is only valid for // LaunchTime returns the launch time of the job. This is only valid for
// jobs created by PeriodicDispatch and will otherwise return an error. // jobs created by PeriodicDispatch and will otherwise return an error.
func (p *PeriodicDispatch) LaunchTime(jobID string) (time.Time, error) { func (p *PeriodicDispatch) LaunchTime(jobID string) (time.Time, error) {
index := strings.LastIndex(jobID, JobLaunchSuffix) index := strings.LastIndex(jobID, structs.PeriodicLaunchSuffix)
if index == -1 { if index == -1 {
return time.Time{}, fmt.Errorf("couldn't parse launch time from eval: %v", jobID) return time.Time{}, fmt.Errorf("couldn't parse launch time from eval: %v", jobID)
} }
launch, err := strconv.Atoi(jobID[index+len(JobLaunchSuffix):]) launch, err := strconv.Atoi(jobID[index+len(structs.PeriodicLaunchSuffix):])
if err != nil { if err != nil {
return time.Time{}, fmt.Errorf("couldn't parse launch time from eval: %v", jobID) return time.Time{}, fmt.Errorf("couldn't parse launch time from eval: %v", jobID)
} }

View file

@ -1023,6 +1023,12 @@ func (p *PeriodicConfig) Next(fromTime time.Time) time.Time {
return time.Time{} return time.Time{}
} }
const (
// PeriodicLaunchSuffix is the string appended to the periodic jobs ID
// when launching derived instances of it.
PeriodicLaunchSuffix = "/periodic-"
)
// PeriodicLaunch tracks the last launch time of a periodic job. // PeriodicLaunch tracks the last launch time of a periodic job.
type PeriodicLaunch struct { type PeriodicLaunch struct {
ID string // ID of the periodic job. ID string // ID of the periodic job.