Skip to content

Instantly share code, notes, and snippets.

@evantahler
Created October 5, 2015 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evantahler/192337ba5ea521e00c87 to your computer and use it in GitHub Desktop.
Save evantahler/192337ba5ea521e00c87 to your computer and use it in GitHub Desktop.
fancy actionhero task
var async = require('async');
var sendAbandonedCartEmail = function(api, cart, callback){
api.models.find({where: {id: cart.user_id}}).then(function(error, user){ // api.models are loaded via sequelize
if(error){ return next(error); )
if(!user){ return next(new Error('user not found')); )
api.cartHelper.sendAbandonmentEmail(api, user, callback); // api.cartHelper would be dinfined in an initializer
}).catch(callback);
};
var task = {
name: 'send_abandoned_cart_emails',
description: 'remind people to give me money',
queue: 'default',
plugins: [],
pluginOptions: [],
frequency: 1000 * 60 * 60,
run: function(api, params, next){
var emailSendJobs = [];
api.models.cart.findAll({where: {createdAt: 'lte ' + new Date(myDate.getTime() - (1000 * 60 * 60)))}}).then(function(error, rows){
if(error){ return next(error); )
if( rows.length === 0 ){ return next(); )
rows.forEach(function(cart){
emailSendJobs.push(function(asyncCallback){ sendAbandonedCartEmail(api, cart, asyncCallback); })
});
async.series(emailSendJobs, next);
}).catch(next);
});
};
exports.task = task;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment