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
1 changed files with 10 additions and 1 deletions

View File

@ -36,10 +36,13 @@ class Daemon(object):
""" number of executions retries before a request fails """
n_tries = 2
def __init__(self, config_file=None):
def __init__(self, config_file=None, message_handler=None):
""" Constructor
Params:
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
@ -66,6 +69,12 @@ class Daemon(object):
""" on_message closure
Params:
msg -- platypush.message.Message instance """
if self.message_handler:
# Proxy the message
self.message_handler(msg)
return
if isinstance(msg, Request):
logging.info('Processing request: {}'.format(msg))
Thread(target=self.run_request(), args=(msg,)).start()