package gocron
import (
"errors"
"fmt"
"reflect"
"regexp"
"runtime"
"sync"
"time"
)
type PanicHandlerFunc func (jobName string , recoverData interface {})
var (
panicHandler PanicHandlerFunc
panicHandlerMutex = sync .RWMutex {}
)
func SetPanicHandler (handler PanicHandlerFunc ) {
panicHandlerMutex .Lock ()
defer panicHandlerMutex .Unlock ()
panicHandler = handler
}
var (
ErrNotAFunction = errors .New ("gocron: only functions can be scheduled into the job queue" )
ErrNotScheduledWeekday = errors .New ("gocron: job not scheduled weekly on a weekday" )
ErrJobNotFoundWithTag = errors .New ("gocron: no jobs found with given tag" )
ErrJobNotFound = errors .New ("gocron: no job found" )
ErrUnsupportedTimeFormat = errors .New ("gocron: the given time format is not supported" )
ErrInvalidInterval = errors .New ("gocron: .Every() interval must be greater than 0" )
ErrInvalidIntervalType = errors .New ("gocron: .Every() interval must be int, time.Duration, or string" )
ErrInvalidIntervalUnitsSelection = errors .New ("gocron: .Every(time.Duration) and .Cron() cannot be used with units (e.g. .Seconds())" )
ErrInvalidFunctionParameters = errors .New ("gocron: length of function parameters must match job function parameters" )
ErrAtTimeNotSupported = errors .New ("gocron: the At() method is not supported for this time unit" )
ErrWeekdayNotSupported = errors .New ("gocron: weekday is not supported for time unit" )
ErrInvalidDayOfMonthEntry = errors .New ("gocron: only days 1 through 28 and -1 through -28 are allowed for monthly schedules" )
ErrInvalidMonthLastDayEntry = errors .New ("gocron: only a single negative integer is permitted for MonthLastDay" )
ErrTagsUnique = func (tag string ) error { return fmt .Errorf ("gocron: a non-unique tag was set on the job: %s" , tag ) }
ErrWrongParams = errors .New ("gocron: wrong list of params" )
ErrDoWithJobDetails = errors .New ("gocron: DoWithJobDetails expects a function whose last parameter is a gocron.Job" )
ErrUpdateCalledWithoutJob = errors .New ("gocron: a call to Scheduler.Update() requires a call to Scheduler.Job() first" )
ErrCronParseFailure = errors .New ("gocron: cron expression failed to be parsed" )
ErrInvalidDaysOfMonthDuplicateValue = errors .New ("gocron: duplicate days of month is not allowed in Month() and Months() methods" )
)
func wrapOrError(toWrap error , err error ) error {
var returnErr error
if toWrap != nil && !errors .Is (err , toWrap ) {
returnErr = fmt .Errorf ("%s: %w" , err , toWrap )
} else {
returnErr = err
}
return returnErr
}
var (
timeWithSeconds = regexp .MustCompile (`(?m)^\d{1,2}:\d\d:\d\d$` )
timeWithoutSeconds = regexp .MustCompile (`(?m)^\d{1,2}:\d\d$` )
)
type schedulingUnit int
const (
milliseconds schedulingUnit = iota
seconds
minutes
hours
days
weeks
months
duration
crontab
)
func callJobFunc(jobFunc interface {}) {
if jobFunc == nil {
return
}
f := reflect .ValueOf (jobFunc )
if !f .IsZero () {
f .Call ([]reflect .Value {})
}
}
func callJobFuncWithParams(jobFunc interface {}, params []interface {}) error {
if jobFunc == nil {
return nil
}
f := reflect .ValueOf (jobFunc )
if f .IsZero () {
return nil
}
if len (params ) != f .Type ().NumIn () {
return nil
}
in := make ([]reflect .Value , len (params ))
for k , param := range params {
in [k ] = reflect .ValueOf (param )
}
vals := f .Call (in )
for _ , val := range vals {
i := val .Interface ()
if err , ok := i .(error ); ok {
return err
}
}
return nil
}
func getFunctionName(fn interface {}) string {
return runtime .FuncForPC (reflect .ValueOf (fn ).Pointer ()).Name ()
}
func getFunctionNameOfPointer(fn interface {}) string {
return runtime .FuncForPC (reflect .ValueOf (fn ).Elem ().Pointer ()).Name ()
}
func parseTime(t string ) (hour , min , sec int , err error ) {
var timeLayout string
switch {
case timeWithSeconds .MatchString (t ):
timeLayout = "15:04:05"
case timeWithoutSeconds .MatchString (t ):
timeLayout = "15:04"
default :
return 0 , 0 , 0 , ErrUnsupportedTimeFormat
}
parsedTime , err := time .Parse (timeLayout , t )
if err != nil {
return 0 , 0 , 0 , ErrUnsupportedTimeFormat
}
return parsedTime .Hour (), parsedTime .Minute (), parsedTime .Second (), nil
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .