Trigger an API call automatically at a scheduled time using the reaction engine

"Only a life lived for others is a life worthwhile." – Albert Einstein

Trigger an API call automatically at a scheduled time using the reaction engine

Rayjas -> Reaction Engine coupled with the Generic Notifier can be used to make HTTP calls to a remote server at a scheduled time. This is entirely based on code and has nothing to do with system cron jobs. Of course, you can use cron for such functionality as well. But sometimes the simplicity of editing a JSON file can overtake the overwhelming complexity of configuring a cron (obviously there will be those that can beg to differ).

1. Install & Enable Reaction Engine

Make sure you have the reaction module installed on your rayjas distribution and enabled. The reaction engine will be responsible for triggering a timed rule in this case on a pre-scheduled time.

2. Enable Generic Notifier

The generic notifier module is already deployed with rayjas. All you need to do is make sure it is enabled.

3. Create a Rule To Trigger A HTTP Call

Next, we create a reaction rule to help trigger the notifier function. This will be a timed-based rule which uses the internal scheduler to trigger an HTTP GET call.

Reaction Rule to notify remote server at a scheduled time – rule-notify-endpoint.json

{
	"id": "rule-notify-endpoint",
	"description": "Cron rule to trigger HTTP call",
	"listen-to": "{time}",
	"enabled": false,
	"trigger": {
		"on-time-object": {
			"recurring": false,
			"using-expression": "*/5 * * * *"
		},
		"evaluator": null
	},
	"response":{
		"intent": "simple_http_notify",
		"parameters": {
			"http":{
				"webhook": ["http://localhost/endpoint"],
				"method": "GET"
			}
		}
	}		
}

The sample rule above should be called rule-notify-endpoint.json (matching its ID) and must reside in [RAYJAS-HOME]/rules folder. In the above-mentioned reaction rule, the attribute using-expression states the time expression of executing every 5 minutes but the recurring attribute set to false ensures that the trigger does not repeat more than once. So essentially the HTTP call will happen only once.

parameters.http.webhook is an array of remote endpoints that will receive the notification and parameters.http.method determines the HTTP method to use.

4. Restart Rayjas Service

After making any changes to the JSON configuration files, make sure to double-check the JSON’s validity and then restart rayjas service for changes to take effect.