dlakomski/asynchronous
Laravel package enabling asynchronous/background execution of tasks and queued jobs, letting you dispatch work without blocking the request cycle. Useful for offloading long-running operations, improving responsiveness, and handling work in parallel or after response.
Installation
composer require dlakomski/asynchronous
Ensure simplebus/simple-bus:^2.0 is also installed (dependency).
Service Provider
Register the package in config/app.php under providers:
Dalakomski\Asynchronous\AsynchronousServiceProvider::class,
Publish Config (Optional)
php artisan vendor:publish --provider="Dalakomski\Asynchronous\AsynchronousServiceProvider" --tag="config"
Default config is minimal; customize as needed.
First Use Case: Dispatching a Job
use Dalakomski\Asynchronous\Jobs\Job;
// Define a job (must extend Dalakomski\Asynchronous\Jobs\Job)
class SendEmailJob extends Job {
public $email;
public function __construct(string $email) { $this->email = $email; }
public function handle() { /* ... */ }
}
// Dispatch asynchronously
SendEmailJob::dispatch('user@example.com');
Job Dispatching
Job::dispatch(...) for one-off tasks.Job::dispatchMany([...]).Queue Integration
database, redis, etc.).config/asynchronous.php:
'queue_connection' => 'redis',
Command-Based Jobs
Dalakomski\Asynchronous\Jobs\CommandJob for CLI-like async tasks:
class ProcessDataJob extends CommandJob {
protected $command = 'process:data';
public function handle() { /* ... */ }
}
Event-Driven Async
Event::listen(UserRegistered::class, function ($user) {
NotifyUserJob::dispatch($user->email);
});
Job Chaining
Use Job::dispatchAfter($nextJob) to chain dependent jobs.
ProcessPaymentJob::dispatch($order)
->dispatchAfter(NotifyCustomerJob::class, $order->user->email);
Retry Logic
Implement shouldRetry() and retryAfter() in your job:
public function shouldRetry(): bool { return $this->attempts < 3; }
public function retryAfter(): int { return 60; }
Job Metadata
Attach metadata via setMetadata(['key' => 'value']) for tracking.
Job Serialization
__serialize()/__unserialize().Queue Worker Stuck?
php artisan queue:work
failed_jobs table for errors if jobs fail silently.Missing Dependencies
simplebus/simple-bus must be installed. If missing, install manually:
composer require simplebus/simple-bus
Config Overrides
'dispatcher' => \SimpleBus\Message\Dispatcher\Dispatcher::class,
Log::debug('Job started', ['job' => $this]) in handle().php artisan queue:failed-table
php artisan queue:retry <job_id>
Job::dispatchSync() (if available) for testing without a queue.Custom Dispatcher
Bind a custom SimpleBus\Message\Dispatcher\Dispatcher in the service provider:
$this->app->bind(
\Dalakomski\Asynchronous\Contracts\Dispatcher::class,
CustomDispatcher::class
);
Job Middleware
Add middleware to jobs via setMiddleware([Middleware::class]):
class LogJobMiddleware {
public function handle($job, $next) { /* ... */ }
}
Event Listeners for Jobs
Listen to JobStarted/JobCompleted events (if package emits them):
Event::listen(JobStarted::class, function ($job) { /* ... */ });
How can I help you explore Laravel packages today?