Replaced EventAction with Procedure to allow more flexibility in event hooks and cronjobs

This commit is contained in:
Fabio Manganiello 2018-09-27 01:52:35 +02:00
parent f3b1bdb397
commit 570ef6f109
3 changed files with 12 additions and 19 deletions

View file

@ -5,7 +5,7 @@ import time
from threading import Thread from threading import Thread
from platypush.event.hook import EventAction from platypush.procedure import Procedure
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -15,19 +15,15 @@ class Cronjob(Thread):
super().__init__() super().__init__()
self.cron_expression = cron_expression self.cron_expression = cron_expression
self.name = name self.name = name
self.actions = [] self.actions = Procedure.build(name=name+'__Cron', _async=False,
requests=actions)
for action in actions:
self.actions.append(EventAction.build(action))
def run(self): def run(self):
logger.info('Running cronjob {}'.format(self.name)) logger.info('Running cronjob {}'.format(self.name))
response = None response = None
context = {} context = {}
response = self.actions.execute(_async=False, **context)
for action in self.actions:
response = action.execute(_async=False, **context)
logger.info('Response from cronjob {}: {}'.format(self.name, response)) logger.info('Response from cronjob {}: {}'.format(self.name, response))

View file

@ -6,6 +6,7 @@ import re
from platypush.config import Config from platypush.config import Config
from platypush.message.event import Event, EventMatchResult from platypush.message.event import Event, EventMatchResult
from platypush.message.request import Request from platypush.message.request import Request
from platypush.procedure import Procedure
from platypush.utils import get_event_class_by_type from platypush.utils import get_event_class_by_type
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -99,8 +100,8 @@ class EventHook(object):
one or multiple actions to be executed """ one or multiple actions to be executed """
def __init__(self, name, priority=None, condition=None, actions=[]): def __init__(self, name, priority=None, condition=None, actions=[]):
""" Construtor. Takes a name, a EventCondition object and a list of """ Construtor. Takes a name, a EventCondition object and an event action
EventAction objects as input. It may also have a priority attached procedure as input. It may also have a priority attached
as a positive number. If multiple hooks match against an event, as a positive number. If multiple hooks match against an event,
only the ones that have either the maximum match score or the only the ones that have either the maximum match score or the
maximum pre-configured priority will be run. """ maximum pre-configured priority will be run. """
@ -132,6 +133,7 @@ class EventHook(object):
else: else:
actions = [hook['then']] actions = [hook['then']]
actions = Procedure.build(name=name+'__Hook', requests=actions, _async=False)
return cls(name=name, condition=condition, actions=actions, priority=priority) return cls(name=name, condition=condition, actions=actions, priority=priority)
@ -151,13 +153,8 @@ class EventHook(object):
if result.is_match: if result.is_match:
logger.info('Running hook {} triggered by an event'.format(self.name)) logger.info('Running hook {} triggered by an event'.format(self.name))
# TODO here event should actually be event.args
for action in self.actions: self.actions.execute(event=event, **result.parsed_args)
a = EventAction.build(action)
if token:
a.token = token
a.execute(event=event, **result.parsed_args)
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et:

View file

@ -2,7 +2,7 @@ from .context import platypush
import unittest import unittest
from platypush.event.hook import EventHook, EventCondition, EventAction from platypush.event.hook import EventCondition
from platypush.message.event.ping import PingEvent from platypush.message.event.ping import PingEvent
class TestEventParse(unittest.TestCase): class TestEventParse(unittest.TestCase):