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

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

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

kicken/gearman-php

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.

kicken/gearman-php 适用场景与选型建议

kicken/gearman-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 64.06k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2015 年 11 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 kicken/gearman-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kicken/gearman-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 kicken/gearman-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

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