Back to Graphql Engine

Create a Scheduled Trigger

docs/docs/scheduled-triggers/create-cron-trigger.mdx

2.50.15.6 KB
Original Source

import Thumbnail from '@site/src/components/Thumbnail'; import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

Create a Scheduled Trigger

Introduction

Scheduled Triggers are used to reliably trigger HTTP endpoints to run custom business logic periodically based on a cron schedule. For example, you can create a Scheduled Trigger to generate an end-of-day sales report every weekday at 10pm.

To add a Scheduled Trigger, follow these steps:

Step 1: Define the Scheduled Trigger

The following fields are required to define a Scheduled Trigger:

  • Name: A name for the Scheduled Trigger.
  • Webhook: The HTTP endpoint that should be triggered.
  • Cron schedule: A cron expression defining the schedule for the cron. Cron events are created based on the UTC timezone. You can use tools like crontab guru to help build a cron expression.
  • Payload: The JSON payload which will be sent to the webhook.

You can also define a comment that helps you identify the Scheduled Trigger.

For example, we can create a Scheduled Trigger called eod_reports, to trigger the webhook https://mywebhook.com/eod with the cron schedule 0 22 * * 1-5, which means "At 22:00 on every day-of-week from Monday through Friday" (you can check this here).

<Tabs groupId="user-preference" className="api-tabs"> <TabItem value="console" label="Console">

Navigate to Events > Cron Triggers > Create in your Hasura Console.

<Thumbnail src="/img/event-triggers/create-cron.png" alt="Adding a Scheduled Trigger" width="1000px" />

In the form opened, fill out the fields defined above:

You can use the link next to the Cron Schedule field to help build a cron expression using crontab guru, or use the Frequently used crons dropdown as a shortcut.

<Thumbnail src="/img/event-triggers/define-cron-trigger.png" alt="Defining a Scheduled Trigger" width="500px" /> </TabItem> <TabItem value="cli" label="CLI">

You can define a Scheduled Trigger by adding it to the cron_triggers.yaml file inside the metadata directory:

yaml
- name: eod_reports
  webhook: https://mywebhook.com/eod
  schedule: 0 22 * * 1-5
  include_in_metadata: true
  payload: {}

Apply the Metadata by running:

yaml
hasura metadata apply
</TabItem> <TabItem value="api" label="API">

You can define a Scheduled Trigger via the create_cron_trigger Metadata API:

http
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin

{
   "type": "create_cron_trigger",
   "args": {
      "name": "eod_reports",
      "webhook": "https://mywebhook.com/eod",
      "schedule": "0 22 * * 1-5",
      "payload": {},
      "include_in_metadata": true
   }
}
</TabItem> </Tabs>

Step 2: Define advanced options (Optional)

If you like, you can also define the following values:

  • Headers: List of headers to be sent to the webhook.
  • Retry configuration: In case the call to the webhook fails.
  • Include in metadata: When set to true, the Scheduled Trigger will be included in the metadata and can be exported along with it.
<Tabs groupId="user-preference" className="api-tabs"> <TabItem value="console" label="Console">

Expand the Advanced section.

<Thumbnail src="/img/event-triggers/advanced-cron.png" alt="Defining advanced options for a Scheduled Trigger" width="700px" />

</TabItem> <TabItem value="cli" label="CLI">

You can define advanced options for a crone trigger when adding it to the cron_triggers.yaml file inside the metadata directory:

yaml
- name: eod_reports
  webhook: https://mywebhook.com/eod
  schedule: 0 22 * * 1-5
  include_in_metadata: true
  payload: {}
  retry_conf:
    num_retries: 3
    timeout_seconds: 120
    tolerance_seconds: 21675
    retry_interval_seconds: 12
  comment: This is a comment

Apply the Metadata by running:

yaml
hasura metadata apply
</TabItem> <TabItem value="api" label="API">

You can define advanced options for a Scheduled Trigger when defining it via the create_cron_trigger Metadata API:

http
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin

{
   "type": "create_cron_trigger",
   "args": {
      "name": "eod_reports",
      "webhook": "https://mywebhook.com/eod",
      "schedule": "0 22 * * 1-5",
      "include_in_metadata": true,
      "payload": {},
      "retry_conf": {
            "num_retries": 3,
            "timeout_seconds": 120,
            "tolerance_seconds": 21675,
            "retry_interval_seconds": 12
      },
      "comment": "sample_cron comment"
   }
}
</TabItem> </Tabs>

Schedule & logs

Once you've created your Scheduled Trigger, you can see Pending events, Processed events, and Invocation logs in their respective tabs.

<Thumbnail src="/img/event-triggers/pending-cron.png" alt="Schedule and logs for Scheduled Triggers" width="1200px" />

Rest Connectors

REST Connectors i.e. request and response transformations, for Scheduled Triggers are used to invoke existing or third-party webhooks without needing any middleware or modifications to the upstream code.

REST Connectors modify the Scheduled Trigger's HTTP request to adapt to your webhook's expected format by adding suitable transforms.

Rest Connectors for Scheduled Triggers can be configured in the same way as rest connectors for event triggers. You can read more about it in the REST Connectors section.