import "git.sr.ht/~sircmpwn/dowork"
dowork is a generic task queueing system for Go programs. It queues, executes, and reschedules tasks in Goroutine in-process.
A global task queue is provided for simple use-cases. To use it:
import ( "context" "git.sr.ht/~sircmpwn/dowork" ) work.Submit(func(ctx context.Context) error { // ...do work... return nil })
This task will be executed in the background. The first time a task is submitted to the global queue, it will be initialized and start running in the background.
To customize options like maximum retries and timeouts, use work.Enqueue:
task := work.NewTask(func(ctx context.Context) error { // ... }). Retries(5). // Maximum number of attempts MaxTimeout(10 * time.Minute). // Maximum timeout between attempts Within(10 * time.Second). // Deadline for each attempt After(func(ctx context.Context, err error) { // Executed once the task completes, successful or not }) work.Enqueue(task)
Retries are conducted with an exponential backoff.
You may also manage your own work queues. Use NewQueue() to obtain a queue, (*Queue).Dispatch() to execute all overdue tasks, and (*Queue).Start() to spin up a goroutine and start dispatching tasks automatically.
Use work.Shutdown() or (*Queue).Shutdown() to perform a soft shutdown of the queue, which will stop accepting new tasks and block until all already-queued tasks complete.
doc.go global.go queue.go task.go
var ( // Returned when a task is attempted which was already successfully completed. ErrAlreadyComplete = errors.New("This task was already successfully completed once") // If this is returned from a task function, the task shall not be re-attempted. ErrDoNotReattempt = errors.New("This task should not be re-attempted") // This task has been attempted too many times. ErrMaxRetriesExceeded = errors.New("The maximum retries for this task has been exceeded") // Set this function to influence the clock that will be used for // scheduling re-attempts. Now = func() time.Time { return time.Now().UTC() } )
var ( ErrQueueShuttingDown = errors.New("Queue is shutting down; new tasks are not being accepted") )
Enqueues a task in the global queue.
Shuts down any number of work queues in parallel and blocks until they're all finished.
func Shutdown()
Stops accepting new tasks and blocks until all queued tasks are completed.
func Start()
Ensures that the global queue is started
type Queue struct {
// contains filtered or unexported fields
}
Creates a new task queue. The name of the task queue is used in Prometheus label names and must match [a-zA-Z0-9:_] (snake case is used by convention).
Attempts any tasks which are due and updates the task schedule. Returns true if there is more work to do.
Enqueues a task.
An error will only be returned if the queue has been shut down.
Sets the function the queue will use to obtain the current time.
Runs the task queue. Blocks until the context is cancelled.
Stops accepting new tasks and blocks until all already-queued tasks are complete. The queue must have been started with Start, not Run.
Starts the task queue in the background. If you wish to use the warm shutdown feature, you must use Start, not Run.
Creates and enqueues a new task, returning the new task. Note that the caller cannot customize settings on the task without creating a race condition; so attempting to will panic. See NewTask and (*Queue).Enqueue to create tasks with customized options.
An error will only be returned if the queue has been shut down.
Stores state for a task which shall be or has been executed. Each task may only be executed successfully once.
Creates a new task for a given function.
See (*Queue).Submit
Sets a function which will be executed once the task is completed, successfully or not. The final result (nil or an error) is passed to the callee.
Attempts to execute this task.
If successful, the zero time and nil are returned.
Otherwise, the error returned from the task function is returned to the caller. If an error is returned for which errors.Is(err, ErrDoNotReattempt) is true, the caller should not call Attempt again.
Returns the number of times this task has been attempted
Returns true if this task was completed, successfully or not.
Sets the maximum timeout between retries, or zero to exponentially increase the timeout indefinitely. Defaults to 30 minutes.
Returns the time the next attempt is scheduled for, or the zero value if it has not been attempted before.
Specifies that randomness should not be introduced into the exponential backoff algorithm.
Specifies the earliest possible time of the first execution.
Returns the result of the task. The task must have been completed for this to be valid.
Set the maximum number of retries on failure, or -1 to attempt indefinitely. By default, tasks are not retried on failure.
Specifies an upper limit for the duration of each attempt.
Package work imports 11 packages (graph). Updated 19 hours ago.
Tools for package owners.