Sails comes bundled with Whelk, which lets you run JavaScript functions as shell scripts. This can be useful for running scheduled jobs (cron, Heroku scheduler), worker processes, and any other custom, one-off scripts that need access to your Sails app's models, configuration, and helpers.
To add a new script, just create a file in the scripts/
folder of your app.
sails generate script hello
Then, to run it, use:
sails run hello
If you need to run a script without global access to the
sails
command-line interface (in a Procfile, for example), usenode ./node_modules/sails/bin/sails run hello
.
Here's a more complex example that you might see in a real-world app:
// scripts/send-email-proof-reminders.js
module.exports = {
description: 'Send a reminder to any recent users who haven\'t confirmed their email address yet.',
inputs: {
template: {
description: 'The name of another email template to use as an optional override.',
type: 'string',
defaultsTo: 'reminder-to-confirm-email'
}
},
fn: async function (inputs, exits) {
await User.stream({
emailStatus: 'pending',
emailConfirmationReminderAlreadySent: false,
createdAt: { '>': Date.now() - 1000*60*60*24*3 }
})
.eachRecord(async (user, proceed)=>{
await sails.helpers.sendTemplateEmail.with({
template: inputs.template,
templateData: {
user: user
},
to: user.emailAddress
});
return proceed();
});//∞
return exits.success();
}
};
Then you can run:
sails run send-email-proof-reminders
For more detailed information on usage, see the whelk
README.