Gearman-php Net_Gearman Library Learning

Source: Internet
Author: User
: This article describes how to learn the Net_Gearman library of gearman-php. if you are interested in the PHP Tutorial, refer to it. According to this English Post
Original code backup

Example 1:
As we show the basic architecture of Net_Geaman, the main client code is as follows:

// A client object that requires jobserver information, an array $ client = new Net_Gearman_Client (gm: $ servers ); // Example1 is the function name $ client-> Example1 (array ('date '));

Main worker code:

// Worker object, which requires jobserver information $ worker = new Net_Gearman_Worker (gm: $ servers); // pass in the function name, ability to process Example1 $ worker-> addAbility ('example1'); // loop $ worker-> beginWork ();

It is slightly different from the traditional gearman api. Net_Geaman requires the user to implement the function in subclass form, for example, Example1.php file code snippet:

ClassNet_Gearman_Job_Example1extendsNet_Gearman_Job_Common {// arg is an array. it corresponds to publicfunctionrun ($ arg) {$ cmd = $ arg [0] for the input parameter array in the client. //... return $ result ;}}

In this example, the client calls Example1, which does not exist and uses the _ call mechanism. Useless.

Example 2:
The client introduces the concepts of task and set, so that multiple tasks can be accumulated into one set and sent together. The client segment:

$set=new Net_Gearman_Set();$task=new Net_Gearman_Task ('Example1', array ('date'));$set->addTask ($task);$client->runSet ($set);

Example 3:
Added the callback function after the task is completed. the callback parameters include function name, handle (wrapped jobserver: number, which uniquely identifies the task), and return values of the task.

$task->attachCallback ("complete",Net_Gearman_Task::TASK_COMPLETE);//...function complete ($func, $handle, $result) {    gm::log_msg ("[gm_client] complete ($handle/$func)");    gm::log_msg ("[gm_client] result: " . $result['result']);}

Example 4:
It mainly shows how to return data through the complete callback.
Example 5:
Shows how to add a fail callback to the client. when a Net_Gearman_Job_Exception exception is thrown in the function, a fail-type callback is triggered. code snippet:

classNet_Gearman_Job_Example3extendsNet_Gearman_Job_Common{publicfunctionrun($arg)    {if (count ($arg) != 1)        {            thrownew Net_Gearman_Job_Exception ("must provide exactly one command to run");        }        //...
$task->attachCallback ("complete",Net_Gearman_Task::TASK_COMPLETE);$task->attachCallback ("fail",Net_Gearman_Task::TASK_FAIL);//...function fail ($task) {    gm::log_msg ("[gm_client] fail, task: " . print_r ($task, true));}

However, the exception content is not returned to the fail callback function. Therefore, we recommend that you use the normal complete callback. you can capture the failure information and place it in the returned value.
Example 6:
The monitor function is used in the worker. code snippet:

$worker->beginWork ('monitor');// ...functionmonitor($idle, $time_of_last_job){$idle_str = ($idle) ? 'idle' : 'not idle';    $time_of_last_job_str = date ('r', $time_of_last_job);    gm::log_msg ("[gm_worker] status: $idle_str, time of last job: $time_of_last_job_str");}

The trigger events include:
Before the job starts.
After the job is complete.
Wait for the job to be triggered once every minute.

Example 7:
The start, complete, and fail callbacks of the task are added on the worker side. you can do something in it, code snippet:

$worker->attachCallback ('job_start', Net_Gearman_Worker::JOB_START);    $worker->attachCallback ('job_complete', Net_Gearman_Worker::JOB_COMPLETE);    $worker->attachCallback ('job_fail', Net_Gearman_Worker::JOB_FAIL);

Example 8:
Is a big synthesis, mainly has the following points worth noting:
1. although I like php very much, I think it is not very safe to let php assume a daemon worker. it may involve abnormal exit, unreasonable memory usage, or something that may affect performance, therefore, he provides a policy: each worker has an instance number and uses a file lock to ensure that only one worker instance number is running. the worker automatically exits after processing a certain number of jobs, use the crontab task to pull the worker in seconds.
2. the beginWorker exit of a worker can be determined in the monitor callback. the monitor callback has this feature: If the callback function returns false, the worker continues the loop. if the return value is true, the loop exits, this is also confirmed in the worker source code:

if (call_user_func($monitor, $idle, $lastJob) == true) {$working = false;}

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.