tasks

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2023 License: MIT Imports: 14 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

This section is empty.

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 WithTickerInterval

func WithTickerInterval(tickerInterval time.Duration) Option

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

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
	// 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

func NewTask(format string) (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) Task

NewTaskAtIntervals creates a new task with the time interval.

func NewTaskDaily

func NewTaskDaily(hour, minute int) Task

NewTaskDaily creates a new task to execute daily at specific time

func NewTaskOnWeekday

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

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

func NewTaskWithID

func NewTaskWithID(id, format string) (Task, error)

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
)

Jump to

Keyboard shortcuts

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