Giter VIP home page Giter VIP logo

yii2-beanstalk's Introduction

yii2-beanstalk

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" : "dev-master"
  }
}

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 everthing 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 an 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($everthingIsAllRight == true){
                fwrite(STDOUT, Console::ansiFormat("- Everything is allright"."\n", [Console::FG_GREEN]));
                //Delete the job from beanstalkd
                return self::DELETE; 
           }

           if($everthingWillBeAllRight == 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, wheater 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.

yii2-beanstalk's People

Contributors

udokmeci avatar elvenpath avatar jcherniak avatar drsdre avatar g1k avatar mitrm avatar vitaliykoziy avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.