taskscheduler

package
v1.19.5 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCancelled = errors.New("task cancelled")

ErrCancelled is returned by a TaskRunner when the task was cancelled via cancelCh.

Functions

func GetRegisteredTaskTypes

func GetRegisteredTaskTypes() []model.TaskType

GetRegisteredTaskTypes returns all registered task type names.

func ListLogStoreBackendNames

func ListLogStoreBackendNames() []string

ListLogStoreBackendNames returns the names of all registered backends. The order is not specified.

func NewTaskLogger

func NewTaskLogger(ctx context.Context, backendName string, taskID string) kitlog.Logger

newTaskLogger creates a logger that forwards Log() calls to the log storage service for the given task.

func RegisterFuncTaskType

func RegisterFuncTaskType(taskType model.TaskType, factory func(ctx context.Context, t *model.Task, progressCallback ProgressCallback, cancelCh <-chan struct{}) (result interface{}, err error))

RegisterTaskType registers a task type and its factory. Panics if the type is already registered.

func RegisterLogStoreBackend

func RegisterLogStoreBackend(name string, factory LogStoreBackendFactory)

RegisterLogStoreBackend adds a backend factory under the given name.

func RegisterScheduledJob

func RegisterScheduledJob(def *ScheduledJobDef)

RegisterScheduledJob registers a scheduled job definition. Panics if ID is already registered.

func RegisterTaskType

func RegisterTaskType(taskType model.TaskType, factory TaskRunner)

RegisterTaskType registers a task type and its factory. Panics if the type is already registered.

func SetDefaultLogStore

func SetDefaultLogStore(name string)

SetDefaultLogStore sets the default backend name (used when Get is called with empty name).

Types

type LogStoreBackend

type LogStoreBackend interface {
	// Write persists a log entry.
	Write(ctx context.Context, refID, level, message string) error
	// ListByTaskID returns log entries for the given ref ID and type, ordered by created_at asc.
	ListByTaskID(ctx context.Context, refID string) ([]model.TaskLog, error)
}

LogStoreBackend is the interface for log storage. Implementations can use database, file, etc.

func GetLogStoreBackend

func GetLogStoreBackend(name string) LogStoreBackend

GetLogStoreBackend returns the Backend for the given name. If name is empty, returns the default backend. Returns nil if the name is not registered.

func NewDatabaseLogStoreBackend

func NewDatabaseLogStoreBackend() LogStoreBackend

NewDatabaseLogStoreBackend returns a Backend that stores logs in the database.

type LogStoreBackendFactory

type LogStoreBackendFactory func() LogStoreBackend

LogStoreBackendFactory creates a Backend instance.

type PayloadBuilder

type PayloadBuilder func() string

PayloadBuilder returns the JSON payload for a scheduled task run. Called each time the schedule fires.

type ProgressCallback

type ProgressCallback func(progress int)

ProgressCallback is called to report task progress (0-100).

type ScheduledJobDef

type ScheduledJobDef struct {
	ID             string
	Name           string
	Spec           string // cron expression, e.g. "0 0 * * *" for daily at midnight, If the Schedule attribute is not empty, this value is only used for the front display.
	Schedule       cron.Schedule
	Description    string
	TaskType       model.TaskType
	PayloadBuilder PayloadBuilder
	MaxRetries     int
	Enabled        bool       // if false, the job is not added to cron (or is removed)
	Runner         TaskRunner // if nil, the task type is not registered by the scheduler, but by the task service directly

	// DisableNotAfter disables the automatic not_after deadline for tasks created
	// by this job. By default (false), not_after is set to the next scheduled fire
	// time so that a task missed within its scheduling window is auto-expired rather
	// than running stale. Set to true for long-running or one-shot jobs that should
	// not be discarded if they cannot start immediately.
	DisableNotAfter bool
}

ScheduledJobDef defines a scheduled job. Registered in code; not persisted to DB.

func GetScheduledJob

func GetScheduledJob(id string) *ScheduledJobDef

GetScheduledJob returns the job definition by ID, or nil.

func ListScheduledJobs

func ListScheduledJobs() []*ScheduledJobDef

ListScheduledJobs returns all registered scheduled job definitions.

type TaskRunner

type TaskRunner interface {
	Run(ctx context.Context, t *model.Task, progressCallback ProgressCallback, cancelCh <-chan struct{}) (result interface{}, err error)
}

TaskRunner runs a single task. Run should respect cancelCh: when it is closed, the runner should stop and return. progressCallback can be called to update progress during execution.

func GetTaskRunner

func GetTaskRunner(ctx context.Context, taskType model.TaskType) (TaskRunner, bool)

GetTaskRunner returns a runner for the given task type, or (nil, false) if not registered.

func NewFuncTaskRunner

func NewFuncTaskRunner(factory func(ctx context.Context, t *model.Task, progressCallback ProgressCallback, cancelCh <-chan struct{}) (result interface{}, err error)) TaskRunner

Jump to

Keyboard shortcuts

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