phlib / beanstalk
Library for handling beanstalk connections.
Installs: 6 661
Dependents: 1
Suggesters: 0
Security: 0
Stars: 9
Watchers: 3
Forks: 1
Open Issues: 3
Requires
- php: ^7.4|^8.0
- phlib/console-configuration: ^2
- psr/log: ^1 || ^2 || ^3
- symfony/console: ^5.4 || ^6
Requires (Dev)
Suggests
- psr/log-implementation: Optional Logger can capture connection failures
This package is auto-updated.
Last update: 2024-11-13 05:37:06 UTC
README
Beanstalkd library implementation.
Install
Via Composer
$ composer require phlib/beanstalk
Basic Usage
<?php use Phlib\Beanstalk\Connection; // producer $beanstalk = new Connection('127.0.0.1'); $beanstalk->useTube('my-tube'); $beanstalk->put(array('my' => 'jobData'));
<?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']);
Connection configuration
Options
Pool configuration
Factory
The factory allows for easy setup of the objects. This especially useful when creating a pool of beanstalk servers. The following example lists the various ways it can be used. The configuration examples in the command line section are created using the factory.
$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 Configuration
The configuration options are as specified above.
With the exception that when creating a pool there is an optional enabled
.
$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], ]);
Pool
The pool allows for work to be pushed to and retrieved from multiple servers. The pool implements the connection interface.
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')); // )
Alternative way to create a Pool, using the Factory to construct the connections:
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);
Command Line Script
./vendor/bin/beanstalk
Running the script will provide you with a list of options. Most are self-explanatory. By default no configuration is required, the script will default to localhost.
Command Line Configuration
There are 2 ways of specifying a configuration.
- Create a file called beanstalk-config.php either in
/app/root/
or/app/root/config/
. - Create a file with a name of your choosing and specify it
using the command option
-c /path/to/my/config.php
.
The file must return an array containing the beanstalk configuration. This configuration will be passed to the Factory to create an instance.
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, ], ];
require_once 'my/app/bootstrap.php'; $app = new MyApp(); return $app['config']['beanstalk'];
License
This package is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this program. If not, see http://www.gnu.org/licenses/.