Trigger Scripts
Triggers are special actions that run periodically given a schedule. By default, adding a trigger will set the schedule to 15 minutes.
Check our pages dedicated to Scheduling and Triggering flows.
Triggers scripts are designed to pull data from an external source and return all of the new items since the last run.
This type of Flow is meant to be scheduled regularly to reduce latency when reacting to new events. It will trigger the rest of the Flow once per new item that is returned. If there are no new items, the flow will be skipped.
Think of this as someone who checks the mailbox every day. If there is a new letter, they will continue to process it - open and read it - and if there is no new letter, they won't do anything.
The key part is that opened letters are not placed back in the mailbox. In Windmill, a Trigger Script has the job to keep track of what's processed and what's not.
Flows can be scheduled through the Flow UI using a CRON expression and then activating the schedule as seen in the image below.
Examples of trigger scripts include:
- Trigger everytime a new item text on HackerNews match at least one mention
- Notify of new Github repo stars
- Check new uploaded files on Google Drive
The following TypeScript code is an example of the first module of a Flow that checks for new documents in a MongoDB collection on a regular schedule. In this case we query documents that were created after a specific time, expressed with a timestamp. The timestamp is stored with the help of Windmill's built-in state functions and is updated in each run.
Code below:
import {
getState,
type Resource,
setState,
} from "https://deno.land/x/windmill/mod.ts";
import { MongoClient, ObjectId } from "https://deno.land/x/atlas_sdk/mod.ts";
export async function main(
auth: Resource<"mongodb_rest">,
data_source: string,
database: string,
collection: string,
) {
const client = new MongoClient({
endpoint: auth.endpoint,
dataSource: data_source,
auth: { apiKey: auth.api_key },
});
const documents = client.database(database).collection(collection);
const lastCheck = await getState() || 0;
await setState(Date.now() / 1000);
const id = ObjectId.createFromTime(lastCheck);
return await documents.find({ "_id": { "$gt": id } });
}
You can find this exact Trigger Script on Windmill Hub, or many more examples here.