承接 udokmeci/yii2-beanstalk 相关项目开发

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

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

udokmeci/yii2-beanstalk

Composer 安装命令:

composer require udokmeci/yii2-beanstalk

包简介

Yii2 Beanstalk Client at the top of Paul Annesley's pheanstalk

README 文档

README

Yii2 beanstalkd web and console component which is an interface on the top of pda/pheanstalk. Thanks Paul Annesley for such a complete work.

How to use?

Installation with Composer

Just add the line under require object in your composer.json file.

{
  "require": {
  ...
    "udokmeci/yii2-beanstalk" : "^1.0.0"
  }
}

Then run

$> composer update

Configuration

Now add following lines in to your main and console configuration file under components

'beanstalk'=>[
    'class' => 'udokmeci\yii2beanstalk\Beanstalk',
    'host' => '127.0.0.1', // default host
    'port' => 11300, //default port
    'connectTimeout' => 1,
    'sleep' => false, // or int for usleep after every job 
],

Now add following in to your console configuration only.

...
'params' => $params,
// add you controller with name and class name next to params.
'controllerMap' => [
    'worker' => [
        'class' => 'app\commands\WorkerController',
    ]
],

Producing

Now if everything is fine, you can run beandstalkd and access to controller as

\Yii::$app->beanstalk
    ->putInTube('tube', $mixedData, $priority, $delay);

$mixedData is added on v1.0 for complex usage. Anything else then string will be send as json format. So you can sent anything within it suppoted by json.

Worker

For worker it also has a built in controller which runs an infinite loop and wait for new jobs. Most of the work is done in BeanstalkController. All you have to do is to create a controller and action like below.

Controller

Create a controller under your commands folder. Give the name anything you want to it and extend your controller from udokmeci\yii2beanstalk\BeanstalkController

Example Controller
<?php
namespace app\commands;

use udokmeci\yii2beanstalk\BeanstalkController;
use yii\helpers\Console;
use Yii;

class WorkerController extends BeanstalkController
{
  // Those are the default values you can override

  const DELAY_PRIORITY = "1000"; //Default priority
  const DELAY_TIME = 5; //Default delay time

  // Used for Decaying. When DELAY_MAX reached job is deleted or delayed with 
  const DELAY_MAX = 3; 

  public function listenTubes(){
    return ["tube"];
  }

  /**
   *
   * @param Pheanstalk\Job $job
   * @return string  self::BURY
   *                 self::RELEASE
   *                 self::DELAY
   *                 self::DELETE
   *                 self::NO_ACTION
   *                 self::DECAY
   *  
   */
  public function actionTube($job){
	    $sentData = $job->getData();
	    try {
    	   // something useful here



           if($everythingIsAllRight == true){
                fwrite(STDOUT, Console::ansiFormat("- Everything is allright"."\n", [Console::FG_GREEN]));
                //Delete the job from beanstalkd
                return self::DELETE; 
           }

           if($everythingWillBeAllRight == true){
                fwrite(STDOUT, Console::ansiFormat("- Everything will be allright"."\n", [Console::FG_GREEN]));
                //Delay the for later try
                //You may prefer decay to avoid endless loop
                return self::DELAY; 
           }

           if($IWantSomethingCustom==true){
                Yii::$app->beanstalk->release($job);
                return self::NO_ACTION;
           }

           fwrite(STDOUT, Console::ansiFormat("- Not everything is allright!!!"."\n", [Console::FG_GREEN]));
           //Decay the job to try DELAY_MAX times.
           return self::DECAY; 

           // if you return anything else job is burried.
	    } catch (\Exception $e) {
            //If there is anything to do.
            fwrite(STDERR, Console::ansiFormat($e."\n", [Console::FG_RED]));
            // you can also bury jobs to examine later
            return self::BURY;
	    }
	}
}
Running Worker

Running console is the easiest part. Run ./yii Your controller

$> php ./yii worker

Controller will tell you, whether there are actions for correspanding tubes or beanstalk server is accessible and which tubes are listening currently. The controller handles with signals. So exit whenever you want, reserved job will not be hanged.

Any forks are welcome.

udokmeci/yii2-beanstalk 适用场景与选型建议

udokmeci/yii2-beanstalk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 130.08k 次下载、GitHub Stars 达 70, 最近一次更新时间为 2014 年 11 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「beanstalkd」 「yii2」 「beanstalk client」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 udokmeci/yii2-beanstalk 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 130.08k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 70
  • 点击次数: 22
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 70
  • Watchers: 7
  • Forks: 26
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-11-22