Support for custom message handlers on the listener

This commit is contained in:
Fabio Manganiello 2017-12-20 22:12:32 +01:00
parent bd5c80175f
commit 7e79fa0418

View file

@ -36,10 +36,13 @@ class Daemon(object):
""" number of executions retries before a request fails """ """ number of executions retries before a request fails """
n_tries = 2 n_tries = 2
def __init__(self, config_file=None): def __init__(self, config_file=None, message_handler=None):
""" Constructor """ Constructor
Params: Params:
config_file -- Configuration file override (default: None) config_file -- Configuration file override (default: None)
message_handler -- Another function that will receive the messages.
If set, Platypush will just act as a message proxy. Useful to
embed into other projects, for tests, or for delegating events.
""" """
self.config_file = config_file self.config_file = config_file
@ -66,6 +69,12 @@ class Daemon(object):
""" on_message closure """ on_message closure
Params: Params:
msg -- platypush.message.Message instance """ msg -- platypush.message.Message instance """
if self.message_handler:
# Proxy the message
self.message_handler(msg)
return
if isinstance(msg, Request): if isinstance(msg, Request):
logging.info('Processing request: {}'.format(msg)) logging.info('Processing request: {}'.format(msg))
Thread(target=self.run_request(), args=(msg,)).start() Thread(target=self.run_request(), args=(msg,)).start()