Documentation ¶
Overview ¶
Package tasks is task scheduling package which lets you run Go functions periodically at pre-determined interval using a simple, human-friendly syntax.
scheduler := tasks.NewScheduler() // Do tasks with params tasks.NewTaskAtIntervals(1, Minutes).Do(taskWithParams, 1, "hello") // Do tasks without params tasks.NewTaskAtIntervals(30, Seconds).Do(task) tasks.NewTaskAtIntervals(5, Minutes).Do(task) tasks.NewTaskAtIntervals(8, Hours).Do(task) // Do tasks on specific weekday tasks.NewTaskOnWeekday(time.Monday, 23, 59).Do(task) // Do tasks daily tasks.NewTaskDaily(10,30).Do(task) // Parse from string format tasks.NewTask("16:18") tasks.NewTask("every 1 second") tasks.NewTask("every 61 minutes") tasks.NewTask("every day") tasks.NewTask("every day 11:15") tasks.NewTask("Monday") tasks.NewTask("Saturday 23:13") scheduler.Add(j) // Start the scheduler scheduler.Start() // Stop the scheduler scheduler.Stop()
Package tasks provides an in-process scheduler for periodic tasks that uses the builder pattern for configuration. Schedule lets you run Golang functions periodically at pre-determined intervals using a simple, human-friendly syntax.
Index ¶
- Constants
- Variables
- func SetGlobalLocation(newLocation *time.Location)
- type Option
- type Publisher
- type Schedule
- type Scheduler
- type Task
- func New(s *Schedule, ops ...Option) Task
- func NewTask(format string, ops ...Option) (Task, error)
- func NewTaskAtIntervals(interval uint64, unit TimeUnit, ops ...Option) Task
- func NewTaskDaily(hour, minute int, ops ...Option) Task
- func NewTaskOnWeekday(startDay time.Weekday, hour, minute int, ops ...Option) Task
- type TimeUnit
Constants ¶
const DefaultRunTimeoutInterval = time.Second
DefaultRunTimeoutInterval specify a timeout for a task to start
const DefaultTickerInterval = time.Second
DefaultTickerInterval for scheduler
Variables ¶
var TimeNow = time.Now
TimeNow is a function that returns the current time
Functions ¶
func SetGlobalLocation ¶
SetGlobalLocation the time location for the package
Types ¶
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures how we set up the client
func WithPublisher ¶ added in v0.16.0
WithPublisher option to provide publisher
func WithRunTimeout ¶ added in v0.13.0
WithRunTimeout option to provide run timeout
func WithTickerInterval ¶
WithTickerInterval option to provide ticker interval
type Publisher ¶ added in v0.16.0
type Publisher interface {
Publish(task Task)
}
Publisher defines a publisher interface
type Schedule ¶ added in v0.13.0
type Schedule struct { // Format specifies the schedule format Format string // Interval * unit bettween runs Interval uint64 // Unit specifies time units, ,e.g. 'minutes', 'hours'... Unit TimeUnit // StartDay specifies day of the week to start on StartDay time.Weekday // LastRunAt specifies datetime of last run LastRunAt *time.Time // NextRunAt specifies datetime of next run NextRunAt time.Time // RunCount specifies the number of runs RunCount uint32 // contains filtered or unexported fields }
Schedule defines task schedule
func ParseSchedule ¶ added in v0.13.0
ParseSchedule parses a schedule string
func (*Schedule) GetLastRun ¶ added in v0.18.1
GetLastRun returns the last run time
func (*Schedule) UpdateNextRun ¶ added in v0.13.0
UpdateNextRun computes the instant when this task should run next
type Scheduler ¶
type Scheduler interface { SetPublisher(Publisher) Scheduler // Add adds a task to a pool of scheduled tasks Add(Task) Scheduler // Get returns the task by id // return nil if task not found Get(id string) Task // List returns all registered tasks List() []Task // Clear will delete all scheduled tasks Clear() // Count returns the number of registered tasks Count() int // IsRunning return the status IsRunning() bool // Start all the pending tasks Start() error // Stop the scheduler Stop() error // Publish the tasks to Publisher Publish() }
Scheduler defines the scheduler interface
type Task ¶
type Task interface { // ID returns the id of the task ID() string // Name returns a name of the task Name() string // RunCount species the number of times the task executed RunCount() uint32 // Schedule returns the task schedule Schedule() *Schedule // UpdateSchedule updates the task with the new format UpdateSchedule(format string) error // ShouldRun returns true if the task should be run now ShouldRun() bool // Run will try to run the task, if it's not already running // and immediately reschedule it after run Run() bool // SetNextRun updates next schedule time SetNextRun(time.Duration) Task // Do accepts a function that should be called every time the task runs Do(taskName string, task interface{}, params ...interface{}) Task // IsRunning return the status IsRunning() bool // SetPublisher sets a publisher for the task, when the status changes SetPublisher(Publisher) Task // Publish publishes the task status Publish() }
Task defines task interface
func NewTask ¶
NewTask creates a new task from parsed format string. every %d seconds | minutes | ... Monday | .. | Sunday at %hh:mm
func NewTaskAtIntervals ¶
NewTaskAtIntervals creates a new task with the time interval.
func NewTaskDaily ¶
NewTaskDaily creates a new task to execute daily at specific time
type TimeUnit ¶
type TimeUnit uint
TimeUnit specifies the time unit: 'minutes', 'hours'...
const ( // Never specifies the time unit to never run a task Never TimeUnit = iota // Seconds specifies the time unit in seconds Seconds // Minutes specifies the time unit in minutes Minutes // Hours specifies the time unit in hours Hours // Days specifies the time unit in days Days // Weeks specifies the time unit in weeks Weeks )