jobqueue

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package jobqueue 提供带优先级、进度上报、生命周期事件的任务队列。

借鉴 BullMQ 的架构分层(Queue / Worker / Events):

  • Queue: 投递任务,支持优先级(数值越小优先级越高)、延迟投递;
  • Worker: N 个 goroutine 并发消费,支持 Pause/Resume;
  • EventHook: 生命周期事件回调(Submit/Start/Progress/Complete/Fail), 用于构建可观测性(日志、metrics、dashboard)。

与 pkg/orchestration/scheduler 的区别:

  • scheduler 是 FIFO channel + 工作池,不支持优先级/进度/事件;
  • jobqueue 用 priority 堆做排序,每个 Job 有状态机 + 进度回调。

与 pkg/orchestration/delayqueue 的区别:

  • delayqueue 仅"到点触发回调",不关心执行状态与并发控制;
  • jobqueue 关注"完整的 Job 生命周期":排队→执行→报告进度→完成/失败。

进程内实现(不持久化);分布式持久化版本见 contrib/redisqueue。 零值不可用,用 New 构造。并发安全。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReportProgress

func ReportProgress(ctx context.Context, percent float64)

ReportProgress 在 Job.Fn 内调用,上报执行进度(0~100)。 若 ctx 中无 reporter(非 jobqueue 执行),则静默忽略。

Types

type Config

type Config struct {
	Workers   int       // worker 数量,默认 4
	QueueSize int       // 内部就绪信号缓冲,默认 1024
	Hook      EventHook // 事件钩子(nil=不回调)
	OnPanic   func(job *Job, r any, stack []byte)
}

Config 配置。

type Event

type Event struct {
	Type EventType
	Job  *Job
	Err  error // 仅 EventFail 时有值
}

Event 一个生命周期事件。

type EventHook

type EventHook interface {
	OnEvent(event Event)
}

EventHook 事件回调接口。实现者可选择性处理感兴趣的事件。 回调在 worker goroutine 中同步调用,应尽量轻量(如写 channel / 递增 metric)。

type EventHookFunc

type EventHookFunc func(Event)

EventHookFunc 函数适配器。

func (EventHookFunc) OnEvent

func (f EventHookFunc) OnEvent(e Event)

type EventType

type EventType int

EventType 事件类型。

const (
	EventSubmit   EventType = iota // 任务入队
	EventStart                     // 开始执行
	EventProgress                  // 进度更新
	EventComplete                  // 执行成功
	EventFail                      // 执行失败(含重试耗尽)
	EventRetry                     // 即将重试
)

func (EventType) String

func (e EventType) String() string

type Job

type Job struct {
	// ID 任务唯一标识。
	ID string
	// Name 任务名称(用于日志/metrics 分组)。
	Name string
	// Priority 优先级:数值越小越优先(0 最高)。默认 0。
	Priority int
	// Payload 任务数据(业务自定义)。
	Payload any
	// Fn 执行函数。ctx 携带 ProgressReporter,可通过 ReportProgress 上报进度。
	Fn func(ctx context.Context, job *Job) error
	// MaxRetries 最大重试次数(0=不重试)。
	MaxRetries int
	// RetryDelay 重试基础延迟(第 n 次 = delay * 2^n)。
	RetryDelay time.Duration
	// Delay 延迟执行:投递后等 Delay 再进入就绪队列。0=立即就绪。
	Delay time.Duration
	// Timeout 单次执行超时(0=不限)。
	Timeout time.Duration

	// State 当前状态。
	State JobState
	// Attempts 已尝试次数。
	Attempts int
	// Progress 当前进度(0~100)。
	Progress float64
	// Err 最近一次执行错误。
	Err error
	// CreatedAt 创建时间。
	CreatedAt time.Time
	// StartedAt 开始执行时间。
	StartedAt time.Time
	// CompletedAt 完成时间。
	CompletedAt time.Time
	// contains filtered or unexported fields
}

Job 是一个待执行的任务。

type JobState

type JobState int

JobState 任务状态。

const (
	StateWaiting   JobState = iota // 在队列中等待执行
	StateDelayed                   // 延迟中(到期后转 Waiting)
	StateActive                    // 正在被 worker 执行
	StateCompleted                 // 执行成功
	StateFailed                    // 执行失败
)

func (JobState) String

func (s JobState) String() string

type Option

type Option func(*Config)

Option 配置函数。

func WithHook

func WithHook(h EventHook) Option

WithHook 设置事件钩子。

func WithHookFunc

func WithHookFunc(fn func(Event)) Option

WithHookFunc 用函数设置事件钩子。

func WithPanicHandler

func WithPanicHandler(fn func(job *Job, r any, stack []byte)) Option

WithPanicHandler 设置 panic 处理。

func WithQueueSize

func WithQueueSize(n int) Option

WithQueueSize 设置就绪信号缓冲大小。默认 1024。

func WithWorkers

func WithWorkers(n int) Option

WithWorkers 设置 worker 并发数。默认 4。

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

Queue 带优先级的任务队列。

func New

func New(opts ...Option) *Queue

New 创建任务队列(未启动)。用 Start 启动 worker。

func (*Queue) Cancel

func (q *Queue) Cancel(id string) bool

Cancel 取消指定 ID 的任务(仅 Waiting/Delayed 状态可取消)。

func (*Queue) Pause

func (q *Queue) Pause()

Pause 暂停消费(不影响投递)。

func (*Queue) Paused

func (q *Queue) Paused() bool

Paused 是否暂停。

func (*Queue) Pending

func (q *Queue) Pending() int

Pending 返回等待中的任务数(近似)。

func (*Queue) Resume

func (q *Queue) Resume()

Resume 恢复消费。

func (*Queue) Start

func (q *Queue) Start(ctx context.Context) error

Start 启动 worker 池和延迟任务调度器。ctx 取消时优雅停止。满足 beauty.Service。

func (*Queue) Stop

func (q *Queue) Stop()

Stop 停止队列。

func (*Queue) String

func (q *Queue) String() string

String 满足 beauty.Service。

func (*Queue) Submit

func (q *Queue) Submit(job *Job) bool

Submit 投递一个任务。返回 false 表示队列已停止。

Jump to

Keyboard shortcuts

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