scheduler

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(ctx context.Context) error

Run starts the global scheduler

func SetEventDispatcher added in v0.8.0

func SetEventDispatcher(fn func(event interface{}) error)

SetEventDispatcher sets the function used to dispatch events. This is called by the events package to wire up event dispatching.

func SetScheduler

func SetScheduler(s *Scheduler)

SetScheduler sets the global scheduler

func Stop

func Stop()

Stop stops the global scheduler

Types

type Expression

type Expression struct {
	// contains filtered or unexported fields
}

Expression represents a parsed cron expression

func ParseExpression

func ParseExpression(expr string) (*Expression, error)

ParseExpression parses a cron expression string

func (*Expression) IsDue

func (e *Expression) IsDue(t time.Time) bool

IsDue checks if the expression matches the given time

func (*Expression) Next

func (e *Expression) Next(from time.Time) time.Time

Next returns the next time the expression will match after the given time

type Job

type Job struct {
	// contains filtered or unexported fields
}

Job represents a scheduled task

func Call

func Call(callback func()) *Job

Call schedules a closure using the global scheduler

func Command

func Command(command string, args ...string) *Job

Command schedules a command using the global scheduler

func Jobs

func Jobs() []*Job

Jobs returns all jobs from the global scheduler

func (*Job) After

func (j *Job) After(callback func()) *Job

After registers a callback to run after the job

func (*Job) AppendOutputTo

func (j *Job) AppendOutputTo(filename string) *Job

AppendOutputTo appends output to a file

func (*Job) At

func (j *Job) At(time string) *Job

At sets the time for daily/weekly jobs

func (*Job) Before

func (j *Job) Before(callback func()) *Job

Before registers a callback to run before the job

func (*Job) Between

func (j *Job) Between(start, end string) *Job

Between limits execution to a time range

func (*Job) Cron

func (j *Job) Cron(expression string) *Job

Cron sets a custom cron expression

func (*Job) Daily

func (j *Job) Daily() *Job

Daily runs the job daily at midnight

func (*Job) DailyAt

func (j *Job) DailyAt(time string) *Job

DailyAt runs the job daily at a specific time

func (*Job) Days

func (j *Job) Days(days ...int) *Job

Days sets specific days for the job

func (*Job) EmailOutputTo

func (j *Job) EmailOutputTo(email string) *Job

EmailOutputTo emails the output (requires mail configuration)

func (*Job) Environments

func (j *Job) Environments(environments ...string) *Job

Environments limits execution to specific environments

func (*Job) EvenInMaintenanceMode

func (j *Job) EvenInMaintenanceMode() *Job

EvenInMaintenanceMode allows job to run in maintenance mode

func (*Job) EveryFifteenMinutes

func (j *Job) EveryFifteenMinutes() *Job

EveryFifteenMinutes runs the job every fifteen minutes

func (*Job) EveryFiveMinutes

func (j *Job) EveryFiveMinutes() *Job

EveryFiveMinutes runs the job every five minutes

func (*Job) EveryMinute

func (j *Job) EveryMinute() *Job

EveryMinute runs the job every minute

func (*Job) EveryTenMinutes

func (j *Job) EveryTenMinutes() *Job

EveryTenMinutes runs the job every ten minutes

func (*Job) EveryThirtyMinutes

func (j *Job) EveryThirtyMinutes() *Job

EveryThirtyMinutes runs the job every thirty minutes

func (*Job) Fridays

func (j *Job) Fridays() *Job

Fridays runs the job on Fridays

func (*Job) GetLastRun

func (j *Job) GetLastRun() time.Time

GetLastRun returns the last run time

func (*Job) GetName

func (j *Job) GetName() string

GetName returns the job name

func (*Job) GetNextRun

func (j *Job) GetNextRun() time.Time

GetNextRun returns the next run time

func (*Job) Hourly

func (j *Job) Hourly() *Job

Hourly runs the job every hour

func (*Job) HourlyAt

