Jobs
Definition
Jobs:
- are simple classes that can do one thing or multiple related things.
- Job is a name given to a class that is usually created to be queued (it's execution is usually deferred for later, after the execution of previous Jobs are completed).
- Jobs can be scheduled to be executed later by a queuing mechanism (queue system like beanstalkd).
- When a Job class is dispatched, it performs its specific job and dies.
- Laravel's queue worker will process every Job as it's pushed onto the queue.
More info here.
Principles
- A Container MAY have more than one Job.
Rules
- All Jobs MUST extend from
App\Ship\Parents\Jobs\Job
.
Folder Structure
- app
- Containers
- {container-name}
- Jobs
- DoSomethingJob.php
- DoSomethingElseJob.php
Code Samples
CreateAndValidateAddress with third party Job
:
<?php
namespace App\Containers\Shipment\Jobs;
use App\Port\Job\Abstracts\Job;
class CreateAndValidateAddressJob extends Job
{
private $recipients;
public function __construct(array $recipients)
{
$this->recipients = $recipients;
}
public function handle()
{
foreach ($this->recipients as $recipient) {
// do whatever you like
}
}
}
Check the parent Job class.
Usage from Action
:
<?php
// using helper function
dispatch(new CreateAndValidateAddressJob($recipients));
// manually
App::make(\Illuminate\Contracts\Bus\Dispatcher\Dispatcher::class)->dispatch(New StatusChangedJob($object));
Execute Jobs Execution
For running your Jobs checkout the Tasks Queuing page.