| 32 | |
| 33 | |
| 34 | class SensorWatcher(ConsumerMixin): |
| 35 | def __init__( |
| 36 | self, create_handler, update_handler, delete_handler, queue_suffix=None |
| 37 | ): |
| 38 | """ |
| 39 | :param create_handler: Function which is called on SensorDB create event. |
| 40 | :type create_handler: ``callable`` |
| 41 | |
| 42 | :param update_handler: Function which is called on SensorDB update event. |
| 43 | :type update_handler: ``callable`` |
| 44 | |
| 45 | :param delete_handler: Function which is called on SensorDB delete event. |
| 46 | :type delete_handler: ``callable`` |
| 47 | """ |
| 48 | # TODO: Handle sensor type filtering using routing key |
| 49 | self._create_handler = create_handler |
| 50 | self._update_handler = update_handler |
| 51 | self._delete_handler = delete_handler |
| 52 | self._sensor_watcher_q = self._get_queue(queue_suffix) |
| 53 | |
| 54 | self.connection = None |
| 55 | self._updates_thread = None |
| 56 | |
| 57 | self._handlers = { |
| 58 | publishers.CREATE_RK: create_handler, |
| 59 | publishers.UPDATE_RK: update_handler, |
| 60 | publishers.DELETE_RK: delete_handler, |
| 61 | } |
| 62 | |
| 63 | def get_consumers(self, Consumer, channel): |
| 64 | consumers = [ |
| 65 | Consumer( |
| 66 | queues=[self._sensor_watcher_q], |
| 67 | accept=["pickle"], |
| 68 | callbacks=[self.process_task], |
| 69 | ) |
| 70 | ] |
| 71 | return consumers |
| 72 | |
| 73 | def process_task(self, body, message): |
| 74 | LOG.debug("process_task") |
| 75 | LOG.debug(" body: %s", body) |
| 76 | LOG.debug(" message.properties: %s", message.properties) |
| 77 | LOG.debug(" message.delivery_info: %s", message.delivery_info) |
| 78 | |
| 79 | routing_key = message.delivery_info.get("routing_key", "") |
| 80 | handler = self._handlers.get(routing_key, None) |
| 81 | |
| 82 | try: |
| 83 | if not handler: |
| 84 | LOG.info("Skipping message %s as no handler was found.", message) |
| 85 | return |
| 86 | |
| 87 | try: |
| 88 | handler(body) |
| 89 | except Exception as e: |
| 90 | LOG.exception( |
| 91 | "Handling failed. Message body: %s. Exception: %s", |
no outgoing calls