Skip to content

Latest commit

 

History

History
90 lines (74 loc) · 4.02 KB

Ref_JobContext_class.md

File metadata and controls

90 lines (74 loc) · 4.02 KB

JobContext DTO Class

Declaration: mle86\Job\JobContext
Source file: src/WQ/Job/JobContext.php

Contains metadata about the job currently being processed and various setters to attach one-off event handlers to the job.

Instances of this class are available in job callbacks run by WorkProcessor::processNextJob() (their expected signature is function(Job, JobContext): ?int|void).

(Only the WorkProcessor class should create instances of this class.)

Context Getters

  • public function getQueueEntry(): QueueEntry
    The queue entry DTO containing the current job instance.
  • public function getJob(): Job
    The job currently being executed.
  • public function getSourceQueue(): string
    The name of the work queue from which the current job was received.
  • public function getWorkProcessor(): WorkProcessor
    The WorkProcessor that created this instance.
  • public function getWorkServer(): WorkServerAdapter
    The work server from which the current job was received.

Callback Setters

Additionally, it's possible to attach one or more callbacks to the current job context. One of them will be run when the job callback returns (or throws an exception).

This may be useful for job handlers that want to perform cleanup only in case of a permanently-failed job (onFailure) but not on a temporarily-failed job (onTemporaryFailure)

Expected signature for the onSuccess callbacks: function(Job, JobContext): void.
Expected signature for the onFailure and onTemporaryFailure callbacks: function(Job, JobContext, ?Throwable): void.

  • public function onTemporaryFailure(?callable $callback): void
    Sets up a callback that will be called once if and when the current job is being re-queued because it failed and should be re-tried.
    This happens if WP_ENABLE_RETRY is set, if jobCanRetry() is true, and if the job handler returned JobResult::FAILED or threw a RuntimeException. (Any such exception will be passed to the callback in its third argument.)
    (This callback will be run by the WorkProcessor after it calls its internal onJobRequeue() hook, immediately before calling WorkServerAdapter::requeueEntry().)

  • public function onFailure(?callable $callback): void
    Sets up a callback that will be called once if and when the current job is being buried/deleted because it failed and should not (or cannot) be re-tried later.
    This happens if WP_ENABLE_RETRY is not set or if jobCanRetry() returns false and if the job handler returned JobResult::ABORT or threw a non-RuntimeException throwable. (Any such exception will be passed to the callback in its third argument.)
    (This callback will be run by the WorkProcessor after it calls its internal onFailedJob() hook, immediately before calling WorkServerAdapter::buryEntry()/deleteEntry().)

  • public function onSuccess(?callable $callback): void
    Sets up a callback that will be called once if and when the current job is being deleted/movied because it succeeded!
    This happens if the job handler returns JobResult::SUCCESS/null/void.
    (This callback will be run by the WorkProcessor after it calls its internal onSuccessfulJob() hook, immediately before calling WorkServerAdapter::deleteEntry()/requeueEntry().)