tasks

package
v0.27.261 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 15 Imported by: 8

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

View Source
const DefaultRunTimeoutInterval = time.Second

DefaultRunTimeoutInterval specify a timeout for a task to start

View Source
const DefaultTickerInterval = time.Second

DefaultTickerInterval for scheduler

Variables

View Source
var TimeNow = time.Now

TimeNow is a function that returns the current time

Functions

func SetGlobalLocation

func SetGlobalLocation(newLocation *time.Location)

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 WithID added in v0.13.0

func WithID(id string) Option

WithID option to provide ID

func WithPublisher added in v0.16.0

func WithPublisher(publisher Publisher) Option

WithPublisher option to provide publisher

func WithRunTimeout added in v0.13.0

func WithRunTimeout(runTimeout time.Duration) Option

WithRunTimeout option to provide run timeout

func WithTickerInterval

func WithTickerInterval(tickerInterval time.Duration) Option

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

func ParseSchedule(format string) (*Schedule, error)

ParseSchedule parses a schedule string

func (*Schedule) Duration added in v0.13.0

func (s *Schedule) Duration() time.Duration

// Duration returns interval between runs

func (*Schedule) Equal added in v0.24.557

func (s *Schedule) Equal(other *Schedule) bool

Equal returns true if the schedules are equal

func (*Schedule) GetLastRun added in v0.18.1

func (s *Schedule) GetLastRun() *time.Time

GetLastRun returns the last run time

func (*Schedule) ShouldRun added in v0.13.0

func (s *Schedule) ShouldRun() bool

ShouldRun returns true if the task should be run now

func (*Schedule) UpdateNextRun added in v0.13.0

func (s *Schedule) UpdateNextRun() time.Time

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

func NewScheduler

func NewScheduler(ops ...Option) Scheduler

NewScheduler creates a new scheduler

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 New added in v0.13.0

func New(s *Schedule, ops ...Option) Task

New returns new task

func NewTask

func NewTask(format string, ops ...Option) (Task, error)

NewTask creates a new task from parsed format string. every %d seconds | minutes | ... Monday | .. | Sunday at %hh:mm

func NewTaskAtIntervals

func NewTaskAtIntervals(interval uint64, unit TimeUnit, ops ...Option) Task

NewTaskAtIntervals creates a new task with the time interval.

func NewTaskDaily

func NewTaskDaily(hour, minute int, ops ...Option) Task

NewTaskDaily creates a new task to execute daily at specific time

func NewTaskOnWeekday

func NewTaskOnWeekday(startDay time.Weekday, hour, minute int, ops ...Option) Task

NewTaskOnWeekday creates a new task to execute on specific day of the week.

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
)

Jump to

Keyboard shortcuts

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