Documentation
¶
Overview ¶
Package taskkit 提供自动启动的任务调度、任务执行上下文和受控并发执行能力。
Index ¶
- Variables
- func Parallel[T any](ctx context.Context, items []T, fn func(context.Context, T) error, ...) error
- type Context
- type ItemError
- type Job
- type JobOption
- type JobStatus
- type Manager
- func (m *Manager) Add(id string, schedule Schedule, task Task, options ...JobOption) (*Job, error)
- func (m *Manager) Get(id string) (*Job, bool)
- func (m *Manager) List() []*Job
- func (m *Manager) Pause(ctx context.Context) error
- func (m *Manager) Remove(id string) error
- func (m *Manager) Resume() error
- func (m *Manager) Run(id string) error
- func (m *Manager) Shutdown(ctx context.Context) error
- func (m *Manager) Update(id string, schedule Schedule, task Task, options ...JobOption) (*Job, error)
- type ManagerOption
- type PanicError
- type ParallelOption
- type Result
- type Schedule
- type Task
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrManagerClosed 表示任务管理器已经永久关闭。 ErrManagerClosed = errx.New("task manager is closed") // ErrManagerPaused 表示任务管理器当前暂停调度。 ErrManagerPaused = errx.New("task manager is paused") // ErrContextRequired 表示没有提供标准 Context。 ErrContextRequired = errx.New("context must not be nil") // ErrJobIDRequired 表示任务业务标识为空。 ErrJobIDRequired = errx.New("job id must not be empty") // ErrJobNotFound 表示指定任务不存在。 ErrJobNotFound = errx.New("job not found") // ErrJobAlreadyExists 表示任务业务标识已经注册。 ErrJobAlreadyExists = errx.New("job already exists") // ErrScheduleRequired 表示没有提供任务调度规则。 ErrScheduleRequired = errx.New("job schedule must not be nil") // ErrTaskRequired 表示没有提供任务执行函数。 ErrTaskRequired = errx.New("job task must not be nil") // ErrInvalidTimeout 表示超时时间不是正数。 ErrInvalidTimeout = errx.New("timeout must be greater than zero") // ErrInvalidParallelLimit 表示并发上限不是正数。 ErrInvalidParallelLimit = errx.New("parallel limit must be greater than zero") // ErrPanicRecovered 表示任务执行期间发生的 panic 已被恢复。 ErrPanicRecovered = errx.New("task panic recovered") )
Functions ¶
Types ¶
type Context ¶
Context 在标准 context.Context 上提供当前任务执行的只读元数据。
func FromContext ¶
FromContext 返回当前 context 中的任务执行元数据视图。
type JobOption ¶
type JobOption func(*jobConfig) error
JobOption 调整单个任务的执行配置。
func WithIntervalFromCompletion ¶
func WithIntervalFromCompletion() JobOption
WithIntervalFromCompletion 使 Every 从上一次执行完成时间计算下一次运行时间。
type JobStatus ¶
type JobStatus struct {
Running int
LastResult Result
LastStartedAt time.Time
LastCompletedAt time.Time
NextRunAt time.Time
}
JobStatus 是任务当前运行状态的只读快照。
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager 集中管理进程内的任务定义、调度和运行状态。
func NewManager ¶
func NewManager(options ...ManagerOption) (*Manager, error)
NewManager 创建并立即启动任务管理器,同时登记进程解构函数。
type ManagerOption ¶
type ManagerOption func(*managerConfig) error
ManagerOption 调整 Manager 的公共配置。
func WithBaseContext ¶
func WithBaseContext(ctx context.Context) ManagerOption
WithBaseContext 设置所有任务的根 Context;取消该 Context 会停止后续调度。
func WithLocation ¶
func WithLocation(location *time.Location) ManagerOption
WithLocation 设置 Cron 和固定时间任务使用的时区。
func WithShutdownTimeout ¶
func WithShutdownTimeout(timeout time.Duration) ManagerOption
WithShutdownTimeout 设置生命周期自动关闭时等待任务退出的最长时间。
type PanicError ¶
PanicError 保存已恢复 panic 的值和调用堆栈。
func (*PanicError) Error ¶
func (e *PanicError) Error() string
func (*PanicError) Unwrap ¶
func (e *PanicError) Unwrap() error
type ParallelOption ¶
type ParallelOption func(*parallelConfig) error
ParallelOption 调整 Parallel 的执行策略。
func WithItemTimeout ¶
func WithItemTimeout(timeout time.Duration) ParallelOption
WithItemTimeout 设置每个元素单独执行的最长时间。
type Schedule ¶
Schedule 描述任务的调度规则。具体实现由 taskkit 提供。
func CronWithSeconds ¶
CronWithSeconds 创建使用六段表达式且包含秒字段的 Cron 调度规则。
Source Files
¶
Click to show internal directories.
Click to hide internal directories.