xobotyi / beansclient

Robust PHP client for beanstalkd work queue
MIT License
91 stars 10 forks source link

Reserving a job from an empty queue throws an "Unknown Error" #14

Open advitum opened 5 years ago

advitum commented 5 years ago

This is a bug report.

When reserving a job using $client->reserve();, an unknown error gets thrown when the queue is empty.

According to the documentation (and the source code), an empty queue should not throw an error, but instead result in a Job with id=null.

$client = new BeansClient(new Connection());
$client->watchTube('Project.Orders');
$job = $client->reserve(30);

// Exception: Unknown error in [/xobotyi/beansclient/src/Socket/SocketBase.php, line 158]
Stack Trace:
#0 /vendor/xobotyi/beansclient/src/Socket/SocketBase.php(175): xobotyi\beansclient\Socket\SocketBase->throwLastError()
#1 /vendor/xobotyi/beansclient/src/Connection.php(120): xobotyi\beansclient\Socket\SocketBase->readLine()
#2 /vendor/xobotyi/beansclient/src/BeansClient.php(76): xobotyi\beansclient\Connection->readLine()
#3 /vendor/xobotyi/beansclient/src/BeansClient.php(317): xobotyi\beansclient\BeansClient->dispatchCommand(Object(xobotyi\beansclient\Command\Reserve))
#4 /src/Command/OrdersCommand.php(30): xobotyi\beansclient\BeansClient->reserve(30)
[...]

Versions i'm using:

xobotyi commented 5 years ago

Try an alpha version, this problem should be solved there.

splastunov commented 5 years ago

Hello!

I've got the same error on alpha...

xobotyi commented 5 years ago

@splastunov what is your use case?

splastunov commented 5 years ago
$beansConnection = new xobotyi\beansclient\Connection('127.0.0.1', 11300, 2, true);
$beansClient = new xobotyi\beansclient\BeansClient($beansConnection);

//Watch tube and get task
$job = $beansClient->watchTube('myTube')->reserve();

Failed with the same Socket error if Tube is empty.

Even if you would try to watch empty Tube with watchTube it will fail.

Allsow you have errors at documentation. listTubeUsed should be listUsedTubes and etc...

xobotyi commented 5 years ago

Due to it is alpha it's not documented yet.

Punkley commented 4 years ago

Also seeing this issue, from what I can tell, the readline isn't waiting or is failing because its not returning anything straight away.

It happens when you do a reserve-with-timeout

Punkley commented 4 years ago

OK I have worked out what the bug is

$result = fgets($this->socket, 8192); in Function ReadLine means the wait for the fgets is very short eg 1 sec.

If you are using reserve-with-timeout say set to 60, you need readline to wait for at least 60 seconds

eg with something like stream_set_timeout($this->socket, 65);

xobotyi commented 4 years ago

@Punkley have you checked that? If that solves the issue ill dig into it in that direction

Punkley commented 4 years ago

Yep, it worksOn 28 Oct 2019 8:26 am, Anton Zinovyev notifications@github.com wrote:@Punkley have you checked that? If that solves the issue ill dig into it in that direction

—You are receiving this because you were mentioned.Reply to this email directly, view it on GitHub, or unsubscribe.

xobotyi commented 4 years ago

@Punkley check out the master branch. Pushed the fix, not tested yet, but it should help.

Punkley commented 4 years ago

so far, so good :)

Punkley commented 4 years ago

OK I think a slight tweak is needed,

Every now and then about every 10th time I get PHP Fatal error: Uncaught xobotyi\beansclient\Exception\SocketException: Unknown error in vendor/xobotyi/beansclient/src/Socket/SocketBase.php:185

I think I have however solved the issue by adding this $timeout !== null && stream_set_timeout($this->socket, $timeout +5);

eg the timeout needs to be just a little longer then the actual beanstalk timeout.

Punkley commented 4 years ago

after making that change, the script has been running for 12 hours+

Kingsley

xobotyi commented 4 years ago

@Punkley 5 seconds is pretty much, have you tried smaller values?

xobotyi commented 4 years ago

@Punkley hello again. Ive attempted to improve the sockets interconnection, check out the v2.0.0-alpha.13 please.