task

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2025 License: GPL-3.0 Imports: 11 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 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
}

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.
	Interval() time.Duration
	// The function that actually gets executed when it's time
	// The user slice contains all users for who the task need to executed
	// In reality this will either be a single user (if the user started the task from the api)
	// Or it 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, 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