PHP code example of caylof / php-queue

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

    

caylof / php-queue example snippets


# consumers/TestPrint.php

namespace Caylof\Examples\Consumer;

use Caylof\Queue\ConsumerInterface;

class TestPrint implements ConsumerInterface
{
    public function getQueue(): string
    {
        return 'test1';
    }

    public function handle(array $data): void
    {
        if (mt_rand(0, 10) > 5) {
            throw new \Exception('test error');
        }
        var_dump($data);
    }
}

# process.php

_ . '/consumers/TestPrint.php';

use Workerman\Worker;

Worker::$pidFile = __DIR__ . '/test_queue-pid';
Worker::$logFile = '/dev/null';
Worker::$statusFile = '/dev/null';
$worker = new Worker();
$worker->count = 1;

$worker->onWorkerStart = function(Worker $worker) {
    $redis = new Redis();
    $redis->connect('192.168.110.116', 16379);

    $logger = new \Monolog\Logger('queue');
    $logger->pushHandler(new \Monolog\Handler\StreamHandler(__DIR__ . '/queue.log'));

    $handler = new \Caylof\Queue\RedisStreamProcess(
        \Illuminate\Container\Container::getInstance(),
        $redis,
        $logger,
        __DIR__ . '/consumers',
        'Caylof\\Examples\\Consumer',
    );
    $handler->onWorkerStart();
};

Worker::runAll();

# client.php

 Redis();
$redis->connect('192.168.110.116', 16379);

$client = new \Caylof\Queue\RedisStreamClient($redis);
$client->send('test1', ['id'=> 1, 'name' => 'cctv']);

example
 - process.php
 - client.php
 - consumers
   - TestPrint.php
shell
php process.php start
shell
php client.php