amphp / beanstalk Goto Github PK
View Code? Open in Web Editor NEWAsynchronous Beanstalk Client for PHP.
Home Page: https://amphp.org/beanstalk/
License: MIT License
Asynchronous Beanstalk Client for PHP.
Home Page: https://amphp.org/beanstalk/
License: MIT License
Mostly stats commands are missing I think. listTubes()
is missing.
I'm trying to subclass the client to add my own method (statsJob to get stats) but I cannot call $this->send from a subclass because it's private and not protected.
Hi
I get this error: "PHP Fatal error: The "yield" expression can only be used inside a function" when I run this code:
// Autoload classes
require_once realpath(__DIR__) . '/vendor/autoload.php';
$beanstalk = new Amp\Beanstalk\BeanstalkClient("tcp://127.0.0.1:11300");
// This step not required if you included a tube query parameter when creating the client
$beanstalk->use('testtube');
while([$jobId, $jobData] = yield $beanstalk->reserve()) {
print_r($jobId);
}
Steps to reproduce:
Which yields the aforementioned error.
Setup:
Debian Jessie
php -v
PHP 7.2.3
uname -a
4.9.0-6-amd64
What is the problem here?
Here:
beanstalk/src/BeanstalkClient.php
Line 192 in a5128a0
The return type is int
but the promise resolves with a bool
value. Should be fixed but it would be a breaking change if someone is relying on a strict type check.
Hi,
I think that the send
callbacks of the following BeanstalkClient
methods have the wrong return type:
\Amp\Beanstalk\BeanstalkClient::delete
: return type hint is int
but it returns bool
(anyway this doesn't raise an error)\Amp\Beanstalk\BeanstalkClient::bury
: return type hint is int
but it returns bool
(anyway this doesn't raise an error)\Amp\Beanstalk\BeanstalkClient::touch
: return type hint is int
but it returns bool
(anyway this doesn't raise an error)\Amp\Beanstalk\BeanstalkClient::release
: return type hint is int
but it returns string
(this raises a fatal error)What do you think?
It seems that for jobs with a big payload (for example a 64k payload) the reserve
function doesn't return the entire payload which was put. Given the following script:
<?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
\Amp\Loop::run(function () {
$client = new Amp\Beanstalk\BeanstalkClient("tcp://127.0.0.1:11300");
yield $client->put(str_repeat('*', 65535));
yield $client->watch('default');
list($jobId, $job) = yield $client->reserve();
echo "Job $jobId, length " . strlen($job) . PHP_EOL;
// yield $client->bury($jobId);
$client->quit();
\Amp\Loop::stop();
});
The output is:
Job 1, length 65518
But it should be:
Job 1, length 65535
It seems a weird buffering issue because if you try to uncomment the bury
function call the output become:
Job 1, length 65518
PHP Fatal error: Uncaught Amp\Beanstalk\BeanstalkException: Unknown response: ***************** in /tmp/vendor/amphp/beanstalk/src/BeanstalkClient.php:203
Stack trace:
#0 /tmp/vendor/amphp/beanstalk/src/BeanstalkClient.php(74): Amp\Beanstalk\BeanstalkClient->Amp\Beanstalk\{closure}(Array)
#1 [internal function]: Amp\Beanstalk\BeanstalkClient->Amp\Beanstalk\{closure}()
#2 /tmp/vendor/amphp/amp/lib/Coroutine.php(76): Generator->send(Array)
#3 /tmp/vendor/amphp/amp/lib/Internal/Placeholder.php(130): Amp\Coroutine->Amp\{closure}(NULL, Array)
#4 /tmp/vendor/amphp/amp/lib/Deferred.php(45): class@anonymous->resolve(Array)
#5 /tmp/vendor/amphp/beanstalk/src/BeanstalkClient.php(38): Amp\Deferred->resolve(Array)
#6 /tmp/vendor/amphp/beanstalk/src/Connection.php(44): Amp\Beanstalk\BeanstalkClient->Amp\Beanstal in /tmp/vendor/amphp/beanstalk/src/BeanstalkClient.php on line 203
I think this happens because the remaining part of the job payload is then treated as the response of the bury
command.
I pushed the reserved-not-as-put-fix
branch with a failing test about this issue.
Methods to fix:
\Amp\Beanstalk\BeanstalkClient::delete
: return type hint is int
but it returns bool
(anyway this doesn't raise an error)\Amp\Beanstalk\BeanstalkClient::bury
: return type hint is int
but it returns bool
(anyway this doesn't raise an error)\Amp\Beanstalk\BeanstalkClient::touch
: return type hint is int
but it returns bool
(anyway this doesn't raise an error)See #11.
Hi,
I use the AMP's beanstalk client with https://github.com/webgriffe/esb.
Sometimes (but I never found the cause) the ESB application crashes with the following error:
PHP Fatal error: Uncaught Error: Call to a member function resolve() on null in vendor/amphp/beanstalk/src/BeanstalkClient.php:38
That resolve
is the one inside the response
event callback of the client:
$this->connection->addEventHandler("response", function ($response) {
/** @var Deferred $deferred */
$deferred = array_shift($this->deferreds);
if ($response instanceof Throwable) {
$deferred->fail($response);
} else {
$deferred->resolve($response);
}
});
And this is strange because only Deferred
instances are added to the BeanstalkClient::$deferreds
array.
beanstalk/src/BeanstalkClient.php
Line 55 in 9973d81
beanstalk/src/BeanstalkClient.php
Line 68 in 9973d81
So it seems that, sometimes, the response
event is fired when the BeanstalkClient::$deferreds
array is empty. Indeed, array_shift
returns null
if the array is empty (http://php.net/manual/en/function.array-shift.php).
I don't know how this could happen but happens...
Maybe we can prevent such error with a simple check:
$this->connection->addEventHandler("response", function ($response) {
if (empty($this->deferreds)) {
return;
}
/** @var Deferred $deferred */
$deferred = array_shift($this->deferreds);
if ($response instanceof Throwable) {
$deferred->fail($response);
} else {
$deferred->resolve($response);
}
});
But I don't know if the right thing to do.
@kelunik what do you think?
Hello. First of all thank you for your work.
We faced a problem that happens when beanstalk close connection after daemon reboot for example. When connection is closed reserve promise does not received any answer and stuck.
There's already a stub available in ./docs
. The README should contain a small example. All further docs should be in ./docs
, so they're available on the website.
There should be more unit and integration tests.
Hi
I use this code to fetch jobs:
// Autoload classes
require_once realpath(__DIR__) . '/vendor/autoload.php';
$client = new Amp\Beanstalk\BeanstalkClient('tcp://127.0.0.1:11300');
$client->watch('testtube');
$client->ignore('default');
Amp\Loop::run(function() use ($client)
{
Amp\Loop::repeat($msInterval = 1, function() use ($client)
{
$client->reserve(0)->onResolve(function($error, $result) use ($client)
{
$id = (int) $result['0'];
if ($id <= 0)
return;
echo "Handle job: " . $id . "\n";
$client->delete($id)->onResolve(function($error, $result) use ($client, $id)
{
echo "Delete job: " . $id . "\n";
});
});
});
});
I use stunnel4 (guide: https://www.digitalocean.com/community/tutorials/how-to-encrypt-traffic-to-redis-with-stunnel-on-ubuntu-16-04) to create a tunnel between machine1 (running Beanstalk) and machine2 (consumer).
When using stunnel4 it will keep the port: 11300 on 127.0.0.1 open all the time, even though the remote port has closed due to either restart or the process stops.
However amphp/beanstalk doesn't timeout the connection as it sees the connection as still alive because the local port doesn't close - so the stream doesn't close.
If I add $stats = yield $client->getSystemStats();
right after repeat, it will close the connection and fail with: PHP Fatal error: Uncaught Amp\ByteStream\StreamException: The stream was closed by the peer
.
Is it however not that great to continuously send a command to request data, because it will waste ressources, and may slow down reserving jobs.
TLDR: amphp/beanstalk doesn't recognize stream closed when reserving.
As the title says, there are methods for either kicking jobs up to N boundary, or kicking a job by id. Typically would be used to kick buried jobs back into the ready portion of a tube.
We should have a few examples, a producer and a consumer at least.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.