PHP code example of bytetcore / queue-unique-runner

1. Go to this page and download the library: Download bytetcore/queue-unique-runner 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/ */

    

bytetcore / queue-unique-runner example snippets


namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Bytetcore\QueueUniqueRunner\Traits\RunsOnUniqueRunner;

class ProcessFinancialAudit implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    use RunsOnUniqueRunner; // <-- Add this trait

    public function handle(): void
    {
        // This code is guaranteed to only run on one server at a time.
        // If a server crashes here, the lock automatically expires.
    }
}

class SyncUserData implements ShouldQueue
{
    use RunsOnUniqueRunner;

    public int $userId;

    public function __construct(int $userId)
    {
        $this->userId = $userId;
    }

    // Lock scope: 'class' (only one SyncUserData job anywhere) 
    // or 'instance' (one SyncUserData job per unique payload)
    public function queueUniqueRunnerScope(): string
    {
        return 'instance';
    }

    // Custom identifier for 'instance' scope
    public function queueUniqueRunnerIdentifier(): ?string
    {
        return 'user:' . $this->userId;
    }

    // How long the lock should be held (in seconds)
    public function queueUniqueRunnerTtl(): int
    {
        return 600; // 10 minutes
    }

    // How long to wait before retrying if another server holds the lock
    public function queueUniqueRunnerRetryDelay(): int
    {
        return 60; // Wait 60 seconds
    }
}
bash
php artisan vendor:publish --tag="queue-unique-runner-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="queue-unique-runner-config"