Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

My Beanstalkd queue just slowed down

Well i decided to put all log action into queue, so i have something like this on some event dispatcher:

$message=PHP_EOL .
                'URL: ' . $di->get('request')->getURI() . PHP_EOL .
                'Module name:' . $dispatcher->getModuleName() . PHP_EOL .
                'Controller name:' . $dispatcher->getControllerName() . PHP_EOL .
                'Action name:' . $dispatcher->getActionName() . PHP_EOL .
                'Params:' . json_encode($dispatcher->getParams()) . PHP_EOL .
                'POST:' . json_encode($post) . PHP_EOL .
                'JSON:' . json_encode($di->get('request')->getJsonRawBody(true)) . PHP_EOL .
                'User id:' . $di->get('session')->get('admin') . PHP_EOL .
                '=============================================================';
            if(PHP_OS == "Linux"){
                /** @var Beanstalk $queue */
                $queue->choose('logger');
                $queue->put(array(
                    'type'=>'beforeDispatch',
                    'message'=>$message
                ));
            }
            else {
                $logger->info($message);
            }

For some time it was working fine, but after reboot of vps it really slowed down, when using this code i have like +300ms on every request.

What's going on ? I don't have anything on php logs(or don't know where to look).

Try to debug which line is doing that 300 ms delay.

edited Apr '16

Exactly this:

$queue->choose('logger');
$queue->put(array(
    'type'=>'beforeDispatch',
    'message'=>$message
));

And choose which line exactly choose or put or both? Seems like problem in your beanstalk, are you using persistent connection?

edited Apr '16

Put causing a problem, i tried both persistant and not connection.

I tested it's not beanstalkd problem with this class https://github.com/davidpersson/beanstalk and it's working much much faster, like putting 4 jobs in 0.0006 s instead of 0.16 s

I created issue here: https://github.com/phalcon/cphalcon/issues/11643



49.7k
Accepted
answer
edited Apr '16

I had similar issues with Beanstalk server and Phalcon. The thing is you NEED to call connect() method each time before trying to talk to the beanstalkd.

https://docs.phalconphp.com/en/latest/api/Phalcon_Queue_Beanstalk.html

public connect () Makes a connection to the Beanstalkd server

After doing this, communication is fast as expected.

edited Apr '16

It is supposed to be fixed in 2.1.x branch. But i will check your solution on 2.0.x version, beacause on production server currently 2.0.x is working.

edited Apr '16

Yeah, I tested it extensively on 2.0.10 production version, in other words I lost entire day debugging why only on first occurence it does work fast but each next time it slows down by magnitude of 10. connect() was the key.