GithubHelp home page GithubHelp logo

mariuswilms / beanstalk Goto Github PK

View Code? Open in Web Editor NEW
200.0 22.0 59.0 63 KB

Minimalistic PHP client for beanstalkd without any dependencies

License: MIT License

PHP 100.00%
beanstalkd client library php

beanstalk's Introduction

BEANSTALK
---- Minimalistic PHP client for beanstalkd.

Synopsis
--------
This library allows you to interface with the beanstalkd[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/Client.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.

Cute[3] is such a project. It uses this library to provide convenient
access and tooling around the work queue for PHP.

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

Copyright & License
-------------------
Beanstalk, a beanstalk PHP client library for the beanstalkd work queue is
Copyright (c) 2009-2015 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
2.0.0, PHP >=5.4.1

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

Installation
------------
The preferred installation method is via composer. You can add the library
as a dependency via:

$ composer require davidpersson/beanstalk

Usage
-----
<?php

use Beanstalk\Client;

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

$beanstalk->connect();
$beanstalk->useTube('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 Client();

$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:
    // ['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
composer install

export TEST_BEANSTALKD_HOST=127.0.0.1
export TEST_BEANSTALKD_PORT=11301
vendor/bin/phpunit tests
```

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

beanstalk's People

Contributors

d1rk avatar eryx avatar mariuswilms avatar miped avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

beanstalk's Issues

Wrong port in doc

Hey! Thanks a lot for the lib, very appreciated :-) Small hint, the README contains Port 11301 as default port, but is 11300 in the config.

README consumer example is incorrect

In the example code for a consumer on Line 75 of the README, it reads $beanstalk->choose('flux'); It should read $beanstalk->watch('flux'); I was unable to make the example program work until I made that change.

delete() is not working...

Hi,
I´m trying to use this class for Beanstalk.
Almost everything is ok, but ->delete($jobid) is not working. The job still appear on tube. I´m using newer versions of beanstalkd and php 7.2

Max payload size capped by chunk size

When loading large payloads into beanstalk, I noticed my jobs were getting cropped much below beanstalkd's maximum size when I reserved them. I think the issue has to do with fread capped by chunk size as mentioned in the docs at http://php.net/manual/en/function.fread.php.

I solved this problem by replacing

$data = fread($this->_connection, $length + 2);

with

$data = stream_get_contents($this->_connection, $length + 2);

inside _read()

-Matt

NOT_FOUND happens

Hi there, i'am actively using the library in production for more than a year, and while pushing the "limits" of how far i can go with it i now got an small issue.

the

\Beanstalk\Client . _error

produces error log message "NOT_FOUND"

            [line] => 147
            [function] => error
            [class] => BackQ\Adapter\Beanstalk
            [type] => ->
            [args] => Array
                (
                    [0] => NOT_FOUND
                )

        )

I have my logger attached with simple error_log() wrapping.

Back to the issue. My call chain looks like:

$client = \Beanstalk\Client($config);
...
$client->connect();
...
$client->useTube('tubename1'); //result is 'USING'
$client->statsTube('tubename1); //result is 'NOT_FOUND'

// \Beanstalk\Client::statsTube
// \Beanstalk\Client::_statsRead
// \Beanstalk\Client::_error('NOT_FOUND')

I did read the Beanstalk protocol spec and i'am aware that:

Tubes are created on demand whenever they are referenced. If a tube is empty
(that is, it contains no ready, delayed, or buried jobs) and no client refers
to it, it will be deleted.

So i assume some worker took the last job from tube somewhere between "useTube" and "statsTube" and that causes the NOT_FOUND when the client is using tube that does not exists.

  1. What exactly means "no client refers to it"
  2. Is there a way to call stats on tube without triggering error (except ignoring the error itself) ?

Thx, greetings from Berlin

Each write is a disconnect.

So essentially in the _write function you check for a connection by invoking the connect function which in turn proceeds to reconnect(disconnect if a connection is present) you each call, isn't that bad?

Not recommended for production!

Hello! Please make note "Not recommended for production!" for you library.

It is unstable, loses connection but does not understand this and continues to pretend that everything is ok. This leads to big problems!

Broken in PHP 5.3.9, 5.3.10

It looks like this bug https://bugs.php.net/bug.php?id=60817 causes problems for _read. May I suggest the following workaround:

diff --git a/3rd_party/beanstalk/src/Socket/Beanstalk.php b/3rd_party/beanstalk/src/Socket/Beanstalk.php
index 577e949..26597ad 100644
--- a/3rd_party/beanstalk/src/Socket/Beanstalk.php
+++ b/3rd_party/beanstalk/src/Socket/Beanstalk.php
@@ -187,7 +187,8 @@ class Socket_Beanstalk {
                        }
                        $packet = rtrim($data, "\r\n");
                } else {
-                       $packet = stream_get_line($this->_connection, 16384, "\r\n");
+                       $packet = fgets($this->_connection, 16384);
+                       $packet = rtrim($packet, "\r\n");
                }
                return $packet;
        }
@@ -624,4 +625,4 @@ class Socket_Beanstalk {
        }
 }

-?>
\ No newline at end of file
+?>

function _write() issues

Notice: fwrite(): send of 6 bytes failed with errno=32 Broken pipe in ./Client.php on line 181

$data .= "\r\n";

change to :

$data .= '\r\n';

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.