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 ¶
const DefaultRunTimeoutInterval = time.Second
DefaultRunTimeoutInterval specify a timeout for a task to start
const DefaultTickerInterval = time.Second
DefaultTickerInterval for scheduler
Variables ¶
This section is empty.
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 WithTickerInterval ¶
WithTickerInterval option to provide ticker interval
type Scheduler ¶
type Scheduler interface {
// 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
// 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
}
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
// NextScheduledTime returns the time of when this task is to run next
NextScheduledTime() time.Time
// LastRunTime returns the time of last run
LastRunTime() time.Time
// Duration returns interval between runs
Duration() time.Duration
// 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
// Do accepts a function that should be called every time the task runs
Do(taskName string, task interface{}, params ...interface{}) Task
}
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
func NewTaskOnWeekday ¶
NewTaskOnWeekday creates a new task to execute on specific day of the week.
func NewTaskWithID ¶
NewTaskWithID creates a new task from the given id and format string.
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 )