spatie/laravel-queueable-action
Add simple queue support to Laravel “actions.” Call actions synchronously or dispatch them to the queue with $action->onQueue()->execute(), optionally choosing a queue name. Includes a configurable job class for customization.
Installation
composer require spatie/laravel-queueable-action
Publish the config (optional):
php artisan vendor:publish --provider="Spatie\QueueableAction\QueueableActionServiceProvider"
First Use Case
Create an action class extending Spatie\QueueableAction\Action:
use Spatie\QueueableAction\Action;
class ProcessOrderAction extends Action
{
public function run()
{
// Your business logic here
Order::find($this->orderId)->process();
}
}
Queue the action:
$action = new ProcessOrderAction(orderId: $orderId);
$action->onQueue('orders')->execute();
Where to Look First
config/queueable-action.php for default queue settings.app/Console/Kernel.php to verify queue workers are configured.Basic Queueing
$action = new MyAction(...);
$action->onQueue()->execute(); // Uses default queue
Custom Queue Names
$action->onQueue('high-priority')->execute();
Delayed Execution
$action->delay(now()->addMinutes(10))->execute();
Chaining Actions
$action1 = new Action1(...);
$action2 = new Action2(...);
$action1->onQueue()->execute();
$action2->onQueue('secondary')->execute();
Dependency Injection: Pass required dependencies via constructor.
class MyAction extends Action {
public function __construct(private OrderRepository $orders) {}
}
Event Dispatching: Dispatch events after execution:
public function run() {
$this->orders->process($this->orderId);
event(new OrderProcessed($this->orderId));
}
Job Chaining: Use Laravel’s afterCommit() for database-heavy actions:
$action->onQueue()->afterCommit()->execute();
Middleware: Apply middleware to actions:
$action->onQueue()->withMiddleware(CheckUserPermission::class)->execute();
Queue Worker Must Be Running
php artisan queue:work is running (or supervised via Supervisor/Foreman).jobs table for stuck jobs or laravel.log for worker errors.Serialization Issues
Default Queue Overrides
QUEUE_CONNECTION in .env is sync, actions won’t queue..env:
QUEUE_CONNECTION=database
Transaction Conflicts
afterCommit() for critical DB operations.Missing run() Method
run() will throw BadMethodCallException.run() in your action class.Check Job Payloads:
php artisan queue:table
Inspect the payload column for malformed data.
Log Queue Events:
Add to AppServiceProvider:
public function boot()
{
Queue::before(function ($job, $data) {
Log::debug('Queueing job', ['job' => get_class($job), 'data' => $data]);
});
}
Queue Namespacing Use namespaced queues for better organization:
$action->onQueue('notifications.email')->execute();
Dynamic Queues Set queue names dynamically:
$queue = $this->user->isPremium() ? 'premium-jobs' : 'standard-jobs';
$action->onQueue($queue)->execute();
Action Metadata Store metadata in the action’s constructor:
class MyAction extends Action {
public function __construct(
public string $userId,
public string $metadata = 'default'
) {}
}
Testing
Use Queue::fake() in tests:
public function test_action_is_queued()
{
Queue::fake();
$action->onQueue()->execute();
Queue::assertPushed(MyAction::class);
}
Performance
onQueue()->withoutOverlapping() for idempotent actions:
$action->onQueue()->withoutOverlapping(10)->execute();
Custom Job Class
Extend Spatie\QueueableAction\Jobs\QueueableActionJob for advanced use cases:
class CustomJob extends QueueableActionJob {
public function retryUntil()
{
return now()->addMinutes(5); // Custom retry logic
}
}
Then bind it in QueueableActionServiceProvider.
How can I help you explore Laravel packages today?