Php-resque Simple PHP Message Queue

Source: Internet
Author: User
Tags message queue install redis

Absrtact: Message Queuing is a good thing, a lot of xxxmq. However, look at their documentation, you have to scare, what a ghost, I just want to use it to trigger a text interface. Fortunately, there are simple. This is php-resque.

Installation
    • First, the goods need to run under Linux, it is necessary to use Windows do not look, nor can not be installed, laborious and performance slag
    • Must have composer, too slow also down, don't look, this article does not introduce no composer how to do
    • Install composer is not the main point of this article, in Ubuntu (in fact, in order not to toss QQ I pretend to be deepin) is a line of command
    • PHP version to be a little higher, under 5.4 don't look.
    • The environment PHP is apt installed, so everything is no hindrance, if it is compiled installation may not be so smooth
Installing composer
sudo apt-get install composer

Installing Redis

Redis is a database, NoSQL. It's not nonsense, because someone really doesn't know.

sudo apt-get install Redis-server

You have to have git.
sudo apt-get install git

The real installation
cd/your/www/clone git://github.com/chrisboulton/php-resque.gitcd php-  Resquecomposer Install

No unexpected words can be installed successfully, the convulsions on more than a few times, the package is not big, can endure

Demo

Some of the online demo, are copied the same blog post, ye can find here is probably the one that looked not right. Composer installs the package inside has the demo, inside the Code also TM Pit's very. So I still read this article ....

First, let's write a simple job.

Job, work, is to break up, after receiving the news work, good, defined

<? PHP // job.php put in the demo directory, the original deleted the calculation of the ball class testjob{    publicfunction  perform ()    {        echo$this ->args[' name '];}    }

Write a script and write a message to the queue.
<?PHP//demo/quere.php Note that this is a command-line version, you need to run in the shell, you can also write a browser can run//function, in the queue to write some forced namesif(Empty($argv[1])) {     die(' Specify the name of a job to add e.g, PHP queue.php php_job ');}require__dir__. '/init.php ';d Ate_default_timezone_set (' GMT '); Resque:: Setbackend (' 127.0.0.1:6379 ');$names= [    ' Li ', ' Lengwenqing ', ' Yin ping ', ' Chamaenerion angustifolium song ', ' Qin Water branch ', ' Li Nianle ', ' Wentayi ', ' Liu Ying ', ' Gu Moyan ', ' ren Shui Han ', ' Gold grinding needles ', ' ding Linglong ', ' Ling ', ' Water Sheng ', ' Jing Yin Meng ', ' Rong Qi Yan ', ' Lin Mo ', ' Hua Shi ', ' Thousand Mae ' , ' Sword dance ', ' Lan Ling ', ' Luo away '];foreach($names  as $name){    $jobId= Resque::enqueue (' Default ', ' testjob ', [' name ' = =]$name]); Echo"Queued job".$jobId. "\ n"; }```## Write a daemon script"' PHP<?PHP//demo/resque.php Copy Original demo, will find the file, so can copy me this//This script is also running in the shell, the real application should be put into the bootDate_default_timezone_set (' GMT ');require' Bad_job.php ';require' Job.php ';require' Php_error_job.php ';require'/install-path/php-resque/bin/resque ';

Try it.

Start the daemon process first

queue=* PHP demo/resque.php

will have the following output

#!/usr/bin/Env Php[notice] Starting worker your-computer-name:10757:*

Open a different terminal, type

PHP demo/queue.php testjob

There will be output like this, and those hash strings are task IDs.