func (j *Job) HourlyAt(minute int) *Job

HourlyAt runs the job every hour at a specific minute

func (*Job) IsDue

func (j *Job) IsDue(t time.Time) bool

IsDue checks if the job should run at the given time

func (*Job) IsRunning

func (j *Job) IsRunning() bool

IsRunning returns whether the job is currently running

func (*Job) Mondays

func (j *Job) Mondays() *Job

Mondays runs the job on Mondays

func (*Job) Monthly

func (j *Job) Monthly() *Job

Monthly runs the job monthly

func (*Job) Name

func (j *Job) Name(name string) *Job

Name sets the job name

func (*Job) OnFailure

func (j *Job) OnFailure(callback func(error)) *Job

OnFailure registers a callback for failed execution

func (*Job) OnOneServer

func (j *Job) OnOneServer() *Job

OnOneServer ensures job runs on only one server (requires distributed lock)

func (*Job) OnSuccess

func (j *Job) OnSuccess(callback func()) *Job

OnSuccess registers a callback for successful execution

func (*Job) Run

func (j *Job) Run() error

Run executes the job

func (*Job) RunInBackground

func (j *Job) RunInBackground() *Job

RunInBackground runs the command in background

func (*Job) Saturdays

func (j *Job) Saturdays() *Job

Saturdays runs the job on Saturdays

func (*Job) SendOutputTo

func (j *Job) SendOutputTo(filename string) *Job

SendOutputTo redirects output to a file

func (*Job) ShouldRun

func (j *Job) ShouldRun() bool

ShouldRun checks if the job should run based on constraints

func (*Job) Skip

func (j *Job) Skip(callback func() bool) *Job

Skip adds a skip condition

func (*Job) Sundays

func (j *Job) Sundays() *Job

Sundays runs the job on Sundays

func (*Job) Thursdays

func (j *Job) Thursdays() *Job

Thursdays runs the job on Thursdays

func (*Job) Tuesdays

func (j *Job) Tuesdays() *Job

Tuesdays runs the job on Tuesdays

func (*Job) UnlessBetween

func (j *Job) UnlessBetween(start, end string) *Job

UnlessBetween prevents execution in a time range

func (*Job) Wednesdays

func (j *Job) Wednesdays() *Job

Wednesdays runs the job on Wednesdays

func (*Job) Weekdays

func (j *Job) Weekdays() *Job

Weekdays runs the job only on weekdays

func (*Job) Weekends

func (j *Job) Weekends() *Job

Weekends runs the job only on weekends

func (*Job) Weekly

func (j *Job) Weekly() *Job

Weekly runs the job weekly

func (*Job) When

func (j *Job) When(callback func() bool) *Job

When adds a condition for job execution

func (*Job) WithoutOverlapping

func (j *Job) WithoutOverlapping() *Job

WithoutOverlapping prevents job overlap

func (*Job) Yearly

func (j *Job) Yearly() *Job

Yearly runs the job yearly

type Kernel

type Kernel struct {
	// contains filtered or unexported fields
}

Kernel provides a convenient way to define scheduled tasks for an application

func NewKernel

func NewKernel() *Kernel

NewKernel creates a new scheduler kernel

func (*Kernel) Boot

func (k *Kernel) Boot()

Boot initializes the kernel

func (*Kernel) Define

func (k *Kernel) Define()

Define is where scheduled tasks should be registered This method should be overridden in the application

func (*Kernel) Jobs

func (k *Kernel) Jobs() []*Job

Jobs returns all scheduled jobs

func (*Kernel) Run

func (k *Kernel) Run(ctx context.Context) error

Run starts the scheduler

func (*Kernel) Schedule

func (k *Kernel) Schedule() *Scheduler

Schedule returns the scheduler for defining tasks

func (*Kernel) Stop

func (k *Kernel) Stop()

Stop stops the scheduler

type Logger

