PHP code example of phlib / beanstalk

1. Go to this page and download the library: Download phlib/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/ */

    

phlib / beanstalk example snippets


$factory = new \Phlib\Beanstalk\Factory();

$beanstalk = $factory->create('localhost');

$beanstalk = $factory->createFromArray([
    'host' => 'localhost',
]);

$beanstalk = $factory->createFromArray([
    ['host' => '10.0.0.1'],
    ['host' => '10.0.0.2'],
    ['host' => '10.0.0.3'],
]);

$factory = new \Phlib\Beanstalk\Factory();

$beanstalk = $factory->createFromArray([
    ['host' => '10.0.0.1', 'enabled' => true],
    ['host' => '10.0.0.2', 'enabled' => false],
    ['host' => '10.0.0.3', 'enabled' => true],
]);

use Phlib\Beanstalk\Connection;
use Phlib\Beanstalk\Pool;

$connections = [
    new Connection('10.0.0.1'),
    new Connection('10.0.0.2'),
    new Connection('10.0.0.3'),
    new Connection('10.0.0.4'),
];
$logger = new MyLogger();
$pool = new Pool($connections, 120, $logger);

$pool->useTube('my-tube');
$pool->put(array('my' => 'jobData1')); // )
$pool->put(array('my' => 'jobData2')); // )-> distributed between random servers
$pool->put(array('my' => 'jobData3')); // )

use Phlib\Beanstalk\Factory;
use Phlib\Beanstalk\Pool;

$connections = (new Factory())->createConnections([
    ['host' => '10.0.0.1', 'enabled' => true],
    ['host' => '10.0.0.2', 'enabled' => false],
    ['host' => '10.0.0.3', 'enabled' => true],
]);
$logger = new MyLogger();
$pool = new Pool($connections, 120, $logger);

return [
    'host' => '10.0.0.1',
    'port' => 11300
];

// pool configuration
return [
    [
        'host' => '10.0.0.1',
        'port' => 11300,
    ],
    [
        'host' => '10.0.0.2',
        'port' => 11300,
    ],
    [
        'host' => '10.0.0.3',
        'port' => 11300,
        'enabled' => false,
    ],
];



$app = new MyApp();
return $app['config']['beanstalk'];

 php

use Phlib\Beanstalk\Connection;

// consumer
$beanstalk = new Connection('127.0.0.1');
$beanstalk->watch('my-tube')
    ->ignore('default');
$job = $beanstalk->reserve();
$myJobData = $job['body'];
$beanstalk->delete($job['id']);