func TestNextRunTime(t *testing.T) { for _, testCase := range TestCases { /* * Set up */ var job *jobfile.Job = jobfile.NewJob("JobA", "blah", "dude") timeSpec, _ := jobfile.ParseFullTimeSpec(testCase.timeSpec) job.FullTimeSpec = *timeSpec var now time.Time = testCase.startTime for _, expRunTime := range testCase.expRunTimes { fmt.Printf("time spec: %v\n", testCase.timeSpec) fmt.Printf("now: %v\n", TimeString(now)) /* * Call */ var actualRunTime *time.Time = nextRunTime(job, now) /* * Test */ require.NotNil(t, actualRunTime) msg := fmt.Sprintf("%v != %v", TimeString(expRunTime), TimeString(*actualRunTime)) require.Equal(t, expRunTime, *actualRunTime, msg) now = actualRunTime.Add(time.Second) } } }
func (jq *JobQueue) SetJobs(now time.Time, jobs []*jobfile.Job) { jq.q = make(jobQueueImpl, 0) heap.Init(&jq.q) for i := 0; i < len(jobs); i++ { var job *jobfile.Job = jobs[i] job.NextRunTime = nextRunTime(job, now) if job.NextRunTime != nil { heap.Push(&jq.q, job) } } }
func RunJob(job *jobfile.Job, ctx context.Context, shell string, testing bool) *jobfile.RunRec { rec := &jobfile.RunRec{Job: job, RunTime: time.Now()} // run var sudoResult *common.SudoResult sudoResult, err := common.Sudo(job.User, job.Cmd, shell, nil) if err != nil { /* unexpected error while trying to run job */ rec.Err = err return rec } // update run rec rec.Succeeded = sudoResult.Succeeded rec.NewStatus = jobfile.JobGood rec.Stdout = &sudoResult.Stdout rec.Stderr = &sudoResult.Stderr if !testing { // update job if sudoResult.Succeeded { /* job succeeded */ job.Status = jobfile.JobGood } else { /* job failed: apply error-handler (which sets job.Status) */ job.ErrorHandler.Apply(job) } job.LastRunTime = rec.RunTime // update rec.NewStatus rec.NewStatus = job.Status } return rec }