Documentation ¶
Overview ¶
Package queued_jobs contains logic for interacting with the "queued_jobs" table.
Index ¶
- Constants
- Variables
- func Acquire(ctx context.Context, name string, workerID int) (*newmodels.QueuedJob, error)
- func CountReadyAndAll(ctx context.Context) (all int, ready int, err error)
- func Decrement(ctx context.Context, id types.PrefixUUID, attempts int16, runAfter time.Time) (*newmodels.QueuedJob, error)
- func Delete(ctx context.Context, id types.PrefixUUID) error
- func DeleteRetry(ctx context.Context, id types.PrefixUUID, attempts int) error
- func Enqueue(params newmodels.EnqueueJobParams) (*newmodels.QueuedJob, error)deprecated
- func EnqueueFast(params newmodels.EnqueueJobFastParams) error
- func Get(ctx context.Context, id types.PrefixUUID) (*newmodels.QueuedJob, error)
- func GetAttempts(ctx context.Context, id types.PrefixUUID) (int16, error)
- func GetCountsByStatus(ctx context.Context, status newmodels.JobStatus) (map[string]int64, error)
- func GetOldInProgressJobs(ctx context.Context, olderThan time.Time) ([]newmodels.QueuedJob, error)
- func GetRetry(ctx context.Context, id types.PrefixUUID, attempts int) (job *newmodels.QueuedJob, err error)
- type UnknownOrArchivedError
Examples ¶
Constants ¶
const Prefix = "job_"
Variables ¶
var ErrNotFound = errors.New("queued_jobs: 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 ¶
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 ¶
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 ¶
DeleteRetry attempts to Delete the item `attempts` times.
func Enqueue
deprecated
func Enqueue(params newmodels.EnqueueJobParams) (*newmodels.QueuedJob, error)
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 ¶
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