Home  >  Article  >  Backend Development  >  PHP-Resque usage

PHP-Resque usage

Guanhui
GuanhuiOriginal
2020-06-06 11:03:323694browse

PHP-Resque usage

PHP-Resque Usage

PHP-Resque is an extension library for PHP for Resque, which enables PHP to use Resque. Resque is A background process system developed based on Redis. Compared with other Queue systems, Resque's design is very simple and fully utilizes the characteristics of Redis.

1. Install php-resque

Enter the project root directory, composer install php-resque

composer require chrisboulton/php-resque

2. Common methods

1. Connect to redis

// setBackend($server, $database = 0)
Resque::setBackend('127.0.0.1:6379');

2. Add work to the queue

// enqueue($queue, $class, $args = null, $trackStatus = false)
$token = Resque::enqueue('default', 'My_Job', ['name'=>'test'], true);

3. Check the work status

$status = (new Resque_Job_Status($token))->get();

4. Stop (remove) the job

(new Resque_Job_Status($token))->stop();

3. Resident task processing queue (example: worker.php)

// 处理 default 队列;也可以填 *,代表所有队列
$worker = new Resque_Worker('default');
// LOG_NONE 不写日志, LOG_NORMAL 普通,LOG_VERBOSE 详细
$worker->logLevel = Resque_Worker::LOG_VERBOSE;
// 队列处理时间间隔,单位:秒
$worker->work(5);

Note: worker.php must be executed through the command line and resides in the background, /usr/local/php/bin/php /xxx/xxx/worker.php

4. Classes for processing work

class My_Job
{
    /**
     * 前置操作
     * @return void
     */
    public function setUp()
    {
        // ... Set up environment for this job
    }
    /**
     * 消费队列
     * @return void
     */
    public function perform()
    {
        // execute a job
    }
    /**
     * 后置操作
     * @return void
     */
    public function tearDown()
    {
        // ... Remove environment for this job
    }
}

Recommended tutorial: "PHP Tutorial"

The above is the detailed content of PHP-Resque usage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn