schedx

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: MIT Imports: 10 Imported by: 0

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:

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

View Source
const MaxParallel = 10000

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error uint8

Error defines error codes of scheduler +genx:code

const (
	ERROR_UNDEFINED               Error = iota
	ERROR__REACH_MAX_PENDING            // reached max pending limitation
	ERROR__SCHEDULER_RERUN              // scheduler is already running
	ERROR__SCHEDULER_CANCELED           // scheduler is manual canceled
	ERROR__SCHEDULER_JOB_PANICKED       // scheduler job panicked
)

func (Error) Message

func (e Error) Message() string

type ExitingCallback

type ExitingCallback[T any] func([]T, error)

type HandlerCallback

type HandlerCallback[T any] func(T, error)

type Job

type Job[T any] interface {
	Do(context.Context, T) error
}

type JobFunc

type JobFunc[T any] func(context.Context, T) error

func (JobFunc[T]) Do

func (f JobFunc[T]) Do(ctx context.Context, v T) error

type ScheduleMode

type ScheduleMode int
const (
	FIFO ScheduleMode = iota
	LIFO
)

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.

type Tasks

type Tasks[T any] interface {
	Len() int
	Push(T)
	Pop() (T, bool)
	Range(func(T) bool)
	Clear()
}

Jump to

Keyboard shortcuts

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