PHP code example of ebects / laravel-roadrunner-queue
1. Go to this page and download the library: Download ebects/laravel-roadrunner-queue library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ebects / laravel-roadrunner-queue example snippets
namespace App\Jobs;
use Elects\RoadRunnerQueue\Jobs\RoadRunnerJob;
class ProcessInvoice extends RoadRunnerJob
{
// ✅ NOW WORKS in RoadRunner!
public $tries = 3;
public $backoff = [10, 30, 60];
public $timeout = 120;
public $invoiceId;
public function __construct($invoiceId)
{
$this->invoiceId = $invoiceId;
}
// ✅ Implement process() instead of handle()
protected function process(): void
{
$invoice = Invoice::find($this->invoiceId);
$invoice->process();
}
// ✅ Auto-called after max retries!
public function failed(\Throwable $exception): void
{
$invoice = Invoice::find($this->invoiceId);
$invoice->markAsFailed();
}
}
ProcessInvoice::dispatch($invoiceId);
class MyJob extends RoadRunnerJob
{
public $tries = 5;
public $backoff = [10, 30, 60, 120, 300]; // seconds
protected function process(): void
{
// Your code - automatically retries on exception!
}
}
protected function process(): void
{
// Your business logic
$this->sendEmail();
}
public function failed(\Throwable $exception): void
{
// ✅ Automatically called after max retries!
Log::error('Job failed completely', [
'exception' => $exception->getMessage()
]);
// Cleanup, notifications, rollback, etc.
$this->cleanup();
}
protected function process(): void
{
// Get current attempt number
$attempt = $this->currentAttempt(); // 1, 2, 3, ...
// Check if final attempt
if ($this->isFinalAttempt()) {
$this->notifyAdmin('Last attempt!');
}
// Your logic
}
class MyJob extends RoadRunnerJob
{
protected function getRetryDelay(int $currentAttempt): int
{
// Exponential backoff: 2^attempt * 10
return pow(2, $currentAttempt) * 10;
}
}
protected function process(): void
{
try {
$this->sendEmail();
} catch (RateLimitException $e) {
// Retry for rate limits
throw $e;
} catch (InvalidEmailException $e) {
// Don't retry for invalid data
$this->failed($e);
return;
}
}
protected function getJobIdentifier(): string
{
return "CustomJob:{$this->userId}:{$this->type}";
}
class MyJob implements ShouldQueue
{
public $tries = 3; // ❌ Ignored
public $backoff = [10, 30, 60]; // ❌ Ignored
public function handle(): void
{
// ❌ Fails once = job gone forever
$this->doWork();
}
public function failed(\Throwable $e): void
{
// ❌ Never called
}
}
class MyJob extends RoadRunnerJob
{
public $tries = 3; // ✅ Works!
public $backoff = [10, 30, 60]; // ✅ Works!
protected function process(): void
{
// ✅ Auto retry with backoff
$this->doWork();
}
public function failed(\Throwable $e): void
{
// ✅ Auto called after 3 attempts!
}
}
class CallExternalAPIJob extends RoadRunnerJob
{
public $tries = 5;
public $backoff = [30, 60, 120, 300, 600];
public $timeout = 30;
protected function process(): void
{
$response = Http::timeout($this->timeout)
->post('https://api.example.com/endpoint', $this->data);
if ($response->failed()) {
throw new \Exception('API call failed');
}
}
public function failed(\Throwable $e): void
{
Notification::send('API integration failed after 5 attempts');
}
}
class UpdateInventoryJob extends RoadRunnerJob
{
public $tries = 3;
public $backoff = [5, 15, 30];
protected function process(): void
{
DB::transaction(function () {
$product = Product::lockForUpdate()->find($this->productId);
$product->decrement('stock', $this->quantity);
});
}
public function failed(\Throwable $e): void
{
// Rollback order if inventory update fails
Order::find($this->orderId)->cancel();
}
}
class SendEmailJob extends RoadRunnerJob
{
public $tries = 4;
public $backoff = [10, 60, 300, 3600]; // 10s, 1m, 5m, 1h
protected function process(): void
{
Mail::to($this->user)->send(new WelcomeEmail());
}
}