PHP code example of webfiori / queue

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

    

webfiori / queue example snippets


use WebFiori\Queue\Job;

class SendEmailJob implements Job {
    public function __construct(
        private string $to,
        private string $subject
    ) {}

    public function handle(): void {
        // Send the email
        mail($this->to, $this->subject, 'Hello!');
    }

    public function getMaxAttempts(): int {
        return 3;
    }

    public function getRetryDelaySeconds(): int {
        return 60; // wait 60s × attempt number before retry
    }
}

use WebFiori\Queue\FileQueueStorage;
use WebFiori\Queue\Queue;

$queue = new Queue(new FileQueueStorage('/path/to/storage'));

// Dispatch jobs
$queue->dispatch(new SendEmailJob('[email protected]', 'Welcome!'));
$queue->dispatch(new SendEmailJob('[email protected]', 'Priority!'), priority: 10);
$queue->dispatch(new SendEmailJob('[email protected]', 'Delayed'), delaySeconds: 300);

// Process pending jobs (call this from a scheduler or worker)
$processed = $queue->process(limit: 50);

use WebFiori\Queue\QueueFacade;

QueueFacade::dispatch(new SendEmailJob('[email protected]', 'Hello'));
QueueFacade::process();

// View failed jobs
$failed = $queue->getFailed();

// Retry a specific failed job
$queue->retry($failed[0]['id']);

// Clear all failed jobs
$queue->flush();