doctorfox / beanstalk

Minimalistic PHP client for beanstalkd.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BEANSTALK
---- Minimalistic PHP client for beanstalk.

Synopsis
--------
This library allows you to interface with the beanstalk[1] work queue and is
built with a minimal featureset still supporting the complete protocol. The
main (and currently only) class can be found at `src/Socket/Beanstalk.php`.

The class was formerly part of the queue plugin for CakePHP[2]. It has been
extracted for higher reusability in other places and frameworks.

[1] http://kr.github.com/beanstalkd
[2] https://github.com/davidpersson/queue

Copyright & License
-------------------
Beanstalk, a beanstalk PHP client library for the beanstalk work queue is
Copyright (c) 2009-2013 David Persson if not otherwise stated. The code
is distributed under the terms of the MIT License. For the full license
text see the LICENSE file.

Versions & Requirements
-----------------------
1.0.0, PHP >=5.2.1
1.1.0, PHP >=5.2.1 (in progress)

Note: In PHP Versions 5.3.9 and 5.3.10 the `stream_get_line()`
      function exhibits buggy behavior (PHP bug #60817), thus
      these versions cannot be used with this library.

Installation
------------
Clone the repository or extract a downloaded archive, then move
or copy the resulting directory into /path/to/your/libraries/.

To enable loading of the classes contained in the library, put
the `src` directory of the library in your include path.
<?php
// ...

ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '/path/to/beanstalk/src');

// ...
?>

Usage
-----
<?php

require_once 'Socket/Beanstalk.php';

//
// A sample producer.
//
$beanstalk = new Socket_Beanstalk(); // For connection options see the class documentation.

$beanstalk->connect();
$beanstalk->choose('flux'); // Begin to use tube `'flux'`.
$beanstalk->put(
    23, // Give the job a priority of 23.
    0,  // Do not wait to put job into the ready queue.
    60, // Give the job 1 minute to run.
    '/path/to/cat-image.png' // The job's body.
);
$beanstalk->disconnect();

//
// A sample consumer.
//
$beanstalk = new Socket_Beanstalk();

$beanstalk->connect();
$beanstalk->watch('flux');

while (true) {
    $job = $beanstalk->reserve(); // Block until job is available.
    // Now $job is an array which contains its ID and body:
    // array('id' => 123, 'body' => '/path/to/cat-image.png')

    // Processing of the job...
    $result = touch($job['body']);

    if ($result) {
        $beanstalk->delete($job['id']);
    } else {
        $beanstalk->bury($job['id']);
    }
}
// When exiting i.e. on critical error conditions
// you may also want to disconnect the consumer.
// $beanstalk->disconnect();

?>

Running the Tests
-----------------
The integration tests contained in this library require a running beanstalkd
instance. You'll need to start a dedicated instance on port 11301 as follows.

$ beanstalkd -VV -l 127.0.0.1 -p 11301

Tests for this library are PHPUnit based. To run the tests you'll need
to have PHPUnit installed[1]. Following commands will run all the tests.

$ cd /path/to/beanstalk/src
$ phpunit -c ../phpunit.xml

[1] http://www.phpunit.de/manual/current/en/installation.html

About

Minimalistic PHP client for beanstalkd.

License:MIT License