yii-dbeanstalkd Distributed wrapper for beanstalkd client with basic weighted algorithm and graceful handling of connection failures

  1. Requirements
  2. Usage
  3. Resources

Distributed wrapper for yii for davidpersson's beanstalkd client

This project differs from other yii extensions in that it provides a very trivial method of managing multiple, distributed beanstalkd servers. Other projects, or classes, differ in that they build upon existing implementations but do not offer any method of failover or recovery from a server failure.

Since a single server going down would cause complete failure this must, at the very least:

  1. be aware of multiple servers/connections
  2. be able to choose between them based upon a simple weighted algorithm
  3. be able to use a different server if one is unavailable In the event of a server failure, this class makes no attempt to ensure enqueued items are not lost since. This will allow the user to enqueue in another server and keep going.

Requirements

  • davidpersson's beanstalkd client to be added to your yii project
  • modifications to config files to support these additions
Add davidpersson's beanstalkd client

Clone davidpersson's library and place it in the extensions folder of your yii application, in a folder named 'beanstalk'.

Modify config files to support these additions

Ensure that the Beanstalk.php class from this project has been placed in your components folder.

Ensure this line has been added under the aliases section of your yii config file:

'aliases'=>array(
        // davidpersson's library has been installed to extensions/beanstalk folder
        'Beanstalk'=> 'application.extensions.beanstalk.src',
),

Ensure these lines have been added to the components section of your yii config file:

'components'=>array(
        'beanstalk'=>array(
            'class'=>'application.components.Beanstalk',
            'servers'=>array(
                'server1'=>array(
                    'host'=>'192.168.0.10',
                    'port'=>11300,
                    'weight'=>50,
                    // array of connections/tubes
                    'connections'=>array(),
                ),
                'server2'=>array(
                    'host'=>'192.168.0.11',
                    'port'=>11300,
                    'weight'=>50,
                    // array of connections/tubes
                    'connections'=>array(),
                ),
            ),
        ),
),

Usage

Producer example
/*
 * connect to a server (determines by weight or selects only existing server::
 */
$client = Yii::app()->beanstalk->getClient();
$client->connect();

/* 
 * alternatively, one can connect to a server by name:
 */
//$client = Yii::app()->beanstalk->getClient('server1');
//$client->connect();

$client->useTube('default');

//echo "Tube used: {$client->listTubeUsed()}.\n";
//print_r($client->stats());

$jobDetails = [
    'application'=>'beanstalk test',
    'payload'=>'performing a test on: '.date('Y-m-d H:i:s'),
];
$jobDetailString = json_encode($jobDetails);

$ret = $client->put(
    0, // priority
    0, // do not wait, put in immediately
    90, // will run within n seconds
    $jobDetailString // job body
);

echo "Added $jobDetailString to queue.\n";
echo "Return was: $ret.\n";

$client->disconnect();
Consumer Examples
Peeking
$peek = Yii::app()->beanstalk->getClient();
$peek->connect();
$peek->watch('default');

$job = $peek->reserve(5);
print_r($job);

$result = touch($job['body']);
print_r($job);
print_r($result);
$peek->disconnect();
Consuming
$consumer = Yii::app()->beanstalk->getClient();
$consumer->connect();
$consumer->watch('default');

while(true)
{
    $job = $consumer->reserve();
    $result = touch($job['body']);

    if( $result )
    {
        // do something with the job request
        echo "Done...\n";
        $consumer->delete($job['id']);
    }
    else
    {
        // handle failure here
        echo "Burying...\n";
        $consumer->bury($job['id']);
    }
}

$consumer->disconnect();

Resources

Inspiration
Great related tools
1 0
3 followers
145 downloads
Yii Version: 1.1
License: MIT
Category: Others
Developed by: cottonaf
Created on: May 20, 2015
Last updated: 9 years ago

Downloads

show all

Related Extensions