fire_event could also be called by an external web server or service with no access to the bus. Therefore don't fail hard if the bus is not available

This commit is contained in:
Fabio Manganiello 2020-02-24 17:17:36 +01:00
parent 6e60edf2e1
commit 40a29a8214

View file

@ -1,4 +1,5 @@
import inspect import inspect
import logging
import threading import threading
@ -9,6 +10,8 @@ class EventGenerator(object):
types. Both plugins and backends extend this class. types. Both plugins and backends extend this class.
""" """
logger = logging.getLogger(__name__)
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self._event_handlers = {} # Event type => callback map self._event_handlers = {} # Event type => callback map
@ -29,7 +32,11 @@ class EventGenerator(object):
from platypush.context import get_bus from platypush.context import get_bus
bus = self.bus if isinstance(self, Backend) else get_bus() bus = self.bus if isinstance(self, Backend) else get_bus()
bus.post(event) if not bus:
self.logger.warning('No bus available to post the event: {}'.format(event))
else:
bus.post(event)
handlers = set() handlers = set()
for cls in inspect.getmro(event.__class__): for cls in inspect.getmro(event.__class__):