Jobs¶
What is a Job¶
Jobs as the work units executed by recruiter.Workable object that we have seen previously (and therefore the procedure to execute) in addition to all other information necessary for the correct execution of this procedure, such as:the scheduling date,
the retry policy in case of failure,
the current state (to be executed, executing, executed),
the group it belongs to
others (i.e. creation date, number of attempts made, tags, etc.)
be executed in process instantaneously,
be scheduled for background execution as soon as possible,
be scheduled for background execution at a specific date/time
be executed in process instantaneously, and in case of failure be scheduled for background execution according to its retry policies.
be retried in case of failure one or more times, according to specific retry policies.
To queue jobs for execution we need to have an instance of the Recruiter\Recruiter class.
Warning
executed; in that case the job will, after a certain period of time, be assigned to another worker and therefore executed a second time.Hello World¶
<?php
use MyDomain\HttpRequestCommand;
use MyDomain\Request;
use Recruiter\Recruiter;
$mongodbInstance = new MongoDB\Client(...);
$recruiter = new Recruiter($mongodbInstance);
$request = Request::post($url, $body);
HttpRequestCommand::fromRequest($request)
->asJobOf($recruiter)
->inBackground()
->execute() // this is the method defined in the Workable class
;
job will be scheduled that will call the execute() method of the HttpRequestCommand class and will be executed as soon as a worker is available.Schedule a Job in the future¶
scheduleAt() method to which we need to pass an instance of Timeless\MomentJanuary 19, 2038 we could do it this way:<?php
use MyDomain\HttpRequestCommand;
use MyDomain\Request;
use Recruiter\Recruiter;
use Timeless\Moment;
$mongodbInstance = new MongoDB\Client(...);
$recruiter = new Recruiter($mongodbInstance);
$request = Request::post($url, $body);
HttpRequestCommand::fromRequest($request)
->asJobOf($recruiter)
->scheduleAt(Moment::fromDateTime(new DateTime('2038-01-19T00:00:00.000000Z');))
->inBackground()
->execute()
;
This way the job will be queued and executed as soon as there is a free worker available after the date ‘2038-01-19T00:00:00.000000Z’
Retry¶
Recruiter\RetryPolicy to the job through the retryWithPolicy(RetryPolicy $retryPolicy) method.<?php
use MyDomain\HttpRequestCommand;
use MyDomain\Request;
use Recruiter\Recruiter;
use Recruiter\RetryPolicy\RetryManyTimes;
use Timeless\Moment;
$mongodbInstance = new MongoDB\Client(...);
$recruiter = new Recruiter($mongodbInstance);
$retryPolicy = new RetryManyTimes(3, 60);
$request = Request::post($url, $body);
HttpRequestCommand::fromRequest($request)
->asJobOf($recruiter)
->scheduleAt(Moment::fromDateTime(new DateTime('2038-01-19T00:00:00.000Z')))
->retryWithPolicy($retryPolicy)
->inBackground()
->execute()
;
Retriable Exceptions¶
retryWithPolicy method in fact allows you to specify, as a second argument, an array of exceptions for which it is allowed to execute a new attempt.<?php
$retryPolicy = new RetryManyTimes(3, 60);
$retriableExceptionTypes = [
\Psr\Http\Client\NetworkExceptionInterface::class
];
HttpCommand::fromRequest($request);
->asJobOf($recruiter)
->retryWithPolicy($retryPolicy, $retriableExceptionTypes)
->inBackground()
->execute()
;
In this case the job will be repeated only if an exception of type Psr\Http\Client\NetworkExceptionInterface occurs, in all other cases the job will be archived.
Optimistic Jobs¶
<?php
$retryPolicy = new RetryManyTimes(3, 60);
$retriableExceptionTypes = [
\Psr\Http\Client\NetworkExceptionInterface::class
];
HttpCommand::fromRequest($request);
->asJobOf($recruiter)
->retryWithPolicy($retryPolicy, $retriableExceptionTypes)
->execute()
;
inBackground() method, this way the command will be executed immediately, and, only in case of failure, will be inserted into the queue of jobs to be executed in background.Note
<?php
HttpCommand::fromRequest($request);
->asJobOf($recruiter)
->retryWithPolicy($retryPolicy, $retriableExceptionTypes)
->inBackground()
->execute()
;
HttpCommand::fromRequest($request);
->asJobOf($recruiter)
->retryWithPolicy($retryPolicy, $retriableExceptionTypes)
->scheduleAt(Moment::fromDateTime(new DateTime('2151-02-21T15:03:01.012345Z');))
->execute()
;
Grouping Jobs¶
inGroup($group) method<?php
HttpCommand::fromRequest($request);
->asJobOf($recruiter)
->inGroup('http')
->inBackground()
->execute()
;