PHP-Resque usage

Guanhui
Release: 2023-03-01 12:52:01
Original
3503 people have browsed it

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
Copy after login

2. Common methods

1. Connect to redis

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

2. Add work to the queue

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

3. Check the work status

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

4. Stop (remove) the job

(new Resque_Job_Status($token))->stop();
Copy after login

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);
Copy after login

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
    }
}
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!