artifex

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2022 License: MIT Imports: 6 Imported by: 6

README

GoDoc

artifex

Simple in-memory job queue for Golang using worker-based dispatching

Documentation here: https://godoc.org/github.com/muety/artifex

Cron jobs use the robfig/cron library: https://godoc.org/github.com/robfig/cron

Forked from: https://github.com/mborders/artifex

Example Usage

// 10 workers, 100 max in job queue
d := artifex.NewDispatcher(10, 100)
d.Start()

d.Dispatch(func() {
  // do something
})

err := d.DispatchIn(func() {
  // do something in 500ms
}, time.Millisecond*500)

// Returns a DispatchTicker
dt, err := d.DispatchEvery(func() {
  // do something every 250ms
}, time.Millisecond*250)

// Stop a given DispatchTicker
dt.Stop()

// Returns a DispatchCron
dc, err := d.DispatchCron(func() {
  // do something every 1s
}, "*/1 * * * * *")

// Stop a given DispatchCron
dc.Stop()

// Stop a dispatcher and all its workers/tickers
d.Stop()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DispatchCron

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

DispatchCron represents a dispatched cron job that executes using cron expression formats.

func (*DispatchCron) Stop

func (c *DispatchCron) Stop()

Stops ends the execution cycle for the given cron.

type DispatchTicker

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

DispatchTicker represents a dispatched job ticker that executes on a given interval. This provides a means for stopping the execution cycle from continuing.

func (*DispatchTicker) Stop

func (dt *DispatchTicker) Stop()

Stop ends the execution cycle for the given ticker.

type Dispatcher

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

Dispatcher maintains a pool for available workers and a job queue that workers will process

func NewDispatcher

func NewDispatcher(maxWorkers int, maxQueue int) *Dispatcher

NewDispatcher creates a new dispatcher with the given number of workers and buffers the job queue based on maxQueue. It also initializes the channels for the worker pool and job queue

func (*Dispatcher) CountDispatched

func (d *Dispatcher) CountDispatched() int

func (*Dispatcher) CountEnqueued

func (d *Dispatcher) CountEnqueued() int

func (*Dispatcher) Dispatch

func (d *Dispatcher) Dispatch(run func()) error

Dispatch pushes the given job into the job queue. The first available worker will perform the job

func (*Dispatcher) DispatchBatch

func (d *Dispatcher) DispatchBatch(runs []func(), gap time.Duration) error

DispatchBatch pushes multiple given jobs into the job queue optionally separated from each other by a given time gap

func (*Dispatcher) DispatchCron

func (d *Dispatcher) DispatchCron(run func(), cronStr string) (*DispatchCron, error)

DispatchEvery pushes the given job into the job queue each time the cron definition is met

func (*Dispatcher) DispatchCronBatch

func (d *Dispatcher) DispatchCronBatch(runs []func(), cronStr string, gap time.Duration) (*DispatchCron, error)

DispatchCronBatch pushes a job into the queue to dispatch a batch of jobs once the given definition is met. Jobs can optionally be separated from each other by a given time gap.

func (*Dispatcher) DispatchEvery

func (d *Dispatcher) DispatchEvery(run func(), interval time.Duration) (*DispatchTicker, error)

DispatchEvery pushes the given job into the job queue continuously at the given interval

func (*Dispatcher) DispatchIn

func (d *Dispatcher) DispatchIn(run func(), duration time.Duration) error

DispatchIn pushes the given job into the job queue after the given duration has elapsed

func (*Dispatcher) Start

func (d *Dispatcher) Start()

Start creates and starts workers, adding them to the worker pool. Then, it starts a select loop to wait for job to be dispatched to available workers

func (*Dispatcher) Stop

func (d *Dispatcher) Stop()

Stop ends execution for all workers/tickers and closes all channels, then removes all workers/tickers

type Job

type Job struct {
	Run func()
}

Job represents a runnable process, where Start will be executed by a worker via the dispatch queue

type Worker

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

Worker attaches to a provided worker pool, and looks for jobs on its job channel

func NewWorker

func NewWorker(workerPool chan chan Job) *Worker

NewWorker creates a new worker using the given id and attaches to the provided worker pool. It also initializes the job/quit channels

func (Worker) Start

func (w Worker) Start()

Start initializes a select loop to listen for jobs to execute

func (Worker) Stop

func (w Worker) Stop()

Stop will end the job select loop for the worker

Jump to

Keyboard shortcuts

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