PHP code example of udokmeci / yii2-beanstalk

1. Go to this page and download the library: Download udokmeci/yii2-beanstalk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

udokmeci / yii2-beanstalk example snippets

 php
'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 
],
 php 
\Yii::$app->beanstalk
    ->putInTube('tube', $mixedData, $priority, $delay);
 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;
	    }
	}
}