We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to catch Beanstalk connection errors if queue is not running

Hi there, I keep getting the following error when I try and test whether the queue is alive or not:

Warning: fsockopen() [function.fsockopen]: unable to connect to 127.0.0.1:11300 (Connection refused) 

Is it possible to elegantly catch this error? I've tried try/catch blocks but they don't seem to work. Any suggestions well received!

Great framework by the way, loving working with it at the moment :)



11.3k

I'm testing the connection by doing $this->queue->connect(); is there a better way?



11.3k
edited Aug '14

Ah, I had this:

        } catch (Exception $e) {

Changing it to this worked:

        } catch (\Exception $e) {

Is there a standard way to avoid having to use the leading slash?



98.9k

PHP prepends the current namespace to every class that has not been renamed in your script:

<?php

namespace MySpace;

try {

    //...

} catch (Exception $e) { // catches MySpace\Exception
    //...   
} catch (\Exception $e) { // catches Exception
    //...
}


11.3k

so if I create Exception.php

<?php
namespace MySpace;

class Exception extends \Exception {

}

Will this catch both? (I've seen this approach used in the Vokuro sample application)

Sorry if this is a basic PHP question, I'm not used to working with namespaces.

Cheers :)



98.9k

Yes, since all exceptions in PHP inherit from \Exception just catching \Exception will catch both.

@Phalcon , What is the reason to keep receive this error ? How to solve it ? Thanks



98.9k

@paanblogger are you getting exceptions or warnings?

@Phalcon , Keep getting error "Warning: fsockopen() [function.fsockopen]: unable to connect to 127.0.0.1:11300 (Connection refused)"



98.9k
edited Aug '14

Warnings cannot be caught unless you implement an error handler or disable them via error_reporting error_reporting(E_ALL & ~E_WARNING);

Thanks @Phalcon , I am not looking for solution to caught this error. I am looking for solution for this error.

I think , the problem is on my server setting.It related to open/close port.



98.9k

I think the port is closed or PHP cannot open the port, have you tried check if the port is open?

telnet 127.0.0.1 11300
edited Nov '14

dear @Phalcon and de-coy you most at the first install --- on server then start connection , so you can use this installation : Install beanstalkd:

Mac OS

brew install beanstalkd
beanstalkd -p 11300

Ubuntu

apt-get install beanstalkd
beanstalkd -p 11300

Install beaneater as a gem:

gem install beaneater

or add this to your Gemfile:

# Gemfile
gem 'beaneater'

and run

bundle install 

to install the dependency.