digest cleanup and tests

v1.18.x
Barış Soner Uşaklı 8 years ago
parent 5f1eb70d01
commit e49f33317d

@ -12,8 +12,9 @@ var plugins = require('../plugins');
var emailer = require('../emailer');
var utils = require('../utils');
(function (Digest) {
Digest.execute = function (interval, callback) {
var Digest = module.exports;
Digest.execute = function (interval, callback) {
callback = callback || function () {};
var digestsDisabled = parseInt(meta.config.disableEmailSubscriptions, 10) === 1;
@ -22,10 +23,6 @@ var utils = require('../utils');
return callback();
}
if (!interval) {
// interval is one of: day, week, month, or year
interval = 'day';
}
var subscribers;
async.waterfall([
function (next) {
@ -62,9 +59,9 @@ var utils = require('../utils');
callback(err);
});
};
};
Digest.getSubscribers = function (interval, callback) {
Digest.getSubscribers = function (interval, callback) {
async.waterfall([
function (next) {
db.getSortedSetRange('digest:' + interval + ':uids', 0, -1, next);
@ -79,9 +76,9 @@ var utils = require('../utils');
next(null, results.subscribers);
},
], callback);
};
};
Digest.send = function (data, callback) {
Digest.send = function (data, callback) {
if (!data || !data.subscribers || !data.subscribers.length) {
return callback();
}
@ -128,5 +125,4 @@ var utils = require('../utils');
], function (err) {
callback(err);
});
};
}(module.exports));
};

@ -10,7 +10,7 @@ var jobs = {};
module.exports = function (User) {
User.startJobs = function (callback) {
winston.verbose('[user/jobs] (Re-)starting user jobs...');
var terminated = 0;
var started = 0;
var digestHour = parseInt(meta.config.digestHour, 10);
@ -21,37 +21,12 @@ module.exports = function (User) {
digestHour = 0;
}
// Terminate any active cron jobs
for (var jobId in jobs) {
if (jobs.hasOwnProperty(jobId)) {
winston.verbose('[user/jobs] Terminating job (' + jobId + ')');
jobs[jobId].stop();
delete jobs[jobId];
terminated += 1;
}
}
winston.verbose('[user/jobs] ' + terminated + ' jobs terminated');
User.stopJobs();
jobs['digest.daily'] = new cronJob('0 ' + digestHour + ' * * *', function () {
winston.verbose('[user/jobs] Digest job (daily) started.');
User.digest.execute('day');
}, null, true);
winston.verbose('[user/jobs] Starting job (digest.daily)');
started += 1;
jobs['digest.weekly'] = new cronJob('0 ' + digestHour + ' * * 0', function () {
winston.verbose('[user/jobs] Digest job (weekly) started.');
User.digest.execute('week');
}, null, true);
winston.verbose('[user/jobs] Starting job (digest.weekly)');
started += 1;
jobs['digest.monthly'] = new cronJob('0 ' + digestHour + ' 1 * *', function () {
winston.verbose('[user/jobs] Digest job (monthly) started.');
User.digest.execute('month');
}, null, true);
winston.verbose('[user/jobs] Starting job (digest.monthly)');
started += 1;
startDigestJob('digest.daily', '0 ' + digestHour + ' * * *', 'day');
startDigestJob('digest.weekly', '0 ' + digestHour + ' * * 0', 'week');
startDigestJob('digest.monthly', '0 ' + digestHour + ' 1 * *', 'month');
started += 3;
jobs['reset.clean'] = new cronJob('0 0 * * *', User.reset.clean, null, true);
winston.verbose('[user/jobs] Starting job (reset.clean)');
@ -63,5 +38,29 @@ module.exports = function (User) {
callback();
}
};
function startDigestJob(name, cronString, term) {
jobs[name] = new cronJob(cronString, function () {
winston.verbose('[user/jobs] Digest job (' + name + ') started.');
User.digest.execute(term);
}, null, true);
winston.verbose('[user/jobs] Starting job (' + name + ')');
}
User.stopJobs = function () {
var terminated = 0;
// Terminate any active cron jobs
for (var jobId in jobs) {
if (jobs.hasOwnProperty(jobId)) {
winston.verbose('[user/jobs] Terminating job (' + jobId + ')');
jobs[jobId].stop();
delete jobs[jobId];
terminated += 1;
}
}
if (terminated > 0) {
winston.verbose('[user/jobs] ' + terminated + ' jobs terminated');
}
};
};

@ -1301,6 +1301,30 @@ describe('User', function () {
});
});
describe('user jobs', function () {
it('should start user jobs', function (done) {
User.startJobs(function (err) {
assert.ifError(err);
done();
});
});
it('should stop user jobs', function (done) {
User.stopJobs();
done();
});
it('should send digetst', function (done) {
db.sortedSetAdd('digest:day:uids', [Date.now(), Date.now()], [1, 2], function (err) {
assert.ifError(err);
User.digest.execute('day', function (err) {
assert.ifError(err);
done();
});
});
});
});
after(function (done) {
db.emptydb(done);

Loading…
Cancel
Save