Cannot trap exceptions from Gearman Client instantiation

566 views Asked by At

When connecting to a gearman daemon, if the daemon url or port are incorrect and no connection can be made an exception is raised:

      File "/usr/lib/python2.7/dist-packages/gearman/client.py", line 205, in establish_request_connection
    raise ServerUnavailable('Found no valid connections: %r' % self.connection_list)

gearman.errors.ServerUnavailable: Found no valid connections: [<GearmanConnection localhost:4700 connected=False>]

I want to catch the exception and handle it gracefully but the code below doesn't do that. The exception and traceback is displayed as though I haven't tried to catch the exception.

The code that generates and tries to trap the exception is:

import gearman
from gearman.errors import ConnectionError, InvalidAdminClientState, ServerUnavailable
try:
    gmClient = gearman.GearmanClient(['localhost:4730'])
except gearman.errors.ServerUnavailable, e:
# I've also tried except ServerUnavailable, e: - same result.
    print(e)

How do I correctly catch gearman client connection exceptions?

1

There are 1 answers

1
Colin Pickard On

The ServerUnavailable exception is raised when you try to submit a job. Try this:

import gearman

gmClient = gearman.GearmanClient(['localhost:4700'])

try:
    request = gmClient.submit_job('reverse', 'Hello World!')
except gearman.errors.ServerUnavailable, e:
    print("caught ServerUnavailable")