Documentation
¶
Overview ¶
Package task provides an interface to schedule background delayed tasks
Index ¶
Constants ¶
const Now = time.Duration(0)
Now can be used if a one time task needs to be run immediately. It shouldn't be used with a recurring task
Variables ¶
var Manager *manager
Manager is the global single task manager instance
Functions ¶
func Init ¶
func Init(repo repository.Repository) error
Init intializes the global task manager instance
Types ¶
type Stat ¶
type Stat struct {
TaskUID string
Name string
Status Status
NextRun time.Time
Type model.TaskType
LastStatus model.TaskResult // Not used with one time tasks
LastRun time.Time // Not used with one time tasks
LastError error // Not used with one time tasks
Interval time.Duration // Not used with one time tasks
}
Stat contains the information about a current running or scheduled task Some fields are populated depending on the type (recurring or one-time)
type Task ¶
type Task interface {
// UID is an unique identifier for a check
// History is kept by linking the UID's of tasks
// Changing the UID will make you lose all the task history
// Changing the frontend name can be done with the Name() function
UID() string
// Name is an user friendly task name
// You can change this as much as you like
Name() string
// Interval returns the time between executions.
// For one time tasks it represents the amount of time to wait before executing
// For immediate execution task.Now can be used
Interval() time.Duration
Func() func(context.Context) error
Ctx() context.Context
}
Task is the interface to whcih a task should adhere to You can manually implement all methods are make use of the `NewTask` function which will automatically add some logging
func NewTask ¶
func NewTask(uid string, name string, interval time.Duration, fn func(context.Context) error, ctx ...context.Context) Task
NewTask creates a new task It supports an optional context, if none is given the background context is used Logs (info level) when a task starts Logs (error level) any error that occurs during the task execution