PHP code example of wangjian / queue

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

    

wangjian / queue example snippets



is = new Predis\Client([
    'schema' => $this->getConfig('QUEUE_REDIS_SCHEMA', 'tcp'),
    'host' => $this->getConfig('QUEUE_REDIS_HOST', '127.0.0.1'),
    'port' => $this->getConfig('QUEUE_REDIS_PORT', 6379),
    'database' => $this->getConfig('QUEUE_REDIS_DATABASE', 0)
]);

$queue = new \Wangjian\Queue\RedisQueue($redis, 'queue');

$queue->push((new \Wangjian\Queue\Job\TestJob()));


 = new PDO('mysql:host=mysql;dbname=test', 'root', 'root');
$queue = new \Wangjian\Queue\MysqlQueue($pdo, 'queue');

$queue->push((new \Wangjian\Queue\Job\TestJob()));

# 只要将任务的执行时间设置成未来的某个时刻,该任务就会成为定时任务
$queue->push((new \Wangjian\Queue\Job\TestJob())->setRunAt(time() + 10));  //十秒后执行该任务
bash
php bin/worker migrate  -c queue.ini 
bash
php bin/worker start default -c queue.ini
bash
export QUEUE_SLEEP_INTERVAL=3
bash
# 关闭名称为default的消息队列消费者进程
php bin/worker stop default
bash
php bin/worker reload default