PHP code example of fahriar / laravel-shared-queue

1. Go to this page and download the library: Download fahriar/laravel-shared-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/ */

    

fahriar / laravel-shared-queue example snippets


'tasks' => [
    'send_invoice_email' => \App\Tasks\SendInvoiceEmailTask::class,
    'process_payment'    => \App\Tasks\ProcessPaymentTask::class,
],

namespace App\Tasks;

use LaravelSharedQueue\Contracts\TaskHandler;
use Illuminate\Support\Facades\Log;

class SendInvoiceEmailTask implements TaskHandler
{
    public function handle(array $payload): void
    {
        // Business logic here.
        Log::info("Sending email for invoice: " . $payload['invoice_id']);
    }
}

use LaravelSharedQueue\Facades\SharedQueue;

SharedQueue::dispatch('send_invoice_email', [
    'invoice_id' => 1001,
]);

// Or securely dispatch using the global helper:
shared_queue('send_invoice_email', ['invoice_id' => 1001]);

SharedQueue::dispatch('send_invoice_email', ['invoice_id' => 1001], [
    'delay' => now()->addMinutes(10) // Also accepts integer seconds
]);

SharedQueue::dispatch('process_payment', ['order_id' => 5], [
    'idempotency_key' => 'payment_order_5'
]);

SharedQueue::dispatchAfterCommit('send_invoice_email', ['invoice_id' => 1001]);

use Illuminate\Support\Facades\Schedule;

Schedule::command('shared-queue:work')->everyMinute()->withoutOverlapping();
bash
php artisan vendor:publish --tag=shared-queue-config
php artisan vendor:publish --tag=shared-queue-migrations
bash
php artisan migrate