type Logger interface {
	Info(msg string, keysAndValues ...interface{})
	Error(msg string, keysAndValues ...interface{})
	Debug(msg string, keysAndValues ...interface{})
}

Logger interface for scheduler logging

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager manages multiple schedulers

func GetManager

func GetManager() *Manager

GetManager returns the global manager

func NewManager

func NewManager() *Manager

NewManager creates a new scheduler manager

func (*Manager) Add

func (m *Manager) Add(name string, scheduler *Scheduler) *Manager

Add adds a scheduler to the manager

func (*Manager) Default

func (m *Manager) Default() *Scheduler

Default returns the default scheduler

func (*Manager) Get

func (m *Manager) Get(name string) (*Scheduler, bool)

Get retrieves a scheduler by name

func (*Manager) RunAll

func (m *Manager) RunAll(ctx context.Context) error

RunAll starts all schedulers

func (*Manager) SetDefault

func (m *Manager) SetDefault(name string) *Manager

SetDefault sets the default scheduler name

func (*Manager) StopAll

func (m *Manager) StopAll()

StopAll stops all schedulers

type Schedule

type Schedule struct {
	// contains filtered or unexported fields
}

Schedule represents when a job should run

func NewSchedule

func NewSchedule() *Schedule

NewSchedule creates a new schedule

func (*Schedule) At

func (s *Schedule) At(time string) *Schedule

At sets the time for daily/weekly jobs

func (*Schedule) Cron

func (s *Schedule) Cron(expression string) *Schedule

Cron sets a custom cron expression

func (*Schedule) Daily

func (s *Schedule) Daily() *Schedule

Daily runs daily at midnight

func (*Schedule) DailyAt

func (s *Schedule) DailyAt(time string) *Schedule

DailyAt runs daily at a specific time

func (*Schedule) Days

func (s *Schedule) Days(days ...int) *Schedule

Days sets specific days of the month

func (*Schedule) EveryFifteenMinutes

func (s *Schedule) EveryFifteenMinutes() *Schedule

EveryFifteenMinutes runs every fifteen minutes

func (*Schedule) EveryFiveMinutes

func (s *Schedule) EveryFiveMinutes() *Schedule

EveryFiveMinutes runs every five minutes

func (*Schedule) EveryMinute

func (s *Schedule) EveryMinute() *Schedule

EveryMinute runs every minute

func (*Schedule) EveryTenMinutes

func (s *Schedule) EveryTenMinutes() *Schedule

EveryTenMinutes runs every ten minutes

func (*Schedule) EveryThirtyMinutes

func (s *Schedule) EveryThirtyMinutes() *Schedule

EveryThirtyMinutes runs every thirty minutes

func (*Schedule) Fridays

func (s *Schedule) Fridays() *Schedule

Fridays runs on Fridays

func (*Schedule) GetExpression

func (s *Schedule) GetExpression() string

GetExpression returns the cron expression

func (*Schedule) Hourly

func (s *Schedule) Hourly() *Schedule

Hourly runs every hour

func (*Schedule) HourlyAt

func (s *Schedule) HourlyAt(minute int) *Schedule

HourlyAt runs every hour at a specific minute

func (*Schedule) IsDue

func (s *Schedule) IsDue(t time.Time) bool

IsDue checks if the schedule is due at the given time

func (*Schedule) Mondays

func (s *Schedule) Mondays() *Schedule

Mondays runs on Mondays

func (*Schedule) Monthly

func (s *Schedule) Monthly() *Schedule

Monthly runs monthly on the first day

func (*Schedule) MonthlyOn

func (s *Schedule) MonthlyOn(day int, time string) *Schedule

MonthlyOn runs monthly on a specific day and time

func (*Schedule) NextRun

func (s *Schedule) NextRun(from time.Time) time.Time

NextRun returns the next run time

func (*Schedule) Saturdays

func (s *Schedule) Saturdays() *Schedule

Saturdays runs on Saturdays

func (*Schedule) Sundays

