worker

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package worker provides a thread pool implementation for concurrent execution of jobs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	WorkerCount    int           // Number of worker goroutines
	MaxRetries     int           // Maximum retry attempts per job
	RetryDelay     time.Duration // Initial retry delay
	MaxRetryDelay  time.Duration // Maximum retry delay
	BackoffFactor  float64       // Exponential backoff multiplier
	RateLimitDelay time.Duration // Delay when rate limited
	QueueSize      int           // Size of job queue buffer
	LogVerbose     bool          // Enable verbose logging
}

Config holds configuration for the worker pool

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a sensible default configuration

type Job

type Job struct {
	ID          string
	Description string
	Execute     func(ctx context.Context) error
	Status      JobStatus
	Attempts    int
	MaxRetries  int
	LastError   error
	CreatedAt   time.Time
	CompletedAt *time.Time
	// contains filtered or unexported fields
}

Job represents a unit of work to be executed

func (*Job) GetError

func (j *Job) GetError() error

GetError safely retrieves the last error

func (*Job) GetStatus

func (j *Job) GetStatus() JobStatus

GetStatus safely retrieves the job status

func (*Job) SetError

func (j *Job) SetError(err error)

SetError safely sets the last error

func (*Job) SetStatus

func (j *Job) SetStatus(status JobStatus)

SetStatus safely updates the job status

type JobStatus

type JobStatus int

JobStatus represents the current status of a job

const (
	// JobPending indicates a job is waiting to be processed
	JobPending JobStatus = iota
	JobRunning
	JobCompleted
	JobFailed
	JobRetrying
)

Job status constants

func (JobStatus) String

func (s JobStatus) String() string

type Pool

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

Pool manages a pool of workers that execute jobs concurrently

func NewPool

func NewPool(config *Config) *Pool

NewPool creates a new worker pool with the given configuration

func (*Pool) ForceKillStuckJobs

func (p *Pool) ForceKillStuckJobs(threshold time.Duration) int

ForceKillStuckJobs attempts to cancel jobs that have been stuck for too long

func (*Pool) GetJob

func (p *Pool) GetJob(id string) (*Job, bool)

GetJob retrieves a job by ID

func (*Pool) GetStats

func (p *Pool) GetStats() Stats

GetStats returns current pool statistics

func (*Pool) GetStuckJobs

func (p *Pool) GetStuckJobs(threshold time.Duration) []*Job

GetStuckJobs returns jobs that have been running for longer than the specified duration

func (*Pool) IsRateLimited

func (p *Pool) IsRateLimited() bool

IsRateLimited returns true if the pool is currently rate limited

func (*Pool) SetRateLimited

func (p *Pool) SetRateLimited(limited bool)

SetRateLimited marks the pool as being rate limited

func (*Pool) Start

func (p *Pool) Start()

Start initializes and starts the worker pool

func (*Pool) Stop

func (p *Pool) Stop()

Stop gracefully shuts down the worker pool

func (*Pool) Submit

func (p *Pool) Submit(job *Job) error

Submit adds a job to the worker pool

type Stats

type Stats struct {
	TotalJobs     int64
	CompletedJobs int64
	FailedJobs    int64
	RetryJobs     int64
	ActiveJobs    int64
	// contains filtered or unexported fields
}

Stats tracks pool statistics

func (*Stats) GetStats

func (s *Stats) GetStats() Stats

GetStats returns a copy of current statistics

Jump to

Keyboard shortcuts

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