Documentation
¶
Overview ¶
Package schedx provides a concurrent job scheduler with bounded pending queues and graceful shutdown.
The core type is Scheduler. Callers enqueue work with Scheduler.Push, start worker goroutines with Scheduler.Run, and shut down with Scheduler.Close. Jobs are handled by Job or JobFunc. Internally, Scheduler.Run creates a nest to track workers and orchestrate cancellation.
Key features:
- FIFO (default) or LIFO scheduling via WithFifoScheduleMode / WithLifoScheduleMode
- Configurable concurrency (WithParallel) and pending limit (WithMaxPending)
- Per-job completion callback (WithCallback) and exit callback for remaining tasks (WithExitCallback)
- Safe close with optional timeout (WithCloseTimeout); default close timeout is 3s
- By default, running jobs use context.WithoutCancel so they are detached from scheduler cancellation; use WithoutDetached to propagate cancel
Lifecycle:
NewScheduler → Run → Push* → Close
Scheduler.Run may succeed only once. After Scheduler.Close, Scheduler.Push returns ERROR__SCHEDULER_CANCELED. Pushing when the pending limit is reached returns ERROR__REACH_MAX_PENDING.
Usage:
s := schedx.NewScheduler(
schedx.JobFunc[int](func(ctx context.Context, v int) error {
return nil
}),
schedx.WithMaxPending[int](100),
schedx.WithParallel[int](8),
schedx.WithCallback(func(v int, err error) {}),
)
if err := s.Run(ctx); err != nil {
return err
}
defer func() { _ = s.Close() }()
if err := s.Push(ctx, 42); err != nil {
return err
}
For structured concurrency without a job queue, use package nest directly.
Code generated by genx:code DO NOT EDIT.
Index ¶
- Constants
- type Error
- type ExitingCallback
- type HandlerCallback
- type Job
- type JobFunc
- type ScheduleMode
- type Scheduler
- type SchedulerOptionApplier
- func WithCallback[T any](cb HandlerCallback[T]) SchedulerOptionApplier[T]
- func WithCloseTimeout[T any](du time.Duration) SchedulerOptionApplier[T]
- func WithExitCallback[T any](cb ExitingCallback[T]) SchedulerOptionApplier[T]
- func WithFifoScheduleMode[T any]() SchedulerOptionApplier[T]
- func WithLifoScheduleMode[T any]() SchedulerOptionApplier[T]
- func WithMaxPending[T any](maxPending int) SchedulerOptionApplier[T]
- func WithParallel[T any](parallel int) SchedulerOptionApplier[T]
- func WithoutDetached[T any]() SchedulerOptionApplier[T]
- func WithoutPendingLimitation[T any]() SchedulerOptionApplier[T]
- type Tasks
Constants ¶
const MaxParallel = 10000
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ExitingCallback ¶
type HandlerCallback ¶
type Scheduler ¶
type Scheduler[T any] interface { Push(context.Context, T) error Run(context.Context) error Pending() int Close() error }
func NewScheduler ¶
func NewScheduler[T any](fn Job[T], appliers ...SchedulerOptionApplier[T]) Scheduler[T]
type SchedulerOptionApplier ¶
type SchedulerOptionApplier[T any] func(*option[T])
func WithCallback ¶
func WithCallback[T any](cb HandlerCallback[T]) SchedulerOptionApplier[T]
WithCallback sets a callback that is invoked after task execution.
func WithCloseTimeout ¶
func WithCloseTimeout[T any](du time.Duration) SchedulerOptionApplier[T]
WithCloseTimeout sets timeout to handle post-close when Scheduler.Close The default value is 0 means waiting all paralleled goroutines exited gracefully
func WithExitCallback ¶
func WithExitCallback[T any](cb ExitingCallback[T]) SchedulerOptionApplier[T]
WithExitCallback sets a callback invoked when the scheduler exits. It receives remaining pending jobs and the exit reason. The callback is guaranteed to be called at most once.
func WithFifoScheduleMode ¶
func WithFifoScheduleMode[T any]() SchedulerOptionApplier[T]
WithFifoScheduleMode sets the scheduler mode to FIFO.
func WithLifoScheduleMode ¶
func WithLifoScheduleMode[T any]() SchedulerOptionApplier[T]
WithLifoScheduleMode sets the scheduler mode to LIFO.
func WithMaxPending ¶
func WithMaxPending[T any](maxPending int) SchedulerOptionApplier[T]
WithMaxPending sets the maximum number of pending tasks allowed. the default value is 1.
func WithParallel ¶
func WithParallel[T any](parallel int) SchedulerOptionApplier[T]
WithParallel sets the number of tasks processed in parallel. the default value is 1.
func WithoutDetached ¶
func WithoutDetached[T any]() SchedulerOptionApplier[T]
WithoutDetached (Decouple from parent lifecycle) If false(the default mode), the internal context is derived using context.WithoutCancel(parent). This allows the popped task to ignore cancellation signals or deadlines from the parent context, while still inheriting and propagating existing context values (e.g., TraceID, UserID).
func WithoutPendingLimitation ¶
func WithoutPendingLimitation[T any]() SchedulerOptionApplier[T]
WithoutPendingLimitation disables the pending-task limit by setting it to -1.