Documentation ¶
Overview ¶
Logic for interacting with the "queued_jobs" table.
Index ¶
- Constants
- Variables
- func Acquire(name string) (*models.QueuedJob, error)
- func CountReadyAndAll() (allCount int, readyCount int, err error)
- func Decrement(id types.PrefixUUID, attempts uint8, runAfter time.Time) (*models.QueuedJob, error)
- func Delete(id types.PrefixUUID) error
- func DeleteRetry(id types.PrefixUUID, attempts uint8) error
- func Enqueue(id types.PrefixUUID, name string, runAfter time.Time, expiresAt types.NullTime, ...) (*models.QueuedJob, error)
- func Get(id types.PrefixUUID) (*models.QueuedJob, error)
- func GetCountsByStatus(status models.JobStatus) (map[string]int64, error)
- func GetOldInProgressJobs(olderThan time.Time) ([]*models.QueuedJob, error)
- func GetRetry(id types.PrefixUUID, attempts uint8) (job *models.QueuedJob, err error)
- func Setup() (err error)
- type UnknownOrArchivedError
Examples ¶
Constants ¶
const Prefix = "job_"
Variables ¶
var ErrNotFound = errors.New("Queued job not found")
ErrNotFound indicates that the job was not found.
var StuckJobLimit = 100
StuckJobLimit is the maximum number of stuck jobs to fetch in one database query.
Functions ¶
func Acquire ¶
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 ¶
CountReadyAndAll returns the total number of queued and ready jobs in the table.
func Decrement ¶
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 ¶
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 ¶
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 ¶
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 ¶
GetOldInProgressJobs finds queued in-progress jobs with an updated_at timestamp older than olderThan. A maximum of StuckJobLimit jobs will be returned.
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