https://github.com/phlib/beanstalk
PHP beanstalk connection and connection pool library
https://github.com/phlib/beanstalk
Last synced: 5 months ago
JSON representation
PHP beanstalk connection and connection pool library
- Host: GitHub
- URL: https://github.com/phlib/beanstalk
- Owner: phlib
- License: lgpl-3.0
- Created: 2014-07-11T14:04:05.000Z (almost 12 years ago)
- Default Branch: main
- Last Pushed: 2025-07-31T11:07:47.000Z (11 months ago)
- Last Synced: 2025-10-03T20:52:48.759Z (9 months ago)
- Language: PHP
- Size: 468 KB
- Stars: 9
- Watchers: 2
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# phlib/beanstalk
[](https://github.com/phlib/beanstalk/actions/workflows/code-checks.yml)
[](https://codecov.io/gh/phlib/beanstalk)
[](https://packagist.org/packages/phlib/beanstalk)
[](https://packagist.org/packages/phlib/beanstalk)

Beanstalkd library implementation.
## Install
Via Composer
``` bash
$ composer require phlib/beanstalk
```
## Basic Usage
``` php
useTube('my-tube');
$beanstalk->put(array('my' => 'jobData'));
```
``` php
watch('my-tube')
->ignore('default');
$job = $beanstalk->reserve();
$myJobData = $job['body'];
$beanstalk->delete($job['id']);
```
## Connection configuration
|Name|Type|Required|Default|Description|
|----|----|--------|-------|-----------|
|`host`|*String*|Yes| |Hostname or IP address.|
|`port`|*Integer*|No|`11300`|Beanstalk's port.|
|`options`|*Array*|No|``|Connection options for Beanstalk.|
### Options
|Name|Type|Default|Description|
|----|----|-------|-----------|
|`timeout`|*Integer*|`60`|The connection timeout.|
## Pool configuration
|Name|Type|Required|Default|Description|
|----|----|--------|-------|-----------|
|`connections`|*ConnectionInterface[]*|Yes| |Array of server connections.|
|`retryDelay`|*Integer*|No|`600`|How long to delay retrying a connection for after an error.|
|`logger`|*LoggerInterface*|No| |Optional Logger to capture connection failures.|
## 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.
```php
$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`.
```php
$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.
```php
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:
```php
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
```bash
./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.
1. Create a file called *beanstalk-config.php* either in
```/app/root/``` or ```/app/root/config/```.
2. 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.
```php
return [
'host' => '10.0.0.1',
'port' => 11300
];
```
```php
// 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,
],
];
```
```php
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 .