The LeezyPheanstalkBundle is a Symfony2 Bundle that provides a command line interface for manage the Beanstalkd workqueue server & a pheanstalk integration.
<?php
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HomeController extends Controller {
public function indexAction() {
$pheanstalk = $this->get("leezy.pheanstalk");
// ----------------------------------------
// producer (queues jobs)
$pheanstalk
->useTube('testtube')
->put("job payload goes here\n");
// ----------------------------------------
// worker (performs jobs)
$job = $pheanstalk
->watch('testtube')
->ignore('default')
->reserve();
echo $job->getData();
$pheanstalk->delete($job);
}
}
?>
The LeezyPheanstalkBundle provides a number of command line utilities. Commands are available for the following tasks:
- Delete a job.
- Flush a tube.
- List available tubes.
- Pause a tube.
- Peek a job and get associated data.
- Put a new job in a tube.
- Get statistics about beanstalkd server.
- Get statistics about a job.
- Get statistics about a tube.
Note:
You must have correctly installed and configured the LeezyPheanstalkBundle before using
these commands.
$ php app/console leezy:pheanstalk:delete-job 42
$ php app/console leezy:pheanstalk:flush-tube your-tube
Note:
When you flush a tube, it will be remove from the beanstalkd server.
$ php app/console leezy:pheanstalk:list-tube
Note:
Tubes that are display contains at least one job.
$ php app/console leezy:pheanstalk:pause-tube your-tube
$ php app/console leezy:pheanstalk:peek 42
$ php app/console leezy:pheanstalk:put your-tube "Hello world - I am a job"
$ php app/console leezy:pheanstalk:stats
$ php app/console leezy:pheanstalk:stats-job 42
$ php app/console leezy:pheanstalk:stats-tube your-tube
Installation is a quick 4 step process:
- Download LeezyPheanstalkBundle
- Configure the Autoloader
- Enable the Bundle
- Configure your application's config.yml
Ultimately, the LeezyPheanstalkBundle files should be downloaded to the
vendor/bundles/Leezy/PheanstalkBundle
directory.
This can be done in several ways, depending on your preference. The first method is the standard Symfony2 method.
Using the vendors script
Add the following lines in your deps
file:
[LeezyPheanstalkBundle]
git=git://github.com/armetiz/LeezyPheanstalkBundle
target=bundles/Leezy/PheanstalkBundle
[Pheanstalk]
git=https://github.com/mrpoundsign/pheanstalk.git
target=/pheanstalk
Now, run the vendors script to download the bundle:
$ php bin/vendors install
Add the Leezy
and Pheanstalk
namespaces to your autoloader:
<?php
// app/autoload.php
$loader->registerNamespaces(array(
// ...
'Pheanstalk' => __DIR__.'/../vendor/pheanstalk/classes',
'Leezy' => __DIR__.'/../vendor/bundles',
));
Enable the bundle in the kernel:
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Leezy\PheanstalkBundle\LeezyPheanstalkBundle(),
);
}
Finally, add the following to your config.yml
# app/config/config.yml
leezy_pheanstalk:
enabled: true
connection:
primary:
server: beanstalkd.domain.tld
port: 11300
timeout: 60
secondary:
server: beanstalkd-2.domain.tld
default: true
This bundle can be configured, and this is the list of what you can do :
- Create many connection. Note that each connection is a Pheanstalk instance.
- Define specific server / host for each connection.
- Define specific port for each connection. This option is optional and default value is 11300.
- Define specific timeout for each connection. Timeout refere to the connection timeout. This option is optional and default value is 60.
- Disable this bundle. This options is optional and default value is true.
Note:
You can retreive each connection using the container with "leezy.pheanstalk.[connection_name]".
When you define a "default" connection. You can have a direct access to it with "leezy.pheanstalk".
<?php
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HomeController extends Controller {
public function indexAction() {
$pheanstalkPrimary = $this->get("leezy.pheanstalk.primary");
$pheanstalkSecondary = $this->get("leezy.pheanstalk");
// ----------------------------------------
// producer (queues jobs) on beanstalk.domain.tld
$pheanstalkDefault
->useTube('testtube')
->put("job payload goes here\n");
// ----------------------------------------
// worker (performs jobs) on beanstalk-2.domain.tld
$job = $pheanstalkSecondary
->watch('testtube')
->ignore('default')
->reserve();
echo $job->getData();
$pheanstalkSecondary->delete($job);
}
}
?>