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 :)



7.9k

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



7.9k
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?



83.4k

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
    //...
}


7.9k

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 :)



83.4k

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



83.4k

@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)"



83.4k
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.



83.4k

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.