PHP code example of silverslice / quick-rabbit

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

    

silverslice / quick-rabbit example snippets



namespace Silverslice\QuickRabbit\Tests\Jobs;

use Silverslice\QuickRabbit\AbstractJob;

class TestJob extends AbstractJob
{
    public $message;
    
    public function execute()
    {
        echo $this->message . ' ' . date('H:i:s') . "\n";
    }
}


use Silverslice\QuickRabbit\Connection;
use Silverslice\QuickRabbit\Queue;
use Silverslice\QuickRabbit\Tests\Jobs\TestJob;

';

$queue = new Queue($connection);

$job = new TestJob();
$job->message = 'My first job';

// push to the queue
$queue->push($job);

// push to the queue with delay 2 seconds
$queue->pushWithDelay($job, 2000);



use Silverslice\QuickRabbit\Connection;
use Silverslice\QuickRabbit\Worker;

be executed if Job failed after max retries
$worker->setFailedCallback(function ($job, Throwable $exception) {
    echo 'FailedCallback: ' . $exception->getMessage() . PHP_EOL;
});
$worker->run();



class TestJob extends AbstractJob
{
    public $message;

    public function execute()
    {
        
    }

    /**
     * Is job retryable?
     *
     * @param int $retries Number of retry
     * @return bool
     */
    public function isRetryable($retries): bool
    {
        return $retries <= 5;
    }

    /**
     * Returns retry delay in milliseconds
     *
     * @param $retries
     * @return int
     */
    public function getRetryDelay($retries): int
    {
        return 1000 * 2 ** ($retries - 1);
    }
}



$queue = new SyncQueue($connection);

$job = new TestJob();
$job->message = 'My first job';

// will be executed synchronously
$queue->push($job);