承接 kicken/gearman-php 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kicken/gearman-php

最新稳定版本:1.4.0

Composer 安装命令:

composer require kicken/gearman-php

包简介

A PHP implementation of the Gearman protocol.

README 文档

README

This library provides a pure PHP implementation of the gearman protocol for creating clients and workers. It provides an alternative to the PHP extension which is not always available.

This library is not a replacement for the existing extension. This library provides an alternative and incompatible API.

Installation

Simply require the kicken/gearman-php package with composer to install the latest version.

composer require kicken/gearman-php

Quick start guide

Below is a quick start guide to setting up workers, submitting jobs, and getting a jobs status. For full details about the API's available, dig into the source code and have a look around.

Workers

Creating workers involves registering different callback functions with an instance of the Worker class and then calling the work method to start accepting and performing work. For example:

<?php

$worker = new \Kicken\Gearman\Worker('127.0.0.1:4730');
$worker
    ->registerFunction('rot13', function(\Kicken\Gearman\Job\WorkerJob $job){
        $workload = $job->getWorkload();
        echo "Running rot13 task with workload {$workload}\n";

        return str_rot13($workload);
    })
    ->work()
;

Submitting Jobs

Jobs can be submitted to the workers using the Client class and either the submitJob or submitBackgroundJob functions. For example:

$client = new \Kicken\Gearman\Client('127.0.0.1:4730');
$job = $client->submitBackgroundJob('rot13', 'Foobar');
echo $job;

A background job will not be able to provide any data back to the client that submitted the job. The only information that can be obtained from a background job is status by using the getJobStatus function.

A non-background job is able to provide a result or other data back to the client as it is processed. The client needs to wait for the job to complete to access this data. This can be accomplished by using the wait function.

There are two ways of getting information regarding a non-background job. First, the client can wait for it to complete, then access the information from the job object.

$client = new \Kicken\Gearman\Client('127.0.0.1:4730');
$job = $client->submitJob('rot13', 'Foobar');
$client->wait();

echo $job->getResult();

Second, the client can register different callbacks which will be executed as information becomes available.

$client = new \Kicken\Gearman\Client('127.0.0.1:4730');
$job = $client->submitJob('rot13', 'Foobar');
$job->onStatus(function(\Kicken\Gearman\Job\ClientJob $job){
    echo $job->getProgressPercentage()."% complete\n";
})->onComplete(function(\Kicken\Gearman\Job\ClientJob $job){
    echo $job->getResult();
});
$client->wait();

Checking a jobs status

If you save the handle to a background job, you can check it's status using the getJobStatus function to determine when it is complete, and how far along it is (if the worker provides progress information).

$client = new \Kicken\Gearman\Client('127.0.0.1:4740');
$status = $client->getJobStatus($jobHandle); //previously saved $jobHandle
$client->wait();

var_dump($status->isKnown(), $status->isRunning(), $status->getNumerator(), $status->getDenominator());

Timeouts

Both clients and workers have a configurable timeout setting. The timeout value only controls network communications and how long the client or worker will wait for data from the server. By default there is no timeout so clients and workers will wait as long as necessary for the network.

To configure a timeout for both workers and clients call the setTimeout method and give it a timeout in milliseconds or a boolean true/false value. Specifying false will permit the code to wait indefinitely. Specifying true will use PHP's default_socket_timeout ini setting.

统计信息

  • 总下载量: 57.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 16
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 16
  • Watchers: 4
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-11-14