MODULE NAME
reaction_engine
-modules/reaction.py or -modules/reaction.so
[ so is an encrypted version the module ]
DESCRIPTION
The reaction engine is the most intuitive module of the Rayjas ecosystem. It is quite lightweight but still handles some of the most important functions in the system. The reaction engine, as the name, suggests maps data/messages/intents to valuable reactions in real-time.
The reaction engine uses something called rules (reaction rules) to make those mapping decisions. Rules define the trigger conditions and response in detail using JSON. There are usually two types of rules in the reaction system – standard data-driven rules and scheduled time-driven rules.
CONFIGURATION
modules/conf/reaction.json
{ "enabled": true, "klass": "ReactionEngine", "conf" : { "topics_of_interest": ["*"], "events_of_interest": ["*"] } }
CONFIGURATION PROPERTIES
Property | Description | Value Type | Default Value |
enabled | Enable/disable the module | Boolean | True |
klass | The class name for this module | String | ReactionEngine |
conf.topics_of_interest | Topic paths that the reaction engine will take interest in and monitor. | Boolean | True |
conf.events_of_interest | System events that the reaction engine will take interest in and monitor. | Number | 10 |
RULES
STANDARD REACTION RULE (DATA-DRIVEN)
A reaction rule is a simple JSON that defines a rule for triggering an intent to perform an action (internal or external to the system). The reaction engine, on startup, will load all the rules from the filesystem. Each of these rules defines a data channel path via the listen-to
attribute, on which the reaction engine must listen for data. Once the reaction engine receives data, it will use the trigger
attribute of the rule to decide whether the rule is triggered or not. If the rule is triggered, the response
attribute of the rule will dictate what action to perform.
Standard Reaction Rule – Triggers an HTTP call to a defined webhook
{ "id": "rule-http-notify", "description": "Rule for triggering http notification", "listen-to": "/httpcapture/path", "enabled": false, "trigger":{ "on-payload-object": { "key":"data", "on-content": "*", "on-condition": "equals" }, "evaluator": null }, "response":{ "intent": "simple_http_notify", "parameters": { "http":{ "webhook": ["http://localhost/receiver"], "method": "GET" } } } }
The sample rule above is configured to make the reaction engine listen to the channel path – /httpcapture/path
. When any data is received on that path ("on-content": "*"
), the reaction engine responds by firing an intent by name simple_http_notify
and accompanying data for the HTTP notification. The GenericNotifier
module which is interested in this intent will capture the broadcast and do the needful of making the HTTP call to the URLs defined in the list of webhook.
SCHEDULED REACTION RULE (TIME DRIVEN)
Rules can be triggered on two parameters – either on data or on time. The previous sample rule is triggered on data. the second type of rule is a timed rule which is triggered on time parameters with the help of the internal scheduler
component. In this type of rule, you define the trigger
with a standard cron expression.
Sample Timed Rule – Triggers a shell script every 5 minutes
{ "id": "rule-script-exec-demo", "description": "Cron rule to trigger script execution", "listen-to": "{time}", "enabled": false, "trigger": { "on-time-object": { "recurring": true, "using-expression": "*/5 * * * *" }, "evaluator": null }, "response":{ "intent": "start_script_execution", "parameters": { "name": "test.sh" } } }
The timed rule above is configured to make the reaction engine fire the intent by name start_script_execution
every 5 minutes. The Shell
module which is interested in this intent will capture the broadcast and do the needful of executing the script on the filesystem.
For security measures, scripts accessible to
shell
module are placed in a special folder within the Rayjas deployment from where they are scanned and indexed during startup. this means that the system cannot execute just any arbitrary script on the file system.