queued_jobs

package
v0.0.0-...-7ed8402 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2023 License: MIT Imports: 8 Imported by: 13

Documentation

Overview

Package queued_jobs contains logic for interacting with the "queued_jobs" table.

Index

Examples

Constants

View Source
const Prefix = "job_"

Variables

View Source
var ErrNotFound = errors.New("queued_jobs: job not found")

ErrNotFound indicates that the job was not found.

View Source
var StuckJobLimit = 100

StuckJobLimit is the maximum number of stuck jobs to fetch in one database query.

Functions

func Acquire

func Acquire(ctx context.Context, name string, workerID int) (*newmodels.QueuedJob, error)

Acquire a queued job with the given name that's able to run now. Returns the queued job and a boolean indicating whether the SELECT query found a row, or a generic error/sql.ErrNoRows if no jobs are available.

func CountReadyAndAll

func CountReadyAndAll(ctx context.Context) (all int, ready int, err error)

CountReadyAndAll returns the total number of queued and ready jobs in the table.

func Decrement

func Decrement(ctx context.Context, id types.PrefixUUID, attempts int16, runAfter time.Time) (*newmodels.QueuedJob, error)

Decrement decrements the attempts counter for an existing job, and sets its status back to 'queued'. If the queued job does not exist, or the attempts counter in the database does not match the passed in attempts value, sql.ErrNoRows will be returned.

attempts: The current value of the `attempts` column, the returned attempts value will be this number minus 1.

func Delete

func Delete(ctx context.Context, id types.PrefixUUID) error

Delete deletes the given queued job. Returns nil if the job was deleted successfully. If no job exists to be deleted, ErrNotFound is returned.

Example
id, _ := types.NewPrefixUUID("job_6740b44e-13b9-475d-af06-979627e0e0d6")
err := Delete(context.TODO(), id)
// Returns an error, because we didn't insert rows into the database.
fmt.Println(err)
Output:

func DeleteRetry

func DeleteRetry(ctx context.Context, id types.PrefixUUID, attempts int) error

DeleteRetry attempts to Delete the item `attempts` times.

func Enqueue deprecated

Enqueue creates a new queued job with the given ID and fields. A sql.ErrNoRows will be returned if the `name` does not exist in the jobs table. Otherwise the QueuedJob will be returned.

Deprecated: use services.Enqueue instead.

func EnqueueFast

func EnqueueFast(params newmodels.EnqueueJobFastParams) error

func Get

func Get(ctx context.Context, id types.PrefixUUID) (*newmodels.QueuedJob, error)

Get the queued job with the given id. Returns the job, or an error. If no record could be found, the error will be `queued_jobs.ErrNotFound`.

func GetAttempts

func GetAttempts(ctx context.Context, id types.PrefixUUID) (int16, error)

func GetCountsByStatus

func GetCountsByStatus(ctx context.Context, status newmodels.JobStatus) (map[string]int64, error)

GetCountsByStatus returns a map with each job type as the key, followed by the number of <status> jobs it has. For example:

"echo": 5, "remind-assigned-driver": 7,

func GetOldInProgressJobs

func GetOldInProgressJobs(ctx context.Context, olderThan time.Time) ([]newmodels.QueuedJob, error)

GetOldInProgressJobs finds queued in-progress jobs with an updated_at timestamp older than olderThan. A maximum of StuckJobLimit jobs will be returned.

func GetRetry

func GetRetry(ctx context.Context, id types.PrefixUUID, attempts int) (job *newmodels.QueuedJob, err error)

GetRetry attempts to retrieve the job attempts times before giving up.

Types

type UnknownOrArchivedError

type UnknownOrArchivedError struct {
	Err string
}

UnknownOrArchivedError is raised when the job type is unknown or the job has already been archived. It's unfortunate we can't distinguish these, but more important to minimize the total number of queries to the database.

func (*UnknownOrArchivedError) Error

func (e *UnknownOrArchivedError) Error() string

Jump to

Keyboard shortcuts

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