Documentation
¶
Overview ¶
Package cron is an implementation of a job scheduler to run within a worker or a server. It allows developers to configure flexible schedules to run jobs, and trigger retries on failure.
Index ¶
- Constants
- Variables
- func FormatTime(t time.Time) string
- func IsContextCancelled(ctx context.Context) bool
- func IsJobAlreadyLoaded(err error) bool
- func IsJobCancelled(err error) bool
- func IsJobNotFound(err error) bool
- func IsJobNotLoaded(err error) bool
- func IsWeekDay(day time.Weekday) bool
- func IsWeekendDay(day time.Weekday) bool
- func Max(t1, t2 time.Time) time.Time
- func Min(t1, t2 time.Time) time.Time
- func NewEventListener(listener func(e *Event)) logger.Listener
- func NewJobInvocationID() string
- func Now() time.Time
- func SetDefault(jm *JobManager)
- func Since(t time.Time) time.Duration
- func WithJobInvocation(ctx context.Context, ji *JobInvocation) context.Context
- type Action
- type Config
- type DailySchedule
- type DescriptionProvider
- type EnabledProvider
- type Event
- func (e Event) Complete() bool
- func (e Event) Elapsed() time.Duration
- func (e Event) Err() error
- func (e Event) IsEnabled() bool
- func (e Event) IsWritable() bool
- func (e Event) JobInvocation() string
- func (e Event) JobName() string
- func (e *Event) WithAnnotation(key, value string) *Event
- func (e *Event) WithElapsed(d time.Duration) *Event
- func (e *Event) WithErr(err error) *Event
- func (e *Event) WithFlag(f logger.Flag) *Event
- func (e *Event) WithHeadings(headings ...string) *Event
- func (e *Event) WithIsEnabled(isEnabled bool) *Event
- func (e *Event) WithIsWritable(isWritable bool) *Event
- func (e *Event) WithJobInvocation(jobInvocation string) *Event
- func (e *Event) WithJobName(jobName string) *Event
- func (e *Event) WithLabel(key, value string) *Event
- func (e *Event) WithTimestamp(ts time.Time) *Event
- func (e Event) WriteJSON() logger.JSONObj
- func (e Event) WriteText(tf logger.TextFormatter, buf *bytes.Buffer)
- type HistoryConfig
- type ImmediateSchedule
- type IntervalSchedule
- type Job
- type JobBuilder
- func (jb *JobBuilder) Enabled() bool
- func (jb *JobBuilder) Execute(ctx context.Context) error
- func (jb *JobBuilder) Name() string
- func (jb *JobBuilder) OnBroken(ctx context.Context)
- func (jb *JobBuilder) OnCancellation(ctx context.Context)
- func (jb *JobBuilder) OnComplete(ctx context.Context)
- func (jb *JobBuilder) OnDisabled(ctx context.Context)
- func (jb *JobBuilder) OnEnabled(ctx context.Context)
- func (jb *JobBuilder) OnFailure(ctx context.Context)
- func (jb *JobBuilder) OnFixed(ctx context.Context)
- func (jb *JobBuilder) OnStart(ctx context.Context)
- func (jb *JobBuilder) Schedule() Schedule
- func (jb *JobBuilder) ShouldTriggerListeners() bool
- func (jb *JobBuilder) ShouldWriteOutput() bool
- func (jb *JobBuilder) Timeout() (timeout time.Duration)
- func (jb *JobBuilder) WithAction(action Action) *JobBuilder
- func (jb *JobBuilder) WithEnabledProvider(enabledProvider func() bool) *JobBuilder
- func (jb *JobBuilder) WithName(name string) *JobBuilder
- func (jb *JobBuilder) WithOnBroken(receiver func(*JobInvocation)) *JobBuilder
- func (jb *JobBuilder) WithOnCancellation(receiver func(*JobInvocation)) *JobBuilder
- func (jb *JobBuilder) WithOnComplete(receiver func(*JobInvocation)) *JobBuilder
- func (jb *JobBuilder) WithOnDisabled(receiver func(context.Context)) *JobBuilder
- func (jb *JobBuilder) WithOnEnabled(receiver func(context.Context)) *JobBuilder
- func (jb *JobBuilder) WithOnFailure(receiver func(*JobInvocation)) *JobBuilder
- func (jb *JobBuilder) WithOnFixed(receiver func(*JobInvocation)) *JobBuilder
- func (jb *JobBuilder) WithOnStart(receiver func(*JobInvocation)) *JobBuilder
- func (jb *JobBuilder) WithSchedule(schedule Schedule) *JobBuilder
- func (jb *JobBuilder) WithShouldTriggerListenersProvider(provider func() bool) *JobBuilder
- func (jb *JobBuilder) WithShouldWriteOutputProvider(provider func() bool) *JobBuilder
- func (jb *JobBuilder) WithTimeoutProvider(timeoutProvider func() time.Duration) *JobBuilder
- type JobInvocation
- type JobManager
- func (jm *JobManager) CancelJob(jobName string) (err error)
- func (jm *JobManager) Config() *Config
- func (jm *JobManager) DisableJob(jobName string) error
- func (jm *JobManager) DisableJobs(jobNames ...string) error
- func (jm *JobManager) EnableJob(jobName string) error
- func (jm *JobManager) EnableJobs(jobNames ...string) error
- func (jm *JobManager) HasJob(jobName string) (hasJob bool)
- func (jm *JobManager) IsJobDisabled(jobName string) (value bool)
- func (jm *JobManager) IsJobRunning(jobName string) (isRunning bool)
- func (jm *JobManager) IsRunning() bool
- func (jm *JobManager) Job(jobName string) (job *JobScheduler, err error)
- func (jm *JobManager) Latch() *async.Latch
- func (jm *JobManager) LoadJob(job Job) error
- func (jm *JobManager) LoadJobs(jobs ...Job) error
- func (jm *JobManager) Logger() logger.FullReceiver
- func (jm *JobManager) NotifyStarted() <-chan struct{}
- func (jm *JobManager) NotifyStopped() <-chan struct{}
- func (jm *JobManager) RunAllJobs()
- func (jm *JobManager) RunJob(jobName string) error
- func (jm *JobManager) RunJobs(jobNames ...string) error
- func (jm *JobManager) Start() error
- func (jm *JobManager) StartAsync() error
- func (jm *JobManager) Status() *Status
- func (jm *JobManager) Stop() error
- func (jm *JobManager) Tracer() Tracer
- func (jm *JobManager) WithConfig(cfg *Config) *JobManager
- func (jm *JobManager) WithLogger(log logger.FullReceiver) *JobManager
- func (jm *JobManager) WithTracer(tracer Tracer) *JobManager
- type JobScheduler
- func (js *JobScheduler) Cancel()
- func (js *JobScheduler) Disable()
- func (js *JobScheduler) Enable()
- func (js *JobScheduler) Invocation(id string) *JobInvocation
- func (js *JobScheduler) NotifyStarted() <-chan struct{}
- func (js *JobScheduler) NotifyStopped() <-chan struct{}
- func (js *JobScheduler) Run()
- func (js *JobScheduler) RunLoop()
- func (js *JobScheduler) Start() error
- func (js *JobScheduler) StartAsync() error
- func (js *JobScheduler) Stop() error
- func (js *JobScheduler) WithLogger(log logger.FullReceiver) *JobScheduler
- func (js *JobScheduler) WithTracer(tracer Tracer) *JobScheduler
- type JobSchedulersByJobNameAsc
- type JobStatus
- type OnBrokenReceiver
- type OnCancellationReceiver
- type OnCompleteReceiver
- type OnDisabledReceiver
- type OnEnabledReceiver
- type OnFailureReceiver
- type OnFixedReceiver
- type OnStartReceiver
- type OnTheHourAtUTCSchedule
- type OnceAtUTCSchedule
- type Schedule
- func DailyAtUTC(hour, minute, second int) Schedule
- func Every(interval time.Duration) Schedule
- func EveryHour() Schedule
- func EveryHourAtUTC(minute, second int) Schedule
- func EveryHourOnTheHour() Schedule
- func EveryMinute() Schedule
- func EverySecond() Schedule
- func OnceAtUTC(t time.Time) Schedule
- func ParseString(cronString string) (Schedule, error)
- func WeekdaysAtUTC(hour, minute, second int) Schedule
- func WeekendsAtUTC(hour, minute, second int) Schedule
- func WeeklyAtUTC(hour, minute, second int, days ...time.Weekday) Schedule
- type ScheduleProvider
- type SerialProvider
- type ShouldTriggerListenersProvider
- type ShouldWriteOutputProvider
- type State
- type Status
- type StatusProvider
- type StringSchedule
- type TimeoutProvider
- type TraceFinisher
- type Tracer
Constants ¶
const ( DefaultMaxCount = 10 DefaultMaxAge = 6 * time.Hour )
Retention defaults
const ( // DefaultEnabled is a default. DefaultEnabled = true // DefaultSerial is a default. DefaultSerial = false // DefaultShouldWriteOutput is a default. DefaultShouldWriteOutput = true // DefaultShouldTriggerListeners is a default. DefaultShouldTriggerListeners = true )
const ( // FlagStarted is an event flag. FlagStarted logger.Flag = "cron.started" // FlagFailed is an event flag. FlagFailed logger.Flag = "cron.failed" // FlagCancelled is an event flag. FlagCancelled logger.Flag = "cron.cancelled" // FlagComplete is an event flag. FlagComplete logger.Flag = "cron.complete" // FlagBroken is an event flag. FlagBroken logger.Flag = "cron.broken" // FlagFixed is an event flag. FlagFixed logger.Flag = "cron.fixed" // FlagEnabled is an event flag. FlagEnabled logger.Flag = "cron.enabled" // FlagDisabled is an event flag. FlagDisabled logger.Flag = "cron.disabled" )
const ( // ErrJobNotLoaded is a common error. ErrJobNotLoaded exception.Class = "job not loaded" // ErrJobAlreadyLoaded is a common error. ErrJobAlreadyLoaded exception.Class = "job already loaded" // ErrJobNotFound is a common error. ErrJobNotFound exception.Class = "job not found" // ErrJobCancelled is a common error. ErrJobCancelled exception.Class = "job cancelled" )
const ( ErrStringScheduleInvalid exception.Class = "cron: schedule string invalid" ErrStringScheduleComponents exception.Class = "cron: must have at least (5) components space delimited; ex: '0 0 * * * * *'" ErrStringScheduleValueOutOfRange exception.Class = "cron: string schedule part out of range" ErrStringScheduleInvalidRange exception.Class = "cron: range (from-to) invalid" )
Error Constants
const ( StringScheduleShorthandAnnually = "@annually" StringScheduleShorthandYearly = "@yearly" StringScheduleShorthandMonthly = "@monthly" StringScheduleShorthandWeekly = "@weekly" StringScheduleShorthandDaily = "@daily" StringScheduleShorthandHourly = "@hourly" )
String schedule shorthands labels
const ( // AllDaysMask is a bitmask of all the days of the week. AllDaysMask = 1<<uint(time.Sunday) | 1<<uint(time.Monday) | 1<<uint(time.Tuesday) | 1<<uint(time.Wednesday) | 1<<uint(time.Thursday) | 1<<uint(time.Friday) | 1<<uint(time.Saturday) // WeekDaysMask is a bitmask of all the weekdays of the week. WeekDaysMask = 1<<uint(time.Monday) | 1<<uint(time.Tuesday) | 1<<uint(time.Wednesday) | 1<<uint(time.Thursday) | 1<<uint(time.Friday) //WeekendDaysMask is a bitmask of the weekend days of the week. WeekendDaysMask = 1<<uint(time.Sunday) | 1<<uint(time.Saturday) )
NOTE: we have to use shifts here because in their infinite wisdom google didn't make these values powers of two for masking
const ( // DefaultHeartbeatInterval is the interval between schedule next run checks. DefaultHeartbeatInterval = 50 * time.Millisecond )
const (
// EnvVarHeartbeatInterval is an environment variable name.
EnvVarHeartbeatInterval = "CRON_HEARTBEAT_INTERVAL"
)
Variables ¶
var ( // DaysOfWeek are all the time.Weekday in an array for utility purposes. DaysOfWeek = []time.Weekday{ time.Sunday, time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday, time.Saturday, } // WeekDays are the business time.Weekday in an array. WeekDays = []time.Weekday{ time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday, } // WeekWeekEndDaysDays are the weekend time.Weekday in an array. WeekendDays = []time.Weekday{ time.Sunday, time.Saturday, } // Epoch is unix epoch saved for utility purposes. Epoch = time.Unix(0, 0) // Zero is basically epoch but if you want to be super duper sure. Zero = time.Time{} )
NOTE: time.Zero()? what's that?
var ( StringScheduleShorthands = map[string]string{ StringScheduleShorthandAnnually: "0 0 0 1 1 * *", StringScheduleShorthandYearly: "0 0 0 1 1 * *", StringScheduleShorthandMonthly: "0 0 0 1 * * *", StringScheduleShorthandDaily: "0 0 0 * * * *", StringScheduleShorthandHourly: "0 0 * * * * *", } )
String schedule shorthand values
Functions ¶
func IsContextCancelled ¶ added in v0.3.2
IsContextCancelled check if a job is cancelled
func IsJobAlreadyLoaded ¶
IsJobAlreadyLoaded returns if the error is a job already loaded error.
func IsJobCancelled ¶
IsJobCancelled returns if the error is a task not found error.
func IsJobNotFound ¶ added in v0.3.2
IsJobNotFound returns if the error is a task not found error.
func IsJobNotLoaded ¶
IsJobNotLoaded returns if the error is a job not loaded error.
func IsWeekendDay ¶
IsWeekendDay returns if the day is a monday->friday.
func NewEventListener ¶
NewEventListener returns a new event listener.
func NewJobInvocationID ¶ added in v0.3.2
func NewJobInvocationID() string
NewJobInvocationID returns a new pseudo-unique job invocation identifier.
func WithJobInvocation ¶ added in v0.3.2
func WithJobInvocation(ctx context.Context, ji *JobInvocation) context.Context
WithJobInvocation adds a job invocation to a context as a value.
Types ¶
type Config ¶
type Config struct {
History HistoryConfig `json:"history" yaml:"history"`
}
Config is the config object.
func MustNewConfigFromEnv ¶
func MustNewConfigFromEnv() *Config
MustNewConfigFromEnv returns a new config set from environment variables, it will panic if there is an error.
func NewConfigFromEnv ¶
NewConfigFromEnv creates a new config from the environment.
type DailySchedule ¶
DailySchedule is a schedule that fires every day that satisfies the DayOfWeekMask at the given TimeOfDayUTC.
func (DailySchedule) Next ¶ added in v1.20201204.1
func (ds DailySchedule) Next(after time.Time) time.Time
Next implements Schedule.
func (DailySchedule) String ¶ added in v1.20201204.1
func (ds DailySchedule) String() string
type DescriptionProvider ¶
type DescriptionProvider interface {
Description() string
}
DescriptionProvider is a type that proivdes a description.
type EnabledProvider ¶
type EnabledProvider interface {
Enabled() bool
}
EnabledProvider is an optional interface that will allow jobs to control if they're enabled.
type Event ¶
Event is an event.
func (Event) IsWritable ¶
IsWritable determines if the event is written to the logger output.
func (Event) JobInvocation ¶ added in v1.20201204.1
JobInvocation returns the event job invocation.
func (*Event) WithAnnotation ¶
WithAnnotation adds an annotation to the event.
func (*Event) WithElapsed ¶
WithElapsed sets the elapsed time.
func (*Event) WithHeadings ¶
WithHeadings sets the headings.
func (*Event) WithIsEnabled ¶
WithIsEnabled sets if the event is enabled
func (*Event) WithIsWritable ¶
WithIsWritable sets if the event is writable.
func (*Event) WithJobInvocation ¶
WithJobInvocation sets the job invocation.
func (*Event) WithJobName ¶ added in v0.3.2
WithJobName sets the job name.
func (*Event) WithTimestamp ¶
WithTimestamp sets the message timestamp.
type HistoryConfig ¶
type HistoryConfig struct {
MaxCount int `json:"maxCount" yaml:"maxCount" env:"CRON_MAX_COUNT"`
MaxAge time.Duration `json:"maxAge" yaml:"maxAge" env:"CRON_MAX_AGE"`
}
HistoryConfig governs job history retention in memory.
func (HistoryConfig) MaxAgeOrDefault ¶
func (hc HistoryConfig) MaxAgeOrDefault() time.Duration
MaxAgeOrDefault returns the max age or a default.
func (HistoryConfig) MaxCountOrDefault ¶
func (hc HistoryConfig) MaxCountOrDefault() int
MaxCountOrDefault returns the max count or a default.
type ImmediateSchedule ¶
type ImmediateSchedule struct {
// contains filtered or unexported fields
}
ImmediateSchedule fires immediately with an optional continuation schedule.
func Immediately ¶
func Immediately() *ImmediateSchedule
Immediately Returns a schedule that casues a job to run immediately on start, with an optional subsequent schedule.
func (*ImmediateSchedule) Next ¶ added in v1.20201204.1
func (i *ImmediateSchedule) Next(after time.Time) time.Time
Next implements Schedule.
func (ImmediateSchedule) String ¶ added in v1.20201204.1
func (i ImmediateSchedule) String() string
String returns a string representation of the schedul.e
func (*ImmediateSchedule) Then ¶
func (i *ImmediateSchedule) Then(then Schedule) Schedule
Then allows you to specify a subsequent schedule after the first run.
type IntervalSchedule ¶
IntervalSchedule is as chedule that fires every given interval with an optional start delay.
func (IntervalSchedule) Next ¶ added in v1.20201204.1
func (i IntervalSchedule) Next(after time.Time) time.Time
Next implements Schedule.
func (IntervalSchedule) String ¶ added in v1.20201204.1
func (i IntervalSchedule) String() string
String returns a string representation of the schedule.
type JobBuilder ¶ added in v0.3.2
type JobBuilder struct {
// contains filtered or unexported fields
}
JobBuilder allows for job creation w/o a fully formed struct.
func (*JobBuilder) Enabled ¶ added in v0.3.2
func (jb *JobBuilder) Enabled() bool
Enabled returns if the job is enabled.
func (*JobBuilder) Execute ¶ added in v0.3.2
func (jb *JobBuilder) Execute(ctx context.Context) error
Execute runs the job action if it's set.
func (*JobBuilder) Name ¶ added in v0.3.2
func (jb *JobBuilder) Name() string
Name returns the job name.
func (*JobBuilder) OnBroken ¶ added in v0.3.2
func (jb *JobBuilder) OnBroken(ctx context.Context)
OnBroken is a lifecycle hook.
func (*JobBuilder) OnCancellation ¶ added in v0.3.2
func (jb *JobBuilder) OnCancellation(ctx context.Context)
OnCancellation is a lifecycle hook.
func (*JobBuilder) OnComplete ¶ added in v0.3.2
func (jb *JobBuilder) OnComplete(ctx context.Context)
OnComplete is a lifecycle hook.
func (*JobBuilder) OnDisabled ¶
func (jb *JobBuilder) OnDisabled(ctx context.Context)
OnDisabled is a lifecycle hook.
func (*JobBuilder) OnEnabled ¶
func (jb *JobBuilder) OnEnabled(ctx context.Context)
OnEnabled is a lifecycle hook.
func (*JobBuilder) OnFailure ¶ added in v0.3.2
func (jb *JobBuilder) OnFailure(ctx context.Context)
OnFailure is a lifecycle hook.
func (*JobBuilder) OnFixed ¶ added in v0.3.2
func (jb *JobBuilder) OnFixed(ctx context.Context)
OnFixed is a lifecycle hook.
func (*JobBuilder) OnStart ¶ added in v0.3.2
func (jb *JobBuilder) OnStart(ctx context.Context)
OnStart is a lifecycle hook.
func (*JobBuilder) Schedule ¶ added in v0.3.2
func (jb *JobBuilder) Schedule() Schedule
Schedule returns the job schedule.
func (*JobBuilder) ShouldTriggerListeners ¶ added in v0.3.2
func (jb *JobBuilder) ShouldTriggerListeners() bool
ShouldTriggerListeners implements the should trigger listeners provider.
func (*JobBuilder) ShouldWriteOutput ¶ added in v0.3.2
func (jb *JobBuilder) ShouldWriteOutput() bool
ShouldWriteOutput implements the should write output provider.
func (*JobBuilder) Timeout ¶ added in v0.3.2
func (jb *JobBuilder) Timeout() (timeout time.Duration)
Timeout returns the job timeout.
func (*JobBuilder) WithAction ¶ added in v0.3.2
func (jb *JobBuilder) WithAction(action Action) *JobBuilder
WithAction sets the job action.
func (*JobBuilder) WithEnabledProvider ¶ added in v0.3.2
func (jb *JobBuilder) WithEnabledProvider(enabledProvider func() bool) *JobBuilder
WithEnabledProvider sets the enabled provider for the job.
func (*JobBuilder) WithName ¶ added in v0.3.2
func (jb *JobBuilder) WithName(name string) *JobBuilder
WithName sets the job name.
func (*JobBuilder) WithOnBroken ¶ added in v0.3.2
func (jb *JobBuilder) WithOnBroken(receiver func(*JobInvocation)) *JobBuilder
WithOnBroken sets a lifecycle handler.
func (*JobBuilder) WithOnCancellation ¶ added in v0.3.2
func (jb *JobBuilder) WithOnCancellation(receiver func(*JobInvocation)) *JobBuilder
WithOnCancellation sets a lifecycle handler.
func (*JobBuilder) WithOnComplete ¶ added in v0.3.2
func (jb *JobBuilder) WithOnComplete(receiver func(*JobInvocation)) *JobBuilder
WithOnComplete sets a lifecycle handler.
func (*JobBuilder) WithOnDisabled ¶
func (jb *JobBuilder) WithOnDisabled(receiver func(context.Context)) *JobBuilder
WithOnDisabled sets a lifecycle handler.
func (*JobBuilder) WithOnEnabled ¶
func (jb *JobBuilder) WithOnEnabled(receiver func(context.Context)) *JobBuilder
WithOnEnabled sets a lifecycle handler.
func (*JobBuilder) WithOnFailure ¶ added in v0.3.2
func (jb *JobBuilder) WithOnFailure(receiver func(*JobInvocation)) *JobBuilder
WithOnFailure sets a lifecycle handler.
func (*JobBuilder) WithOnFixed ¶ added in v0.3.2
func (jb *JobBuilder) WithOnFixed(receiver func(*JobInvocation)) *JobBuilder
WithOnFixed sets a lifecycle handler.
func (*JobBuilder) WithOnStart ¶ added in v0.3.2
func (jb *JobBuilder) WithOnStart(receiver func(*JobInvocation)) *JobBuilder
WithOnStart sets a lifecycle handler.
func (*JobBuilder) WithSchedule ¶ added in v0.3.2
func (jb *JobBuilder) WithSchedule(schedule Schedule) *JobBuilder
WithSchedule sets the schedule for the job.
func (*JobBuilder) WithShouldTriggerListenersProvider ¶ added in v0.3.2
func (jb *JobBuilder) WithShouldTriggerListenersProvider(provider func() bool) *JobBuilder
WithShouldTriggerListenersProvider sets the enabled provider for the job.
func (*JobBuilder) WithShouldWriteOutputProvider ¶ added in v0.3.2
func (jb *JobBuilder) WithShouldWriteOutputProvider(provider func() bool) *JobBuilder
WithShouldWriteOutputProvider sets the enabled provider for the job.
func (*JobBuilder) WithTimeoutProvider ¶ added in v0.3.2
func (jb *JobBuilder) WithTimeoutProvider(timeoutProvider func() time.Duration) *JobBuilder
WithTimeoutProvider sets the timeout provider.
type JobInvocation ¶ added in v0.3.2
type JobInvocation struct {
ID string `json:"id"`
JobName string `json:"jobName"`
Started time.Time `json:"started"`
Finished time.Time `json:"finished,omitempty"`
Cancelled time.Time `json:"cancelled,omitempty"`
Timeout time.Time `json:"timeout,omitempty"`
Err error `json:"err,omitempty"`
Elapsed time.Duration `json:"elapsed"`
Status JobStatus `json:"status"`
State interface{} `json:"state,omitempty"`
Context context.Context `json:"-"`
Cancel context.CancelFunc `json:"-"`
}
JobInvocation is metadata for a job invocation (or instance of a job running).
func GetJobInvocation ¶ added in v0.3.2
func GetJobInvocation(ctx context.Context) *JobInvocation
GetJobInvocation returns the job invocation ID from a context.
type JobManager ¶
JobManager is the main orchestration and job management object.
func Default ¶
func Default() *JobManager
Default returns a shared instance of a JobManager. If unset, it will initialize it with `New()`.
func MustNewFromEnv ¶
func MustNewFromEnv() *JobManager
MustNewFromEnv returns a new job manager from the environment.
func NewFromConfig ¶
func NewFromConfig(cfg *Config) *JobManager
NewFromConfig returns a new job manager from a given config.
func NewFromEnv ¶
func NewFromEnv() (*JobManager, error)
NewFromEnv returns a new job manager from the environment.
func (*JobManager) CancelJob ¶ added in v0.3.2
func (jm *JobManager) CancelJob(jobName string) (err error)
CancelJob cancels (sends the cancellation signal) to a running job.
func (*JobManager) Config ¶
func (jm *JobManager) Config() *Config
Config returns the job manager config.
func (*JobManager) DisableJob ¶
func (jm *JobManager) DisableJob(jobName string) error
DisableJob stops a job from running but does not unload it.
func (*JobManager) DisableJobs ¶
func (jm *JobManager) DisableJobs(jobNames ...string) error
DisableJobs disables a variadic list of job names.
func (*JobManager) EnableJob ¶
func (jm *JobManager) EnableJob(jobName string) error
EnableJob enables a job that has been disabled.
func (*JobManager) EnableJobs ¶
func (jm *JobManager) EnableJobs(jobNames ...string) error
EnableJobs enables a variadic list of job names.
func (*JobManager) HasJob ¶
func (jm *JobManager) HasJob(jobName string) (hasJob bool)
HasJob returns if a jobName is loaded or not.
func (*JobManager) IsJobDisabled ¶ added in v0.3.2
func (jm *JobManager) IsJobDisabled(jobName string) (value bool)
IsJobDisabled returns if a job is disabled.
func (*JobManager) IsJobRunning ¶ added in v0.3.2
func (jm *JobManager) IsJobRunning(jobName string) (isRunning bool)
IsJobRunning returns if a task is currently running.
func (*JobManager) IsRunning ¶
func (jm *JobManager) IsRunning() bool
IsRunning returns if the job manager is running. It serves as an authoritative healthcheck.
func (*JobManager) Job ¶
func (jm *JobManager) Job(jobName string) (job *JobScheduler, err error)
Job returns a job metadata by name.
func (*JobManager) Latch ¶ added in v0.3.2
func (jm *JobManager) Latch() *async.Latch
Latch returns the internal latch.
func (*JobManager) LoadJobs ¶
func (jm *JobManager) LoadJobs(jobs ...Job) error
LoadJobs loads a variadic list of jobs.
func (*JobManager) Logger ¶
func (jm *JobManager) Logger() logger.FullReceiver
Logger returns the diagnostics agent.
func (*JobManager) NotifyStarted ¶ added in v0.3.1
func (jm *JobManager) NotifyStarted() <-chan struct{}
NotifyStarted returns the started notification channel.
func (*JobManager) NotifyStopped ¶ added in v0.3.1
func (jm *JobManager) NotifyStopped() <-chan struct{}
NotifyStopped returns the stopped notification channel.
func (*JobManager) RunAllJobs ¶
func (jm *JobManager) RunAllJobs()
RunAllJobs runs every job that has been loaded in the JobManager at once.
func (*JobManager) RunJob ¶
func (jm *JobManager) RunJob(jobName string) error
RunJob runs a job by jobName on demand.
func (*JobManager) RunJobs ¶
func (jm *JobManager) RunJobs(jobNames ...string) error
RunJobs runs a variadic list of job names.
func (*JobManager) Start ¶
func (jm *JobManager) Start() error
Start starts the job manager and blocks.
func (*JobManager) StartAsync ¶ added in v1.20201204.1
func (jm *JobManager) StartAsync() error
StartAsync starts the job manager and the loaded jobs. It does not block.
func (*JobManager) Stop ¶
func (jm *JobManager) Stop() error
Stop stops the schedule runner for a JobManager.
func (*JobManager) Tracer ¶
func (jm *JobManager) Tracer() Tracer
Tracer returns the manager's tracer.
func (*JobManager) WithConfig ¶
func (jm *JobManager) WithConfig(cfg *Config) *JobManager
WithConfig sets the job manager config.
func (*JobManager) WithLogger ¶
func (jm *JobManager) WithLogger(log logger.FullReceiver) *JobManager
WithLogger sets the logger and returns a reference to the job manager.
func (*JobManager) WithTracer ¶
func (jm *JobManager) WithTracer(tracer Tracer) *JobManager
WithTracer sets the manager's tracer.
type JobScheduler ¶ added in v1.20201204.1
type JobScheduler struct {
sync.Mutex `json:"-"`
Latch *async.Latch `json:"-"`
Name string `json:"name"`
Description string `json:"description"`
Job Job `json:"-"`
Tracer Tracer `json:"-"`
Log logger.FullReceiver `json:"-"`
Config *Config `json:"-"`
// Meta Fields
Disabled bool `json:"disabled"`
NextRuntime time.Time `json:"nextRuntime"`
Current *JobInvocation `json:"current"`
Last *JobInvocation `json:"last"`
History []JobInvocation `json:"history"`
Schedule Schedule `json:"-"`
EnabledProvider func() bool `json:"-"`
SerialProvider func() bool `json:"-"`
TimeoutProvider func() time.Duration `json:"-"`
ShouldTriggerListenersProvider func() bool `json:"-"`
ShouldWriteOutputProvider func() bool `json:"-"`
}
JobScheduler is a job instance.
func NewJobScheduler ¶ added in v1.20201204.1
func NewJobScheduler(cfg *Config, job Job) *JobScheduler
NewJobScheduler returns a job scheduler for a given job.
func (*JobScheduler) Cancel ¶ added in v1.20201204.1
func (js *JobScheduler) Cancel()
Cancel stops an execution in process.
func (*JobScheduler) Disable ¶ added in v1.20201204.1
func (js *JobScheduler) Disable()
Disable sets the job as disabled.
func (*JobScheduler) Enable ¶ added in v1.20201204.1
func (js *JobScheduler) Enable()
Enable sets the job as enabled.
func (*JobScheduler) Invocation ¶
func (js *JobScheduler) Invocation(id string) *JobInvocation
Invocation returns an invocation by id.
func (*JobScheduler) NotifyStarted ¶ added in v1.20201204.1
func (js *JobScheduler) NotifyStarted() <-chan struct{}
NotifyStarted notifies the job scheduler has started.
func (*JobScheduler) NotifyStopped ¶ added in v1.20201204.1
func (js *JobScheduler) NotifyStopped() <-chan struct{}
NotifyStopped notifies the job scheduler has stopped.
func (*JobScheduler) Run ¶ added in v1.20201204.1
func (js *JobScheduler) Run()
Run forces the job to run. It checks if the job should be allowed to execute. It blocks on the job execution to enforce or clear timeouts.
func (*JobScheduler) RunLoop ¶ added in v1.20201204.1
func (js *JobScheduler) RunLoop()
RunLoop is the main scheduler loop. it alarms on the next runtime and forks a new routine to run the job. It can be aborted with the scheduler's async.Latch.
func (*JobScheduler) Start ¶ added in v1.20201204.1
func (js *JobScheduler) Start() error
Start starts the scheduler. This call blocks.
func (*JobScheduler) StartAsync ¶
func (js *JobScheduler) StartAsync() error
StartAsync starts the job scheduler in the background.
func (*JobScheduler) Stop ¶ added in v1.20201204.1
func (js *JobScheduler) Stop() error
Stop stops the scheduler.
func (*JobScheduler) WithLogger ¶
func (js *JobScheduler) WithLogger(log logger.FullReceiver) *JobScheduler
WithLogger sets the scheduler logger.
func (*JobScheduler) WithTracer ¶
func (js *JobScheduler) WithTracer(tracer Tracer) *JobScheduler
WithTracer sets the scheduler tracer.
type JobSchedulersByJobNameAsc ¶ added in v1.20201204.1
type JobSchedulersByJobNameAsc []*JobScheduler
JobSchedulersByJobNameAsc is a wrapper that sorts job schedulers by the job name ascending.
func (JobSchedulersByJobNameAsc) Len ¶ added in v1.20201204.1
func (s JobSchedulersByJobNameAsc) Len() int
Len implements sorter.
func (JobSchedulersByJobNameAsc) Less ¶ added in v1.20201204.1
func (s JobSchedulersByJobNameAsc) Less(i, j int) bool
Less implements sorter.
func (JobSchedulersByJobNameAsc) Swap ¶ added in v1.20201204.1
func (s JobSchedulersByJobNameAsc) Swap(i, j int)
Swap implements sorter.
type OnBrokenReceiver ¶ added in v0.3.2
OnBrokenReceiver is an interface that allows a job to be signaled when it is a failure that followed a previous success.
type OnCancellationReceiver ¶
OnCancellationReceiver is an interface that allows a task to be signaled when it has been canceled.
type OnCompleteReceiver ¶
OnCompleteReceiver is an interface that allows a task to be signaled when it has been completed.
type OnDisabledReceiver ¶
OnDisabledReceiver is a lifecycle hook for disabled events.
type OnEnabledReceiver ¶
OnEnabledReceiver is a lifecycle hook for enabled events.
type OnFailureReceiver ¶ added in v0.3.2
OnFailureReceiver is an interface that allows a task to be signaled when it has been completed.
type OnFixedReceiver ¶ added in v0.3.2
OnFixedReceiver is an interface that allows a jbo to be signaled when is a success that followed a previous failure.
type OnStartReceiver ¶
OnStartReceiver is an interface that allows a task to be signaled when it has started.
type OnTheHourAtUTCSchedule ¶ added in v1.20201204.1
OnTheHourAtUTCSchedule is a schedule that fires every hour on the given minute.
func (OnTheHourAtUTCSchedule) Next ¶ added in v1.20201204.1
func (o OnTheHourAtUTCSchedule) Next(after time.Time) time.Time
Next implements the chronometer Schedule api.
func (OnTheHourAtUTCSchedule) String ¶ added in v1.20201204.1
func (o OnTheHourAtUTCSchedule) String() string
String returns a string representation of the schedule.
type OnceAtUTCSchedule ¶ added in v0.3.2
OnceAtUTCSchedule is a schedule.
func (OnceAtUTCSchedule) Next ¶ added in v1.20201204.1
func (oa OnceAtUTCSchedule) Next(after time.Time) time.Time
Next returns the next runtime.
func (OnceAtUTCSchedule) String ¶ added in v1.20201204.1
func (oa OnceAtUTCSchedule) String() string
String returns a string representation of the schedule.
type Schedule ¶
type Schedule interface {
// GetNextRuntime should return the next runtime after a given previous runtime. If `after` is time.Time{} it should be assumed
// the job hasn't run yet. If time.Time{} is returned by the schedule it is inferred that the job should not run again.
Next(time.Time) time.Time
}
Schedule is a type that provides a next runtime after a given previous runtime.
func DailyAtUTC ¶ added in v0.3.2
DailyAtUTC returns a schedule that fires every day at the given hour, minute and second in UTC.
func EveryHourAtUTC ¶ added in v1.20201204.1
EveryHourAtUTC returns a schedule that fires every hour at a given minute.
func EveryHourOnTheHour ¶
func EveryHourOnTheHour() Schedule
EveryHourOnTheHour returns a schedule that fires every 60 minutes on the 00th minute.
func EveryMinute ¶
func EveryMinute() Schedule
EveryMinute returns a schedule that fires every minute.
func EverySecond ¶
func EverySecond() Schedule
EverySecond returns a schedule that fires every second.
func OnceAtUTC ¶ added in v0.3.2
OnceAtUTC returns a schedule that fires once at a given time. It will never fire again unless reloaded.
func ParseString ¶ added in v1.20201204.1
ParseString parses a cron formatted string into a schedule. The string must be at least 5 components, whitespace separated. If the string has 5 components a 0 will be prepended for the seconds component, and a * appended for the year component. If the string has 6 components a * appended for the year component. (seconds) (minutes) (hours) (day of month) (month) (day of week) (year)
Field name Mandatory? Allowed values Allowed special characters ---------- ---------- -------------- -------------------------- Seconds No 0-59 * / , - Minutes Yes 0-59 * / , - Hours Yes 0-23 * / , - Day of month Yes 1-31 * / , - L W Month Yes 1-12 or JAN-DEC * / , - Day of week Yes 0-6 or SUN-SAT * / , - L # Year No 1970–2099 * / , -
You can also use shorthands for the cron string:
@yearly is equivalent to "0 0 0 1 1 * *" @monthly is equivalent to "0 0 0 1 * * *" @weekly is equivalent to "0 0 0 * * 0 *" @daily is equivalent to "0 0 0 * * * *" @hourly is equivalent to "0 0 * * * * *" @every xyz will parse the `xyz` value as a duration and return an every schedule for that
func WeekdaysAtUTC ¶ added in v0.3.2
WeekdaysAtUTC returns a schedule that fires every week day at the given hour, minute and second in UTC>
func WeekendsAtUTC ¶ added in v0.3.2
WeekendsAtUTC returns a schedule that fires every weekend day at the given hour, minut and second.
type ScheduleProvider ¶ added in v0.3.2
type ScheduleProvider interface {
Schedule() Schedule
}
ScheduleProvider returns a schedule for the job.
type SerialProvider ¶
type SerialProvider interface {
Serial() bool
}
SerialProvider is an optional interface that prohibits a task from running if another instance of the task is currently running.
type ShouldTriggerListenersProvider ¶ added in v0.3.2
type ShouldTriggerListenersProvider interface {
ShouldTriggerListeners() bool
}
ShouldTriggerListenersProvider is a type that enables or disables logger listeners.
type ShouldWriteOutputProvider ¶ added in v0.3.2
type ShouldWriteOutputProvider interface {
ShouldWriteOutput() bool
}
ShouldWriteOutputProvider is a type that enables or disables logger output for events.
type Status ¶
type Status struct {
Jobs []*JobScheduler `json:"jobs"`
Running map[string][]*JobInvocation `json:"running,omitempty"`
}
Status is a status object
type StatusProvider ¶
type StatusProvider interface {
Status() string
}
StatusProvider is an interface that allows a task to report its status.
type StringSchedule ¶ added in v1.20201204.1
type StringSchedule struct {
Original string
Seconds []int
Minutes []int
Hours []int
DaysOfMonth []int
Months []int
DaysOfWeek []int
Years []int
}
StringSchedule is a schedule generated from a cron string.
func (*StringSchedule) FullString ¶ added in v1.20201204.1
func (ss *StringSchedule) FullString() string
FullString returns a fully formed string representation of the schedule's components. It shows fields as expanded.
func (*StringSchedule) Next ¶ added in v1.20201204.1
func (ss *StringSchedule) Next(after time.Time) time.Time
Next implements cron.Schedule.
func (*StringSchedule) String ¶ added in v1.20201204.1
func (ss *StringSchedule) String() string
String returns the original string schedule.
type TimeoutProvider ¶
TimeoutProvider is an interface that allows a task to be timed out.
type TraceFinisher ¶
TraceFinisher is a finisher for traces.
Source Files
¶
- action.go
- cancellation.go
- config.go
- constants.go
- context.go
- daily_schedule.go
- default.go
- errors.go
- event.go
- immediate_schedule.go
- interfaces.go
- interval_schedule.go
- job.go
- job_builder.go
- job_invocation.go
- job_manager.go
- job_scheduler.go
- on_the_hour_at.go
- once_at.go
- package.go
- schedule.go
- sort.go
- status.go
- string_schedule.go
- tracer.go
- util.go