Jacob Sondergaard avatar Jacob Sondergaard committed ed7808e

Auto re-connect if socket is closed. Re-connection attempts are (by default) made with 1 sec. intervals.

Comments (0)

Files changed (3)

 ### Connection methods
 
 **`connect(callback=None)`**  
-Establish the client's connection to beanstalkd. Calls back when connection has been established.
+Establish the client's connection to beanstalkd. Calls back when connection has been established. When first connected, the client will attempt to re-connect if the socket is closed.
 
 **`close(callback=None)`**  
 Close the client's connection to beanstalkd. Calls back when connection has been closed.

beanstalktc/beanstalktc.py

 limitations under the License.
 '''
 
-__version__ = '0.1.0'
+__version__ = '0.2.0'
 
 import socket
+import time
 
 from tornado.ioloop import IOLoop
 from tornado.iostream import IOStream
 
 DEFAULT_PRIORITY = 2 ** 31
 DEFAULT_TTR = 120  # Time (in seconds) To Run a job, min. 1 sec.
+RECONNECT_TIMEOUT = 1  # Time (in seconds) between re-connection attempts
 
 
 class BeanstalktcException(Exception): pass
 class CommandFailed(BeanstalktcException): pass
 class Buried(BeanstalktcException): pass
 class DeadlineSoon(BeanstalktcException): pass
+class NotConnected(BeanstalktcException): pass
 
 
 class Client(object):
         self.host = host
         self.port = port
         self.io_loop = io_loop or IOLoop.instance()
-        self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
-                socket.IPPROTO_TCP)
-        self._stream = IOStream(self._socket, io_loop=self.io_loop)
+        self._stream = None
+
+    def _reconnect(self, callback):
+        self.io_loop.add_timeout(time.time() + RECONNECT_TIMEOUT,
+                lambda: self.connect(callback))
 
     def connect(self, callback=None):
         """Connect to beanstalkd server."""
+        if not self._stream or self._stream.closed():
+            self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
+                    socket.IPPROTO_TCP)
+            self._stream = IOStream(self._socket, io_loop=self.io_loop)
+            self._stream.set_close_callback(lambda: self._reconnect(callback))
         self._stream.connect((self.host, self.port), callback)
 
     def close(self, callback=None):
         return self._stream.closed()
 
     def _interact(self, command, callback, expected_ok=[], expected_err=[]):
+        if self.closed():
+            raise NotConnected('Not connected to the beanstalkd server'
+                    ' - attempting to reconnect')
+
         # write command to socket stream
         self._stream.write(command,
                 # when command is written: read line from socket stream
 from setuptools import setup
 
 setup(name='beanstalktc',
-      version='0.1.0',
+      version='0.2.0',
       description='An async beanstalkd client for Tornado',
       author='Jacob Sondergaard',
       author_email='jacob@nephics.com',
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.