Home >Backend Development >PHP Tutorial >php-resque usage instructions

php-resque usage instructions

小云云
小云云Original
2018-02-24 15:30:222616browse

There is often a need to run tasks in the background in projects. For example, when sending emails, it often takes 5-10 seconds or even longer to connect to the mail server. If you can first give the user a successful prompt message, and then in the background Slowly handling the operation of sending emails will obviously lead to a better user experience.

In order to achieve similar needs, the general implementation method in Web projects is to use message queue (Message Queue), such as MemcacheQ, RabbitMQ, etc., which are all well-known products.

To put it bluntly, the message queue is the simplest first-in-first-out queue, and a member of the queue is a piece of text. Precisely because the message queue is so simple, when you hold the message queue, you feel a little confused, because this is just a task of sending emails, which will lead to many problems:

  1. The message queue can only store string type data. How to convert a "task" such as sending an email into a "message" in the message queue?

  2. The message queue only Responsible for the storage and entry of data, it cannot execute any program itself, so how do we take out the data one by one from the message queue, and then convert the data back into tasks and execute them.

  3. We cannot predict when the message queue will generate data, so our task execution program also needs to have the ability to monitor the message queue, which is a daemon process resident in the background.

  4. General web application PHP runs in cgi mode and cannot reside in memory. We know that PHP also has a CLI mode, so can the daemon process be implemented with PHP CLI and how efficient is it?

  5. When the daemon process is running, can the web application interact with the background daemon process to realize the function of starting/killing the process and obtaining the running status of the process?


Resque solves these problems like this:

Role division of background tasks

In fact, from the above The problems can already be seen. Only one message queue cannot solve all problems, and new roles are required to intervene. In Resque, a background task is abstracted to be completed by three roles:

  • Job | Task: A Job is a task that needs to be completed in the background, such as sending emails as an example in this article , it can be abstracted into a Job. In Resque, a Job is a Class.

  • Queue | Queue: This is the message queue above. In Resque, the queue is implemented by Redis. Resque also provides a simple queue manager that can implement functions such as inserting/removing jobs from the queue.

  • Worker | Executor: Responsible for taking out the job from the queue and executing it, and can run in the background as a daemon process.

Based on this division, the basic process of a background task under Resque is as follows:

  1. Write a background task as an independent Class, this Class is a Job.

  2. Where the background program needs to be used, the system puts the name of the Job Class and the required parameters into the queue.

  3. Open a Worker through the command line and specify the queue that the Worker needs to process through parameters.

  4. Worker runs as a daemon process and checks the queue regularly.

  5. When there is a Job in the queue, the Worker takes out the Job and runs it, that is, instantiates the Job Class and executes the methods in the Class.

Now you can complete a background task.

In Resque, there is another very important design: a Worker can handle one queue or many queues, and the execution speed of the queue can be accelerated by increasing the number of Worker processes/threads.

Installation of php-resque

It should be noted in advance that because it involves process development and management, php-resque uses the PCNTL function of php, so it can only run under Linux. And php is required to compile the PCNTL function. If you want to use Windows to do the same work, you can find other language versions of Resque. PHP is very unsuitable for background tasks under Windows.

Take Ubuntu12.04LTS as an example. The php installed by Ubuntu using apt has compiled the PCNTL function by default. No configuration is required. The following instructions are for the root account computer training class

Installing Redis

apt-get install redis-server

Install Composer

apt-get install curl
cd /usr/local/bin
curl -s http://getcomposer.org/installer | php
chmod a+x composer.phar
alias composer='/usr/local/bin/composer.phar'

Use Composer to install php-resque

Assume that the web directory is in /opt/htdocs

apt-get install git git-core
cd /opt/htdocs
git clone git://github.com/chrisboulton/php-resque.git
cd php-resque
composer install

Use of php-resque

Write a Worker

In fact, php-resque has given a simple example. The demo/job.php file is the simplest Job:

class PHP_Job
{
    public function perform()
    {
        sleep(120);
        fwrite(STDOUT, 'Hello!');
    }
}

This Job is to send the message to the server after 120 seconds. STDOUT output characters Hello!

In the design of Resque, a Job must have a perform method, and the Worker will automatically run this method.

将Job插入队列

php-resque也给出了最简单的插入队列实现 demo/queue.php:

if(empty($argv[1])) {
    die('Specify the name of a job to add. e.g, php queue.php PHP_Job');
}

require __DIR__ . '/init.php';
date_default_timezone_set('GMT');
Resque::setBackend('127.0.0.1:6379');

$args = array(
    'time' => time(),
    'array' => array(
        'test' => 'test',
    ),
);

$jobId = Resque::enqueue('default', $argv[1], $args, true);
echo "Queued job ".$jobId."\n\n";

在这个例子中,queue.php需要以cli方式运行,将cli接收到的第一个参数作为Job名称,插入名为'default'的队列,同时向屏幕输出刚才插入队列的Job Id。在终端输入:

php demo/queue.php PHP_Job

结果可以看到屏幕上输出:

Queued job b1f01038e5e833d24b46271a0e31f6d6

即Job已经添加成功。注意这里的Job名称与我们编写的Job Class名称保持一致:PHP_Job

查看Job运行情况

php-resque同样提供了查看Job运行状态的例子,直接运行:

php demo/check_status.php b1f01038e5e833d24b46271a0e31f6d6

可以看到输出为:

Tracking status of b1f01038e5e833d24b46271a0e31f6d6. Press [break] to stop. 
Status of b1f01038e5e833d24b46271a0e31f6d6 is: 1

我们刚才创建的Job状态为1。在Resque中,一个Job有以下4种状态:

  • Resque_Job_Status::STATUS_WAITING = 1; (等待)

  • Resque_Job_Status::STATUS_RUNNING = 2; (正在执行)

  • Resque_Job_Status::STATUS_FAILED = 3; (失败)

  • Resque_Job_Status::STATUS_COMPLETE = 4; (结束)

因为没有Worker运行,所以刚才创建的Job还是等待状态。

运行Worker

这次我们直接编写demo/resque.php:

<?php
date_default_timezone_set('GMT');
require 'job.php';
require '../bin/resque';

可以看到一个Worker至少需要两部分:

  1. 可以直接包含Job类文件,也可以使用php的自动加载机制,指定好Job Class所在路径并能实现自动加载

  2. 包含Resque的默认Worker: bin/resque

在终端中运行:

QUEUE=default php demo/resque.php

前面的QUEUE部分是设置环境变量,我们指定当前的Worker只负责处理default队列。也可以使用

QUEUE=* php demo/resque.php

来处理所有队列。

运行后输出为

#!/usr/bin/env php
*** Starting worker

用ps指令检查一下:

ps aux | grep resque

可以看到有一个php的守护进程已经在运行了

1000      4607  0.0  0.1  74816 11612 pts/3    S+   14:52   0:00 php demo/resque.php

再使用之前的检查Job指令

php demo/check_status.php b1f01038e5e833d24b46271a0e31f6d6

2分钟后可以看到

Status of b1f01038e5e833d24b46271a0e31f6d6 is: 4

任务已经运行完毕,同时屏幕上应该可以看到输出的Hello!

至此我们已经成功的完成了一个最简单的Resque实例的全部演示,更复杂的情况以及遗留的问题会在下一次的日志中说明。

相关推荐:

利用redis和php-resque实现后台任务 redis 下载 redis 集群 redis可视化工具

php网站设计 php-resque的设计和使用

php-resque消息队列的执行情况

The above is the detailed content of php-resque usage instructions. 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