Subscribe to our list to receive the latest updates on Node on Fire

Class: Triggers

Triggers

new Triggers(app)

The Triggers module allows you to execute tasks once whenever a certain condition matches. For example, send a mail to a user when the user signed up 7 days ago, or send a mail to your user 2 days after their last activity. The Trigger modules guarantees only one task is executed when the condition matches.

By default, triggers run every 30 minutes and check their conditions. Triggers were not designed to provide a time-critical solution. If you need near instant timing, consider directly evaluating triggers during your model hooks manually.

To create a trigger to send a mail to a user when he or she signed up 7 days ago:

function MyTrigger() {
    //
}
app.trigger(MyTrigger);

MyTrigger.prototype.select = function() {
    return {createdAt:{$lt: moment().add(-1, 'weeks').toDate()}};
};

MyTrigger.prototype.run = function(user) {
    return sendMailToTheUser(user);
};

You can store your triggers in the triggers/ folder which automatically gets loaded.

Tip: set the TZ environment variable to be explicit about the time zone to use. It's generally a good idea to align this setting with whatever your datastore is configured to.

Node on Fire creates a worker process to run the triggers. This should be added to your Procfile automatically.

worker: node index.js --triggers

Triggers internally use the Clock module which will also be added to your Procfile if not added yet.

clock: node index.js --clock
Parameters:
Name Type Description
app App

Methods

migrate(models)

The migrate phase which gets called during the build migrations phase.

Parameters:
Name Type Description
models Models

The active models.

trigger(triggerConstructor)

Creates a trigger.

Parameters:
Name Type Description
triggerConstructor Constructor
Returns:
Type
App

Do you have any questions or comments on this page? Start a discussion below.

comments powered by Disqus
Subscribe to our list to receive the latest updates on Node on Fire