-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic status checking for batch job types.
Nomad job type batch does not support the deployment model which meant previously Levant would just exit after the job was registered. This update adds additional functionality which means Levant will at least confirm the job has a status of running before exiting. Closes #53
- Loading branch information
Showing
2 changed files
with
55 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package levant | ||
|
||
import ( | ||
"time" | ||
|
||
nomad "github.com/hashicorp/nomad/api" | ||
nomadStructs "github.com/hashicorp/nomad/nomad/structs" | ||
"github.com/jrasell/levant/logging" | ||
) | ||
|
||
// checkBatchJob checks the status of a batch job at least reaches a status of | ||
// running. This is required as currently Nomad does not support deployments of | ||
// job type batch. | ||
func (c *nomadClient) checkBatchJob(jobName *string) bool { | ||
|
||
// Initialiaze our WaitIndex | ||
var wi uint64 | ||
|
||
// Setup the Nomad QueryOptions to allow blocking query and a timeout. | ||
q := &nomad.QueryOptions{WaitIndex: wi} | ||
timeout := time.Tick(time.Minute * 5) | ||
|
||
for { | ||
|
||
job, meta, err := c.nomad.Jobs().Info(*jobName, q) | ||
if err != nil { | ||
logging.Error("levant/job_status_checker: unable to query batch job %s: %v", *jobName, err) | ||
return false | ||
} | ||
|
||
// If the LastIndex is not greater than our stored LastChangeIndex, we don't | ||
// need to do anything. | ||
if meta.LastIndex <= wi { | ||
continue | ||
} | ||
|
||
if *job.Status == nomadStructs.JobStatusRunning { | ||
logging.Info("levant/job_status_checker: batch job %s has status %s", *jobName, *job.Status) | ||
return true | ||
} | ||
|
||
select { | ||
case <-timeout: | ||
logging.Error("levant/job_status_checker: timeout reached while verifying the status of batch job %s", | ||
*jobName) | ||
return false | ||
default: | ||
logging.Debug("levant/job_status_checker: batch job %s currently has status %s", *jobName, *job.Status) | ||
q.WaitIndex = meta.LastIndex | ||
continue | ||
} | ||
} | ||
} |