tasks

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultManager = NewManager(time.Local)

DefaultManager is the default task manage of this package. Users of the DefaultManager must make sure that no unwanted tasks are registered automatically by the init() functions of other packages. For safety, users are adviced to use NewManager on their own and register all required tasks manually. Creators of tasks are adivced to export a RegisterOn(mng *Manager) method that can be used to register a task on a certain manager.

Functions

func Register

func Register(t *Task) error

Register registers t at the DefaultManager.

func Start

func Start(ctx context.Context)

Start starts the DefaultManager.

Types

type Manager

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

Manager manages and schedules tasks register on it. The manager must be started for any tasks to get scheduled. Once started tasks are scheduled as long as the manager is not stopped.

func NewManager

func NewManager(loc *time.Location) *Manager

NewManager returns a new task manager that is configured to interpret cron schedule as at the timezone loc. If loc is nil then time.UTC will be used.

func (*Manager) Register

func (mng *Manager) Register(task *Task) error

Register registers task at mng. An error can only be returned when the Schedule field of task cannot be parsed. If StartNow is set to true on task it will be executed as soon as the manager is started. If mng has already been started and StartNow is true the task is executed immediately in a dedicated go-routine.

func (*Manager) Start

func (mng *Manager) Start(ctx context.Context)

Start starts the manager and the cron scheduler. Tasks that are marked as StartNow are executed immediately in a dedicated goroutine.

func (*Manager) Stop

func (mng *Manager) Stop()

Stop stops the manager and waits for all running tasks to complete. Running tasks are NOT cancelled! If the user wants to cancel all tasks the context passed to mng.Start() should be cancelled instead. Afterwards a call to Stop() will make sure that no more tasks get scheduled and will wait for all running tasks to finish.

type Task

type Task struct {
	// Name is the name of the task.
	Name string

	// TaskFunc is the actual function that should be executed
	// when the task runs.
	TaskFunc TaskFunc

	// Deadline can be set to the maximum duration the task is
	// allowed to run. The context passed to TaskFunc is cancelled
	// with the deadline.
	Deadline time.Duration

	// StartNow can be set to true if the task should run immediately
	// when the task manager is started.
	StartNow bool

	// Schedule holds the cron schedule at which the task should be
	// executed.
	Schedule string
	// contains filtered or unexported fields
}

Task is a workload that runs async on a given cron schedule. The same task is guaranteed to not run twice.

func (*Task) LastResult

func (task *Task) LastResult() (time.Time, error)

LastResult returns time and the error of the last execution. If the task has not yet been executed a zero time is returned together with a nil error.

func (*Task) Run

func (task *Task) Run()

Run actually runs the task. Any panic thrown by the TaskFunc is caught and stored as the last execution error.

type TaskFunc

type TaskFunc func(context.Context) error

TaskFunc is that is executed when a task runs.

Notes

Bugs

  • see bug in Manager.Register(*Task)

  • right now it is possible for the task to run twice at the same time because the cron-schedule might also execute the task the next moment.

Jump to

Keyboard shortcuts

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