Home >Backend Development >PHP Tutorial >Learning the Net_Gearman library of gearman-php

Learning the Net_Gearman library of gearman-php

WBOY
WBOYOriginal
2016-08-08 09:26:241664browse

Learn according to this English post
Backup of original code

Example 1:
Like we showed the basic architecture of Net_Geaman, the main code on the client side:

<code><span>// 一个client对象,需要jobserver信息,一个数组</span><span>$client</span> = <span>new</span> Net_Gearman_Client (gm::<span>$servers</span>);
<span>// Example1就是function name</span><span>$client</span>->Example1 (<span>array</span> (<span>'date'</span>));</code>

The main code on the worker side:

<code><span>// worker对象,需要jobserver信息</span><span>$worker</span> = <span>new</span> Net_Gearman_Worker (gm::<span>$servers</span>);
<span>// 传入function name,给他处理Example1的能力</span><span>$worker</span>->addAbility (<span>'Example1'</span>);
<span>// loop</span><span>$worker</span>->beginWork ();</code>

is slightly different from the traditional gearman api. Net_Geaman requires users to implement functions in the form of subclasses, such as the Example1.php file code Snippet:

<code><span><span>class</span><span>Net_Gearman_Job_Example1</span><span>extends</span><span>Net_Gearman_Job_Common</span>
{</span><span>// arg 是数组,与client里面的入参array对应</span><span>public</span><span><span>function</span><span>run</span><span>(<span>$arg</span>)</span>
    {</span><span>$cmd</span> = <span>$arg</span>[<span>0</span>];
    <span>//...</span><span>return</span><span>$result</span>;
    }
}</code>

One of the tricks in Example 1 is that the client calls the non-existent method Example1, using the _call mechanism. Useless.

Example 2:
The client side introduces the concepts of task and set, so that multiple tasks can be accumulated into a set and issued together. Client fragment:

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

Example 3:
A callback function is added after the task is completed. The parameters of the callback include function name, handle (wrap jobserver:number, mainly to uniquely identify the task), and the return value of the task.

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

Example 4:
It mainly shows the data returned through the complete callback.
Example 5:
Shows that the client side adds a fail callback. When a Net_Gearman_Job_Exception exception is thrown in the function, a fail type callback will be triggered. Code snippet:

<code><span><span>class</span><span>Net_Gearman_Job_Example3</span><span>extends</span><span>Net_Gearman_Job_Common</span>
{</span><span>public</span><span><span>function</span><span>run</span><span>(<span>$arg</span>)</span>
    {</span><span>if</span> (count (<span>$arg</span>) != <span>1</span>)
        {
            <span>throw</span><span>new</span> Net_Gearman_Job_Exception (<span>"must provide exactly one command to run"</span>);
        }
        <span>//...</span></code>
<code>$task->attachCallback (<span>"complete"</span>,Net_Gearman_Task::TASK_COMPLETE);
$task->attachCallback (<span>"fail"</span>,Net_Gearman_Task::TASK_FAIL);
//<span>...</span><span>function</span> fail ($task) {
    gm::log_msg (<span>"[gm_client] fail, task: "</span> . print_r ($task, true));
}</code>

However, the exception content will not be passed back to the fail callback function, so it is recommended to use the normal complete callback. , users can capture the failure information themselves and put it in the return value.
Example 6:
The monitor function is used in the worker. The code snippet is:

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

The triggering events are:
Before the job starts.
After the job is completed.
When waiting for a job, it is triggered once a minute.

Example 7:
The main thing is to add the start, complete and fail callbacks of the task on the worker side, so that you can do something inside. Code snippet:

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

Example 8:
It is a comprehensive review, with the following main points worth noting:
1. Although the author likes PHP very much, he also believes that it is not very safe to let PHP take on the role of a daemon worker. It may involve abnormal exits, unreasonable memory usage, etc., which may affect performance, so he provides a strategy: Each worker has an instance number. File locks are used to ensure that only one worker is running per instance number. The worker automatically exits after processing a certain number of jobs. The crontab task is used to pull up the worker in seconds.
2. The exit of the worker's beginWorker can be judged in the monitor callback. The monitor callback has this characteristic: if the callback function returns false, the worker continues the loop, and if the callback function returns true, the loop exits. This is also confirmed in the source code of the worker:

<code><span>if</span> (call_user_func(<span>$monitor</span>, <span>$idle</span>, <span>$lastJob</span>) == <span>true</span>) {
<span>$working</span> = <span>false</span>;
}</code>

The above introduces the learning of the Net_Gearman library of gearman-php, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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