2017-04-29 18:26:43 +02:00
|
|
|
package jobrun
|
|
|
|
|
|
|
|
import (
|
2017-05-03 18:13:50 +02:00
|
|
|
"fmt"
|
2017-04-29 18:26:43 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-05-03 17:48:29 +02:00
|
|
|
type Logger interface {
|
|
|
|
Printf(format string, v ...interface{})
|
|
|
|
}
|
|
|
|
|
2017-05-03 18:13:50 +02:00
|
|
|
type jobLogger struct {
|
|
|
|
MainLog Logger
|
|
|
|
JobName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l jobLogger) Printf(format string, v ...interface{}) {
|
|
|
|
l.MainLog.Printf(fmt.Sprintf("job[%s]: %s", l.JobName, format), v...)
|
|
|
|
}
|
|
|
|
|
2017-04-29 18:26:43 +02:00
|
|
|
type Job struct {
|
2017-06-09 21:00:28 +02:00
|
|
|
Name string
|
|
|
|
RunFunc func(log Logger) (err error)
|
|
|
|
LastStart time.Time
|
|
|
|
LastError error
|
|
|
|
DueAt time.Time
|
|
|
|
RepeatStrategy RepeatStrategy
|
|
|
|
}
|
|
|
|
|
|
|
|
type JobRunResult struct {
|
|
|
|
Start time.Time
|
|
|
|
Finish time.Time
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r JobRunResult) RunTime() time.Duration { return r.Finish.Sub(r.Start) }
|
|
|
|
|
|
|
|
type RepeatStrategy interface {
|
|
|
|
ShouldReschedule(lastResult JobRunResult) (nextDue time.Time, reschedule bool)
|
2017-04-29 18:26:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type JobRunner struct {
|
2017-05-03 17:48:29 +02:00
|
|
|
logger Logger
|
2017-04-30 23:30:30 +02:00
|
|
|
notificationChan chan Job
|
|
|
|
newJobChan chan Job
|
|
|
|
finishedJobChan chan Job
|
|
|
|
scheduleTimer <-chan time.Time
|
|
|
|
pending map[string]Job
|
|
|
|
running map[string]Job
|
2017-04-29 18:26:43 +02:00
|
|
|
}
|
|
|
|
|
2017-05-03 17:48:29 +02:00
|
|
|
func NewJobRunner(logger Logger) *JobRunner {
|
2017-04-29 18:26:43 +02:00
|
|
|
return &JobRunner{
|
2017-05-03 17:48:29 +02:00
|
|
|
logger: logger,
|
2017-04-30 23:30:30 +02:00
|
|
|
notificationChan: make(chan Job),
|
|
|
|
newJobChan: make(chan Job),
|
|
|
|
finishedJobChan: make(chan Job),
|
|
|
|
pending: make(map[string]Job),
|
|
|
|
running: make(map[string]Job),
|
2017-04-29 18:26:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *JobRunner) AddJobChan() chan<- Job {
|
|
|
|
return r.newJobChan
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *JobRunner) AddJob(j Job) {
|
|
|
|
r.newJobChan <- j
|
|
|
|
}
|
|
|
|
|
2017-04-30 23:30:30 +02:00
|
|
|
func (r *JobRunner) NotificationChan() <-chan Job {
|
|
|
|
return r.notificationChan
|
|
|
|
}
|
|
|
|
|
2017-04-29 18:26:43 +02:00
|
|
|
func (r *JobRunner) Start() {
|
|
|
|
|
|
|
|
loop:
|
|
|
|
select {
|
|
|
|
|
|
|
|
case newJob := <-r.newJobChan:
|
|
|
|
|
|
|
|
_, jobPending := r.pending[newJob.Name]
|
|
|
|
_, jobRunning := r.running[newJob.Name]
|
|
|
|
|
|
|
|
if jobPending || jobRunning {
|
|
|
|
panic("job already in runner")
|
|
|
|
}
|
|
|
|
|
|
|
|
r.pending[newJob.Name] = newJob
|
|
|
|
|
|
|
|
case finishedJob := <-r.finishedJobChan:
|
|
|
|
|
2017-06-09 21:00:28 +02:00
|
|
|
delete(r.running, finishedJob.Name)
|
2017-04-29 18:26:43 +02:00
|
|
|
|
2017-06-09 21:00:28 +02:00
|
|
|
res := JobRunResult{
|
|
|
|
Start: finishedJob.LastStart,
|
|
|
|
Finish: time.Now(),
|
|
|
|
Error: finishedJob.LastError,
|
2017-04-29 18:26:43 +02:00
|
|
|
}
|
|
|
|
|
2017-06-09 21:00:28 +02:00
|
|
|
r.logger.Printf("[%s] finished after %s\n", finishedJob.Name, res.RunTime())
|
|
|
|
|
|
|
|
dueTime, resched := finishedJob.RepeatStrategy.ShouldReschedule(res)
|
|
|
|
if resched {
|
2017-07-01 20:32:56 +02:00
|
|
|
r.logger.Printf("[%s] rescheduling to %s", finishedJob.Name, dueTime)
|
2017-06-09 21:00:28 +02:00
|
|
|
finishedJob.DueAt = dueTime
|
2017-04-29 18:26:43 +02:00
|
|
|
r.pending[finishedJob.Name] = finishedJob
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-r.scheduleTimer:
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.pending) == 0 && len(r.running) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find jobs to run
|
|
|
|
var now time.Time
|
|
|
|
var jobPending bool
|
|
|
|
|
|
|
|
now = time.Now()
|
|
|
|
jobPending = false
|
|
|
|
|
|
|
|
nextJobDue := now.Add(time.Minute) // max(pending.Interval)
|
|
|
|
|
|
|
|
for jobName, job := range r.pending {
|
|
|
|
|
2017-07-01 20:32:56 +02:00
|
|
|
if job.DueAt.After(now) {
|
2017-06-09 21:00:28 +02:00
|
|
|
if job.DueAt.Before(nextJobDue) {
|
|
|
|
nextJobDue = job.DueAt
|
2017-04-29 18:26:43 +02:00
|
|
|
}
|
|
|
|
jobPending = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// This job is due, run it
|
|
|
|
|
|
|
|
delete(r.pending, jobName)
|
|
|
|
r.running[jobName] = job
|
|
|
|
job.LastStart = now
|
|
|
|
|
|
|
|
go func(job Job) {
|
2017-05-03 18:13:50 +02:00
|
|
|
jobLog := jobLogger{r.logger, job.Name}
|
|
|
|
if err := job.RunFunc(jobLog); err != nil {
|
2017-04-30 23:30:30 +02:00
|
|
|
job.LastError = err
|
|
|
|
r.notificationChan <- job
|
2017-04-30 16:08:22 +02:00
|
|
|
}
|
2017-04-29 18:26:43 +02:00
|
|
|
r.finishedJobChan <- job
|
|
|
|
}(job)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if jobPending || len(r.running) > 0 {
|
2017-05-03 17:48:29 +02:00
|
|
|
r.logger.Printf("waiting until %v\n", nextJobDue)
|
2017-04-29 18:26:43 +02:00
|
|
|
r.scheduleTimer = time.After(nextJobDue.Sub(now))
|
|
|
|
goto loop
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|