task

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2026 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package task provides an interface to schedule background recurring tasks

Index

Constants

This section is empty.

Variables

View Source
var (
	IntervalOnce     = time.Duration(0)
	ErrTaskExists    = errors.New("task already exists")
	ErrTaskNotExists = errors.New("task doesn't exist")
)
View Source
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
	LastRun   time.Time
	Interval  time.Duration
	Recurring bool
}

Stat contains the information about a current running or scheduled task

type Status

type Status string
const (
	Waiting Status = "waiting"
	Running Status = "running"
)

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.
	// An interval == IntervalOnce means it will only run once.
	Interval() time.Duration
	// Hidden determines if the task is returned when Tasks is
	// called on the manager.
	Hidden() bool
	// The function that actually gets executed when it's time
	// The user slice contains all users for who the task needs to executed
	// In reality this will either be a single user (if the user started the task from the api)
	// or contain all users if it's a regular interval run
	// It's up to the function to decide how to handle it
	// If the returned task result does not contain one of the users that was given as argument
	// then the task result is not saved for that user
	Func() func(context.Context, []model.User) []TaskResult
	Ctx() context.Context
}

Task is the interface to which a task should adhere to You can manually implement all methods or make use of the `NewTask` function which will automatically add some logging

func NewTask

func NewTask(uid, name string, interval time.Duration, hidden bool, fn func(context.Context, []model.User) []TaskResult, 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 and ends Logs (error level) any error that occurs during the task execution

type TaskResult

type TaskResult struct {
	User    model.User
	Message string
	Error   error
}

TaskResult is the expected return from the actual task function

Jump to

Keyboard shortcuts

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