You can create one-time or recurring scheduled events to invoke serverless apps. At the specified time, the App Framework is notified which then invokes the relevant serverless method in the app.
Sample Use Cases
- Reminders: You can be notified about a specific event scheduled at a particular time. For example, you can trigger an SMS five minutes before a meeting.
- Data Sync: You can poll an external product every hour for updates and make the same changes in Freshteam.
- Recurring tasks: You can create daily/weekly reports or tickets to track recurring tasks.
Note:
1. You can have a maximum of 1000 one-time schedules and 1 recurring schedule.
2. The timeout period for app execution is 20 seconds and the default time zone is UTC. The schedule_at time must be at least 5 minutes from the current time.
Take a look at the Scheduled events Freshdesk sample app for a demonstration of this feature. The same functionality is available in Freshteam.
Payload
At the specified time, the corresponding method in the server.js file is invoked and the following payload is passed to the app.
Copied Copy1 2 3 4 5 6 7 8 9 10 11 12 | { "account_id" : "value", "event" : "onScheduledEvent", "region" : "value", "timestamp" : "value", "domain" : "value", "data" : {}, "iparams" : { "Param1" : "value", "Param2" : "value" } } |
The following table lists the payload attributes.
Attribute | Type | Description |
---|---|---|
account_id | string | The Freshteam account ID. |
event | string | The name of the event (onScheduledEvent). |
region | string | Region where the app is installed - "US". |
timestamp | number | Represents the time (epoch format) when the event was received. |
domain | string | The Freshteam account domain. |
data | JSON | The payload that was passed while creating the schedule. The payload should be of JSON type and the size should not exceed 4 KB. |
iparams | object | Installation parameters. |
Sample payload
Copied Copy1 2 3 4 5 6 7 8 9 10 11 12 13 14 | { "account_id": "12345", "event": "onScheduledEvent", "timestamp" : 1496400354326, "region" : "US", "domain": "xyz.freshteam.com", "data": { "account_name": "Jacob", "account_id": "3", "country": "France" }, "iparams": { } } |
Registration
To register a scheduled event and the corresponding callback:
- From your app’s root directory, navigate to the manifest.json file.
- Include the events attribute, specifying the scheduled event and the corresponding callback methods as follows:
Copied
Copy
12345
"events": { "onScheduledEvent": { "handler": "onScheduledEventHandler" } } Note: Include only one callback method for an event. Multiple events can access the same callback method.
- Navigate to the server.js file.
- In the exports block, enter the callback function definition as follows:
Copied
Copy
EXPAND ↓123456789exports = { onScheduledEventHandler: function(payload) { console.log("Logging arguments from onScheduledEvent: " + JSON.stringify(payload)); if(payload.data.account_id = "3") { //your code to perform any actions } } };
Schedules
You can use the following methods to create, update, and delete schedules.
To create a one-time schedule - server.js Copied Copy1 2 3 4 5 6 7 8 9 10 | $schedule.create({ name: "ticket_reminder", data: {ticket_id: 100001}, schedule_at: "2018-06-10T07:00:00.860Z", }) .then(function(data) { //"data" is a json with status and message. }, function(err) { //"err" is a json with status and message. }); |
To create a recurring schedule - server.js Copied Copy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $schedule.create({ name: "ticket_reminder", data: {ticket_id: 10001}, schedule_at: "2018-06-10T07:00:00.860Z", repeat: { time_unit: "minutes", frequency: 5 } }) .then(function(data) { //"data" is a json with status and message. }, function(err) { //"err" is a json with status and message. }); |
To fetch a schedule - server.js Copied Copy
1 2 3 4 5 6 7 8 | $schedule.fetch({ name: "ticket_reminder" }) .then(function(data) { //"data" is a json with name, data and schedule_at used to create the schedule }, function(err) { // “err” is a json with status and message. }); |
To update a schedule - server.js Copied Copy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $schedule.update({ name: "ticket_reminder", data: {ticket_id: 10001}, schedule_at: "2018-06-10T07:00:00.860Z", repeat: { time_unit: "hours", frequency: 1 } }) .then(function(data) { //"data" is a json with status and message. }, function(err) { //"err" is a json with status and message. }); |
To delete a schedule - server.js Copied Copy
1 2 3 4 5 6 7 8 | $schedule.delete({ name: "ticket_reminder" }) .then(function(data) { //"data" is a json with status and message. }, function(err) { //"err" is a json with status and message. }); |
The parameters and their description are listed in the table.
PARAMETER | TYPE | DESCRIPTION |
---|---|---|
name | string | Unique string to identify the schedule. |
data | json | Data that needs to be passed to the schedule event handler when the event is fired. |
schedule_at | time (ISO format) | Time at which the schedule needs to be triggered. Note: For recurring schedules (with time_unit as days), the hour and minutes at which the scheduled event must be fired will be taken from the schedule_at value. |
time_unit | string | Time unit (minutes, hours, or days) for which the recurring schedule is created. |
frequency | number | Frequency of execution of an event with respect to the time unit. |
Testing
Notes:
1. Use the latest version of Chrome browser.
2. Ensure that the onScheduledEvent.json file, which contains the sample payload to test scheduled events, is available at <app's root directory>/server/test_data.
When testing scheduled events on your computer, schedules will be triggered at the specified time and frequency after they are created or updated.