queued_jobs

package
v0.0.0-...-453881e Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2018 License: MIT Imports: 9 Imported by: 6

Documentation

Overview

Logic for interacting with the "queued_jobs" table.

Index

Examples

Constants

View Source
const Prefix = "job_"

Variables

View Source
var ErrNotFound = errors.New("Queued 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(name string) (*models.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() (allCount int, readyCount int, err error)

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

func Decrement

func Decrement(id types.PrefixUUID, attempts uint8, runAfter time.Time) (*models.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(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, sql.ErrNoRows is returned.

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

func DeleteRetry

func DeleteRetry(id types.PrefixUUID, attempts uint8) error

DeleteRetry attempts to Delete the item `attempts` times.

func Enqueue

func Enqueue(id types.PrefixUUID, name string, runAfter time.Time, expiresAt types.NullTime, data json.RawMessage) (*models.QueuedJob, error)

Enqueue creates a new queued job with the given ID and fields. A dberror.Error will be returned if Postgres returns a constraint failure - job exists, job name unknown, &c. A sql.ErrNoRows will be returned if the `name` does not exist in the jobs table. Otherwise the QueuedJob will be returned.

func Get

func Get(id types.PrefixUUID) (*models.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 GetCountsByStatus

func GetCountsByStatus(status models.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(olderThan time.Time) ([]*models.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(id types.PrefixUUID, attempts uint8) (job *models.QueuedJob, err error)

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

func Setup

func Setup() (err error)

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