Skip to content

Commit

Permalink
Log the error message to stderr or a configurable log.Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Meire committed Mar 29, 2016
1 parent 634cf55 commit eedb142
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions cron.go
Original file line number Diff line number Diff line change
@@ -3,7 +3,8 @@
package cron

import (
"fmt"
"log"
"runtime"
"sort"
"time"
)
@@ -17,6 +18,7 @@ type Cron struct {
add chan *Entry
snapshot chan []*Entry
running bool
ErrorLog *log.Logger
}

// Job is an interface for submitted cron jobs.
@@ -75,6 +77,7 @@ func New() *Cron {
stop: make(chan struct{}),
snapshot: make(chan []*Entry),
running: false,
ErrorLog: nil,
}
}

@@ -131,7 +134,10 @@ func (c *Cron) Start() {
func (c *Cron) runWithRecovery(j Job) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Cron function panicked: ", r)
const size = 64 << 10
buf := make([]byte, size)
buf = buf[:runtime.Stack(buf, false)]
c.logf("cron: panic running job: %v\n%s", r, buf)
}
}()
j.Run()
@@ -188,6 +194,15 @@ func (c *Cron) run() {
}
}

// Logs an error to stderr or to the configured error log
func (c *Cron) logf(format string, args ...interface{}) {
if c.ErrorLog != nil {
c.ErrorLog.Printf(format, args...)
} else {
log.Printf(format, args...)
}
}

// Stop stops the cron scheduler if it is running; otherwise it does nothing.
func (c *Cron) Stop() {
if !c.running {

0 comments on commit eedb142

Please sign in to comment.