99from simpleflow .process import NamedMixin , with_state
1010from simpleflow .swf .helpers import swf_identity
1111
12-
1312logger = logging .getLogger (__name__ )
1413
15-
1614__all__ = ['Poller' ]
1715
1816
1917class Poller (swf .actors .Actor , NamedMixin ):
2018 """Multi-processing implementation of a SWF actor.
2119
2220 """
21+
2322 def __init__ (self , domain , task_list = None ):
2423 self .is_alive = False
2524 self ._named_mixin_properties = ["task_list" ]
@@ -52,6 +51,7 @@ def bind_signal_handlers(self):
5251 - SIGTERM and SIGINT lead to a graceful shutdown
5352 - other signals are not modified for now
5453 """
54+
5555 # NB: Function is nested to have a reference to *self*.
5656 def _handle_graceful_shutdown (signum , frame ):
5757 logger .info ("process: caught signal signal=SIGTERM pid={}" .format (os .getpid ()))
@@ -74,7 +74,7 @@ def start(self):
7474 self .set_process_name ()
7575 while self .is_alive :
7676 try :
77- response = self ._poll ()
77+ response = self .poll_with_retry ()
7878 except swf .exceptions .PollTimeout :
7979 continue
8080 self .process (response )
@@ -87,7 +87,7 @@ def stop_gracefully(self):
8787 logger .info ('stopping %s' , self .name )
8888 self .is_alive = False # No longer take requests.
8989
90- def _complete (self , token , response ):
90+ def complete_with_retry (self , token , response ):
9191 """
9292 Complete with retry.
9393 :param token:
@@ -97,7 +97,6 @@ def _complete(self, token, response):
9797 :return:
9898 :rtype:
9999 """
100- # FIXME this is a public member
101100 try :
102101 complete = utils .retry .with_delay (
103102 nb_times = self .nb_retries ,
@@ -131,7 +130,7 @@ def name(self):
131130 """
132131 return '{}()' .format (self .__class__ .__name__ )
133132
134- def _poll (self ):
133+ def poll_with_retry (self ):
135134 """
136135 Polls a task represented by its token and data. It uses long-polling
137136 with a timeout of one minute.
@@ -147,19 +146,26 @@ def _poll(self):
147146 identity = self .identity
148147
149148 logger .debug ("polling task on %s" , task_list )
150- try :
151- response = self .poll (
152- task_list ,
153- identity = identity ,
154- )
155- except swf .exceptions .PollTimeout :
156- logger .debug ('{}: PollTimeout' .format (self ))
157- raise
158- except Exception as err :
159- logger .error (
160- "exception %s when polling on %s" ,
161- str (err ),
162- task_list ,
163- )
164- raise
149+ poll = utils .retry .with_delay (
150+ nb_times = self .nb_retries ,
151+ delay = utils .retry .exponential ,
152+ log_with = logger .exception ,
153+ on_exceptions = swf .exceptions .ResponseError ,
154+ )(self .poll )
155+ response = poll (task_list , identity = identity )
156+ return response
157+
158+ @abc .abstractmethod
159+ def fail (self , * args , ** kwargs ):
160+ """fail; only relevant for activity workers."""
161+ raise NotImplementedError
162+
163+ def fail_with_retry (self , * args , ** kwargs ):
164+ fail = utils .retry .with_delay (
165+ nb_times = self .nb_retries ,
166+ delay = utils .retry .exponential ,
167+ log_with = logger .exception ,
168+ on_exceptions = swf .exceptions .ResponseError ,
169+ )(self .fail )
170+ response = fail (* args , ** kwargs )
165171 return response
0 commit comments