threading

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package threading provides tools for multi threading applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CallbackFunc

type CallbackFunc = func(id string, isRunning bool, lastRunTime *time.Time)

CallbackFunc describes the interface for a func called before and after running a job.

type DoWork

type DoWork = func(ctx context.Context, logger *slog.Logger) error

DoWork describes the interface for work executed by the workers.

type EventQueue

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

EventQueue is used to divide [Subscriber]s between [Worker]s. This prevents one Worker of being very busy.

func NewEventQueue

func NewEventQueue(
	logger *slog.Logger,
	maxWorkers int,
	channelBufferSize int,
) *EventQueue

NewEventQueue creates a new EventQueue.

func (*EventQueue) AddSubscriber

func (q *EventQueue) AddSubscriber(sub Subscriber)

AddSubscriber adds a Subscriber to the EventQueue.

func (*EventQueue) EnqueueEvent

func (q *EventQueue) EnqueueEvent(event any)

EnqueueEvent puts an event on the Worker channels.

func (*EventQueue) RemoveSubscriber

func (q *EventQueue) RemoveSubscriber(sub Subscriber)

RemoveSubscriber removes a Subscriber from the EventQueue.

type Job

type Job interface {
	ID() string
	Run(context.Context, *slog.Logger) error
	RunEvery() time.Duration
}

Job describes the interface for a job executable by the jobqueue.

type JobQueue

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

JobQueue is a queue of Jobs which will be executed by the workerpool.

func NewJobQueue

func NewJobQueue(logger *slog.Logger, amountWorkers int, size int) *JobQueue

NewJobQueue creates a new jobqueue.

func (*JobQueue) AddJob

func (q *JobQueue) AddJob(job Job, callback CallbackFunc) error

AddJob adds a recurring job which should be executed by the workerpool. This will also execute the job.

func (*JobQueue) Clear

func (q *JobQueue) Clear()

Clear clears the JobQueue completely.

func (*JobQueue) FetchJobIDs

func (q *JobQueue) FetchJobIDs() []string

FetchJobIDs fetches all IDs for all jobs.

func (*JobQueue) FetchState

func (q *JobQueue) FetchState(id string) (bool, *time.Time)

FetchState fetches the current state of the specified job.

func (*JobQueue) ForceRun

func (q *JobQueue) ForceRun(id string)

ForceRun forces a run of the specified job.

type Subscriber

type Subscriber interface {
	ID() string
	OnEventCallback(event any)
}

Subscriber describes the interface of subscribers of new events.

type Worker

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

Worker is used to handle work of the WorkerPool.

func NewWorker

func NewWorker(id int, pool *WorkerPool) Worker

NewWorker creates a new Worker.

func (*Worker) Active

func (worker *Worker) Active() bool

Active fetches the current state of the worker.

func (*Worker) IsDoingWork added in v0.3.1

func (worker *Worker) IsDoingWork() bool

IsDoingWork fetches the current state of the worker.

func (*Worker) Run

func (worker *Worker) Run(ctx context.Context, logger *slog.Logger) error

Run makes Worker start doing work.

func (*Worker) Stop

func (worker *Worker) Stop()

Stop stops the worker.

type WorkerPool

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

WorkerPool is used to divide [Subscriber]s between [Worker]s. This prevents one Worker of being very busy.

func NewWorkerPool

func NewWorkerPool(
	logger *slog.Logger,
	amountWorkers int,
	queueSize int,
) *WorkerPool

NewWorkerPool creates a new WorkerPool.

func (*WorkerPool) Active

func (pool *WorkerPool) Active() bool

Active checks if the WorkerPool is active by checking if any Worker is active.

func (*WorkerPool) EnqueueWork

func (pool *WorkerPool) EnqueueWork(doWork DoWork)

EnqueueWork puts an work on the queue.

func (*WorkerPool) IsDoingWork added in v0.3.1

func (pool *WorkerPool) IsDoingWork() bool

IsDoingWork checks if the WorkerPool is still processing work.

func (*WorkerPool) IsWorkRemaining

func (pool *WorkerPool) IsWorkRemaining() bool

IsWorkRemaining checks if there is still work on the queue.

func (*WorkerPool) Start

func (pool *WorkerPool) Start()

Start starts [Worker]s of a WorkerPool if they weren't active yet.

func (*WorkerPool) Stop

func (pool *WorkerPool) Stop()

Stop stops all workers.

func (*WorkerPool) WaitUntilDone

func (pool *WorkerPool) WaitUntilDone()

WaitUntilDone blocks until the queue is empty.

Jump to

Keyboard shortcuts

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