Skip to content

Instantly share code, notes, and snippets.

@whatsadebugger
Created April 16, 2018 22:11
Show Gist options
  • Save whatsadebugger/18ba4b803ed1da5ce466a9ff70a6c268 to your computer and use it in GitHub Desktop.
Save whatsadebugger/18ba4b803ed1da5ce466a9ff70a6c268 to your computer and use it in GitHub Desktop.
// RunJobAt wait until the Stop() function has been called on the run
// or the specified time for the run is after the present time.
func (ot *OneTime) RunJobAt(initr models.Initiator, job models.JobSpec) {
select {
case <-ot.done:
case <-ot.Clock.After(initr.Time.DurationFromNow()):
if initr.Ran {
logger.Error((fmt.Errorf("Job runner: Initiator: %v cannot be run more than once", initr.ID)))
return
}
initr.Ran = true
_, err := BeginRun(job, initr, models.RunResult{}, ot.Store)
if err != nil {
logger.Error(err.Error())
}
}
}
func TestOneTime_RunJobAt_RunTwice(t *testing.T) {
t.Parallel()
store, cleanup := cltest.NewStore()
defer cleanup()
ot := services.OneTime{
Clock: store.Clock,
Store: store,
}
j, initr := cltest.NewJobWithRunAtInitiator(time.Now())
assert.Nil(t, store.SaveJob(&j))
ot.RunJobAt(initr, j)
initr.Time = models.Time{Time: time.Now().Add(time.Second * 2)}
ot.RunJobAt(initr, j)
jobRuns := []models.JobRun{}
assert.Nil(t, store.Where("JobID", j.ID, &jobRuns))
assert.Equal(t, 1, len(jobRuns))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment