jobrun: log through abstract logger interface instead of stderr

This commit is contained in:
Christian Schwarz 2017-05-03 17:48:29 +02:00
parent 77f749112c
commit 3b6d79ec67
2 changed files with 14 additions and 7 deletions

View File

@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/zrepl/zrepl/jobrun" "github.com/zrepl/zrepl/jobrun"
"log"
"os"
"sync" "sync"
"time" "time"
) )
@ -30,7 +32,8 @@ func main() {
return return
} }
runner = jobrun.NewJobRunner() jobrunLogger := log.New(os.Stderr, "jobrun: ", log.LUTC|log.Ldate|log.Ltime)
runner = jobrun.NewJobRunner(jobrunLogger)
return return
} }
app.Commands = []cli.Command{ app.Commands = []cli.Command{

View File

@ -1,11 +1,13 @@
package jobrun package jobrun
import ( import (
"fmt"
"os"
"time" "time"
) )
type Logger interface {
Printf(format string, v ...interface{})
}
type Job struct { type Job struct {
Name string Name string
RunFunc func() (err error) RunFunc func() (err error)
@ -16,6 +18,7 @@ type Job struct {
} }
type JobRunner struct { type JobRunner struct {
logger Logger
notificationChan chan Job notificationChan chan Job
newJobChan chan Job newJobChan chan Job
finishedJobChan chan Job finishedJobChan chan Job
@ -24,8 +27,9 @@ type JobRunner struct {
running map[string]Job running map[string]Job
} }
func NewJobRunner() *JobRunner { func NewJobRunner(logger Logger) *JobRunner {
return &JobRunner{ return &JobRunner{
logger: logger,
notificationChan: make(chan Job), notificationChan: make(chan Job),
newJobChan: make(chan Job), newJobChan: make(chan Job),
finishedJobChan: make(chan Job), finishedJobChan: make(chan Job),
@ -66,9 +70,9 @@ loop:
runTime := time.Since(finishedJob.LastStart) runTime := time.Since(finishedJob.LastStart)
fmt.Fprintf(os.Stderr, "[%s] finished after %v\n", finishedJob.Name, runTime) r.logger.Printf("[%s] finished after %v\n", finishedJob.Name, runTime)
if runTime > finishedJob.Interval { if runTime > finishedJob.Interval {
fmt.Fprintf(os.Stderr, "[%s] WARN: job exceeded interval of %v\n", finishedJob.Name, finishedJob.Interval) r.logger.Printf("[%s] job exceeded interval of %v\n", finishedJob.Name, finishedJob.Interval)
} }
delete(r.running, finishedJob.Name) delete(r.running, finishedJob.Name)
@ -121,7 +125,7 @@ loop:
if jobPending || len(r.running) > 0 { if jobPending || len(r.running) > 0 {
nextJobDue = nextJobDue.Add(time.Second).Round(time.Second) nextJobDue = nextJobDue.Add(time.Second).Round(time.Second)
fmt.Fprintf(os.Stderr, "jobrun: waiting until %v\n", nextJobDue) r.logger.Printf("waiting until %v\n", nextJobDue)
r.scheduleTimer = time.After(nextJobDue.Sub(now)) r.scheduleTimer = time.After(nextJobDue.Sub(now))
goto loop goto loop
} }