.cursor/skills/laravel-actions/references/job.md
dispatch, asJob)Use this reference when running an action through queues.
makeJob, makeUniqueJob, and withChain.assertPushed*).JobDecorator hooks/properties for retries, uniqueness, timeout, and failure handling.Action::dispatch(...) for async execution.asJob(...).handle(...).AsJob trait)dispatchDispatches the action asynchronously.
SendTeamReportEmail::dispatch($team);
dispatchIfDispatches asynchronously only if condition is met.
SendTeamReportEmail::dispatchIf($team->plan === 'premium', $team);
dispatchUnlessDispatches asynchronously unless condition is met.
SendTeamReportEmail::dispatchUnless($team->plan === 'free', $team);
dispatchSyncDispatches synchronously.
SendTeamReportEmail::dispatchSync($team);
dispatchNowAlias of dispatchSync.
SendTeamReportEmail::dispatchNow($team);
dispatchAfterResponseDispatches synchronously after the HTTP response is sent.
SendTeamReportEmail::dispatchAfterResponse($team);
makeJobCreates a JobDecorator wrapper. Useful with dispatch(...) helper or chains.
dispatch(SendTeamReportEmail::makeJob($team));
makeUniqueJobCreates a UniqueJobDecorator wrapper. Usually automatic with ShouldBeUnique, but can be forced.
dispatch(SendTeamReportEmail::makeUniqueJob($team));
withChainAttaches jobs to run after successful processing.
$chain = [
OptimizeTeamReport::makeJob($team),
SendTeamReportEmail::makeJob($team),
];
CreateNewTeamReport::withChain($chain)->dispatch($team);
Equivalent using Bus::chain(...):
use Illuminate\Support\Facades\Bus;
Bus::chain([
CreateNewTeamReport::makeJob($team),
OptimizeTeamReport::makeJob($team),
SendTeamReportEmail::makeJob($team),
])->dispatch();
Chain assertion example:
use Illuminate\Support\Facades\Bus;
Bus::fake();
Bus::assertChained([
CreateNewTeamReport::makeJob($team),
OptimizeTeamReport::makeJob($team),
SendTeamReportEmail::makeJob($team),
]);
assertPushedAsserts the action was queued.
use Illuminate\Support\Facades\Queue;
Queue::fake();
SendTeamReportEmail::assertPushed();
SendTeamReportEmail::assertPushed(3);
SendTeamReportEmail::assertPushed($callback);
SendTeamReportEmail::assertPushed(3, $callback);
$callback receives:
JobDecorator instance.assertNotPushedAsserts the action was not queued.
use Illuminate\Support\Facades\Queue;
Queue::fake();
SendTeamReportEmail::assertNotPushed();
SendTeamReportEmail::assertNotPushed($callback);
assertPushedOnAsserts the action was queued on a specific queue.
use Illuminate\Support\Facades\Queue;
Queue::fake();
SendTeamReportEmail::assertPushedOn('reports');
SendTeamReportEmail::assertPushedOn('reports', 3);
SendTeamReportEmail::assertPushedOn('reports', $callback);
SendTeamReportEmail::assertPushedOn('reports', 3, $callback);
JobDecorator)asJobCalled when dispatched as a job. Falls back to handle(...) if missing.
class SendTeamReportEmail
{
use AsAction;
public function handle(Team $team, bool $fullReport = false): void
{
// Prepare report and send it to all $team->users.
}
public function asJob(Team $team): void
{
$this->handle($team, true);
}
}
getJobMiddlewareAdds middleware to the queued action.
public function getJobMiddleware(array $parameters): array
{
return [new RateLimited('reports')];
}
configureJobConfigures JobDecorator options.
use Lorisleiva\Actions\Decorators\JobDecorator;
public function configureJob(JobDecorator $job): void
{
$job->onConnection('my_connection')
->onQueue('my_queue')
->through(['my_middleware'])
->chain(['my_chain'])
->delay(60);
}
$jobConnectionDefines queue connection.
public string $jobConnection = 'my_connection';
$jobQueueDefines queue name.
public string $jobQueue = 'my_queue';
$jobTriesDefines max attempts.
public int $jobTries = 10;
$jobMaxExceptionsDefines max unhandled exceptions before failure.
public int $jobMaxExceptions = 3;
$jobBackoffDefines retry delay seconds.
public int $jobBackoff = 60;
getJobBackoffDefines retry delay (int or per-attempt array).
public function getJobBackoff(): int
{
return 60;
}
public function getJobBackoff(): array
{
return [30, 60, 120];
}
$jobTimeoutDefines timeout in seconds.
public int $jobTimeout = 60 * 30;
$jobRetryUntilDefines timestamp retry deadline.
public int $jobRetryUntil = 1610191764;
getJobRetryUntilDefines retry deadline as DateTime.
public function getJobRetryUntil(): DateTime
{
return now()->addMinutes(30);
}
getJobDisplayNameCustomizes queued job display name.
public function getJobDisplayName(): string
{
return 'Send team report email';
}
getJobTagsAdds queue tags.
public function getJobTags(Team $team): array
{
return ['report', 'team:'.$team->id];
}
getJobUniqueIdDefines uniqueness key when using ShouldBeUnique.
public function getJobUniqueId(Team $team): int
{
return $team->id;
}
$jobUniqueIdStatic uniqueness key alternative.
public string $jobUniqueId = 'some_static_key';
getJobUniqueForDefines uniqueness lock duration in seconds.
public function getJobUniqueFor(Team $team): int
{
return $team->role === 'premium' ? 1800 : 3600;
}
$jobUniqueForProperty alternative for uniqueness lock duration.
public int $jobUniqueFor = 3600;
getJobUniqueViaDefines cache driver used for uniqueness lock.
public function getJobUniqueVia()
{
return Cache::driver('redis');
}
$jobDeleteWhenMissingModelsProperty alternative for missing model handling.
public bool $jobDeleteWhenMissingModels = true;
getJobDeleteWhenMissingModelsDefines whether jobs with missing models are deleted.
public function getJobDeleteWhenMissingModels(): bool
{
return true;
}
jobFailedHandles job failure. Receives exception and dispatched parameters.
public function jobFailed(?Throwable $e, ...$parameters): void
{
// Notify users, report errors, trigger compensations...
}
dispatch, dispatchSync, dispatchAfterResponse).$jobConnection, $jobQueue, configureJob).asJob(...) delegates to handle(...) unless queue-specific branching is required.Queue::fake() and action assertions (assertPushed*).asJob(...).