func (s *Schedule) Sundays() *Schedule

Sundays runs on Sundays

func (*Schedule) Thursdays

func (s *Schedule) Thursdays() *Schedule

Thursdays runs on Thursdays

func (*Schedule) Tuesdays

func (s *Schedule) Tuesdays() *Schedule

Tuesdays runs on Tuesdays

func (*Schedule) Wednesdays

func (s *Schedule) Wednesdays() *Schedule

Wednesdays runs on Wednesdays

func (*Schedule) Weekdays

func (s *Schedule) Weekdays() *Schedule

Weekdays runs only on weekdays (Mon-Fri)

func (*Schedule) Weekends

func (s *Schedule) Weekends() *Schedule

Weekends runs only on weekends (Sat-Sun)

func (*Schedule) Weekly

func (s *Schedule) Weekly() *Schedule

Weekly runs weekly on Sunday at midnight

func (*Schedule) Yearly

func (s *Schedule) Yearly() *Schedule

Yearly runs yearly on January 1st

type ScheduledTaskFailed added in v0.8.0

type ScheduledTaskFailed struct {
	Context    context.Context
	TaskName   string
	Error      string
	DurationMs int64
	TraceID    string
	SpanID     string
	ParentID   string
}

ScheduledTaskFailed is dispatched when a scheduled task fails

func (*ScheduledTaskFailed) Name added in v0.8.0

func (e *ScheduledTaskFailed) Name() string

Name returns the event name

type ScheduledTaskFinished added in v0.8.0

type ScheduledTaskFinished struct {
	Context    context.Context
	TaskName   string
	DurationMs int64
	TraceID    string
	SpanID     string
	ParentID   string
}

ScheduledTaskFinished is dispatched when a scheduled task completes successfully

func (*ScheduledTaskFinished) Name added in v0.8.0

func (e *ScheduledTaskFinished) Name() string

Name returns the event name

type ScheduledTaskStarting added in v0.8.0

type ScheduledTaskStarting struct {
	Context  context.Context
	TaskName string
	TraceID  string
	SpanID   string
	ParentID string
}

ScheduledTaskStarting is dispatched when a scheduled task begins

func (*ScheduledTaskStarting) Name added in v0.8.0

func (e *ScheduledTaskStarting) Name() string

Name returns the event name

type Scheduler

type Scheduler struct {
	// contains filtered or unexported fields
}

Scheduler manages and executes scheduled jobs

func GetScheduler

func GetScheduler() *Scheduler

GetScheduler returns the global scheduler

func New

func New() *Scheduler

New creates a new scheduler instance

func (*Scheduler) Add

func (s *Scheduler) Add(job *Job) *Job

Add registers a new job with the scheduler

func (*Scheduler) After

func (s *Scheduler) After(callback func()) *Scheduler

After registers a callback to run after job execution

func (*Scheduler) Before

func (s *Scheduler) Before(callback func()) *Scheduler

Before registers a callback to run before job execution

func (*Scheduler) Call

func (s *Scheduler) Call(callback func()) *Job

Call creates a new job that executes a closure

func (*Scheduler) Command

func (s *Scheduler) Command(command string, args ...string) *Job

Command creates a new job that executes a command

func (*Scheduler) Jobs

func (s *Scheduler) Jobs() []*Job

Jobs returns all registered jobs

func (*Scheduler) MaintenanceMode

func (s *Scheduler) MaintenanceMode(enabled bool) *Scheduler

MaintenanceMode enables or disables maintenance mode

func (*Scheduler) Run

func (s *Scheduler) Run(ctx context.Context) error

Run starts the scheduler

func (*Scheduler) SetLogger

func (s *Scheduler) SetLogger(logger Logger) *Scheduler

SetLogger sets a custom logger

func (*Scheduler) SetTimezone

func (s *Scheduler) SetTimezone(tz *time.Location) *Scheduler

SetTimezone sets the timezone for the scheduler

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop stops the scheduler

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL