3 from __future__ import absolute_import, print_function
10 from .config import actor_class
12 def _notify_subscribers(response, subscribers):
13 """Send the response to all the subscriber methods.
15 If any of the subscriber actors have stopped, remove them from the
18 dead_subscribers = set()
19 for subscriber in subscribers:
22 except pykka.ActorDeadError:
23 dead_subscribers.add(subscriber)
24 subscribers.difference_update(dead_subscribers)
26 class RemotePollLoopActor(actor_class):
27 """Abstract actor class to regularly poll a remote service.
29 This actor sends regular requests to a remote service, and sends each
30 response to subscribers. It takes care of error handling, and retrying
31 requests with exponential backoff.
33 To use this actor, define CLIENT_ERRORS and the _send_request method.
34 If you also define an _item_key method, this class will support
35 subscribing to a specific item by key in responses.
37 def __init__(self, client, timer_actor, poll_wait=60, max_poll_wait=180):
38 super(RemotePollLoopActor, self).__init__()
40 self._timer = timer_actor
41 self._logger = logging.getLogger(self.LOGGER_NAME)
42 self._later = self.actor_ref.proxy()
43 self.min_poll_wait = poll_wait
44 self.max_poll_wait = max_poll_wait
45 self.poll_wait = self.min_poll_wait
46 self.last_poll_time = None
47 self.all_subscribers = set()
48 self.key_subscribers = {}
49 if hasattr(self, '_item_key'):
50 self.subscribe_to = self._subscribe_to
52 def _start_polling(self):
53 if self.last_poll_time is None:
54 self.last_poll_time = time.time()
57 def subscribe(self, subscriber):
58 self.all_subscribers.add(subscriber)
59 self._logger.debug("%r subscribed to all events", subscriber)
62 # __init__ exposes this method to the proxy if the subclass defines
64 def _subscribe_to(self, key, subscriber):
65 self.key_subscribers.setdefault(key, set()).add(subscriber)
66 self._logger.debug("%r subscribed to events for '%s'", subscriber, key)
69 def _send_request(self):
70 raise NotImplementedError("subclasses must implement request method")
72 def _got_response(self, response):
73 self.poll_wait = self.min_poll_wait
74 _notify_subscribers(response, self.all_subscribers)
75 if hasattr(self, '_item_key'):
76 items = {self._item_key(x): x for x in response}
77 for key, subscribers in self.key_subscribers.iteritems():
78 _notify_subscribers(items.get(key), subscribers)
80 def _got_error(self, error):
81 self.poll_wait = min(self.poll_wait * 2, self.max_poll_wait)
82 self._logger.warning("Client error: %s - waiting %s seconds",
83 error, self.poll_wait)
86 start_time = time.time()
88 response = self._send_request()
89 except self.CLIENT_ERRORS as error:
90 self.last_poll_time = start_time
91 self._got_error(error)
93 self.last_poll_time += self.poll_wait
94 self._got_response(response)
95 self._timer.schedule(self.last_poll_time + self.poll_wait,