Queued job 4b510e225af5897bd5022fee30d202bfqueued Job d14a2ed9339f739b2dec0e0c64069dd7queued Job d58a536dab2fde9aa6097b37577bd02aqueued Job 53ed1a64d7d8e9a4fed15a0942587e64queued Job 178ea9087a159c9c7b74b8d9d87e40c2queued Job cc5dd2087de6865e613fcfdebd52abbcqueued Job e647351c4a83b04b3d701a5dafa52118queued Job 3f69bd449888e5adb5984a0d64aeb3acqueued Job 31e114326c8db42443029e14bd677cdbqueued Job 28404f8202a22ade5d204345596d10e0queued Job fefcf7c402b09c92de2eac5aa642ca80queued Job e6ecf1529d97e568c5f80bb330c423f2queued Job c92e64ca49948e7547a5dacce2f373fbqueued Job 97a504c56d74f7714781318b6d244ae6queued Job 2f8e51937016fc981e0426fcef5d1643queued Job db3be312811732803fc5d7b814cc69d4queued Job 23124e4693146812471c09401137a6fdqueued Job af070ce348f73c6c6fe25782351a1937queued Job 8825fc73b7881e13067e59d10287b598queued Job 21e31139c9f70c7097927a80a442a577queued Job 283a8d6119657dd018a5cc8298021bba

At this point you see just the daemon running the terminal, there will be similar to the following output, as expected output a series of loaded offenders

[Notice] Starting work on (job{default} | id:3dcf4dc9008d255ab59917a221aaf984 | Testjob | [{"name":"\u674e\u7075\u9edb"}]) Li [notice] (job{default} | id:3dcf4dc9008d255ab59917a221aaf984 | Testjob | [{"name":"\u674e\u7075\u9edb"}]) have finished[notice] starting work on (job{default} | id:fa2d6bd7a97a77ab754c35822cb8dcd0 | Testjob | [{"name":"\u51b7\u6587\u537f"}]) Lengwenqing [notice] (job{default} | id:fa2d6bd7a97a77ab754c35822cb8dcd0 | Testjob | [{"name":"\u51b7\u6587\u537f"}]) have finished[notice] starting work on (job{default} | id:0962a49c4ffd3e987864b2c7e2fdf857 | Testjob | [{"name":"\u9634\u9732\u840d"}]) Shiruping [notice] (job{default} | id:0962a49c4ffd3e987864b2c7e2fdf857 | Testjob | [{"name":"\u9634\u9732\u840d"}]) have finished[notice] starting work on (job{default} | id:51ecee3f45d0ba8a906c438ad8ad1887 | Testjob | [{"name":"\u67f3\u5170\u6b4c"}]) chamaenerion angustifolium song [notice] (job{default} | id:51ecee3f45d0ba8a906c438ad8ad1887 | Testjob | [{"name":"\u67f3\u5170\u6b4c"}]) have finished[notice] starting work on (job{default} | id:6dbdf11ee30ab874a94d239d93637671 | Testjob | [{"name":"\u79e6\u6c34\u652f"}]) Qingshui branch [notice] (job{default} | id:6dbdf11ee30ab874a94d239d93637671 | Testjob | [{"name":"\u79e6\u6c34\u652f"}]) have finished[notice] starting work on (job{default} | id:e566a95f0666dcca8fa30c1ed984434f | Testjob | [{"name":"\u674e\u5ff5\u513f"}]) Li Nianle [notice] (job{default} | id:e566a95f0666dcca8fa30c1ed984434f | Testjob | [{"name":"\u674e\u5ff5\u513f"}]) have finished[notice] starting work on (job{default} | id:826cfe9d90f06f06f19c790bae54955d | Testjob | [{"name":"\u6587\u5f69\u4f9d"}]) Wentayi [notice] (job{default} | id:826cfe9d90f06f06f19c790bae54955d | Testjob | [{"name":"\u6587\u5f69\u4f9d"}]) have finished[notice] starting work on (job{default} | id:106843bea773add35576cb796f5a2036 | Testjob | [{"name":"\u67f3\u5a75\u8bd7"}]) Liu Ying [notice] (job{default} | id:106843bea773add35576cb796f5a2036 | Testjob | [{"name":"\u67f3\u5a75\u8bd7"}]) has finished ..... 

Finish the call.

Here is a simple example, actually do anything, send text messages, send e-mails, do not worry about the database operation, and so on, just job.php inside write a new class can, to the queue of some messages when attention to mark the parameters on the line

Resque::enqueue ('default' 'ClassName', [' parameter One  " parameter two ' = = $param 2,]);

Simple application completely enough.

Copied from https://my.oschina.net/inuxor/blog/635959

Php-resque Simple PHP Message Queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.