Email sending by our cron

This commit is contained in:
vjrj 2018-04-06 17:20:35 +02:00
parent 1570ef7d44
commit 61339f4eb2
2 changed files with 44 additions and 51 deletions

View file

@ -3,6 +3,7 @@
import { Meteor } from 'meteor/meteor'; import { Meteor } from 'meteor/meteor';
import { tweetIberiaFires, tweetEuropeFires } from '/imports/api/ActiveFires/server/tweetFiresInZone'; import { tweetIberiaFires, tweetEuropeFires } from '/imports/api/ActiveFires/server/tweetFiresInZone';
import { isMailServerMaster, sendEmailsFromQueue } from '/imports/startup/server/email';
// https://github.com/thesaucecode/meteor-synced-cron/ // https://github.com/thesaucecode/meteor-synced-cron/
@ -23,19 +24,35 @@ Meteor.startup(() => {
// timezone: 'utc', // timezone: 'utc',
/* /*
TTL in seconds for history records in collection to expire TTL in seconds for history records in collection to expire
NOTE: Unset to remove expiry but ensure you remove the index from NOTE: Unset to remove expiry but ensure you remove the index from
mongo by hand mongo by hand
ALSO: SyncedCron can't use the `_ensureIndex` command to modify ALSO: SyncedCron can't use the `_ensureIndex` command to modify
the TTL index. The best way to modify the default value of the TTL index. The best way to modify the default value of
`collectionTTL` is to remove the index by hand (in the mongo shell `collectionTTL` is to remove the index by hand (in the mongo shell
run `db.cronHistory.dropIndex({startedAt: 1})`) and re-run your run `db.cronHistory.dropIndex({startedAt: 1})`) and re-run your
project. SyncedCron will recreate the index with the updated TTL. project. SyncedCron will recreate the index with the updated TTL.
*/ */
// collectionTTL: 172800 // collectionTTL: 172800
}); });
if (isMailServerMaster) {
SyncedCron.add({
name: 'Send emails',
timezone: 'Europe/Madrid',
schedule: (parser) => {
// http://bunkat.github.io/later/
const sched = parser.text('every 1 min');
if (sched.error !== -1) {
console.error(`Mail cron 'when' field parsed with errors: ${sched.error}`);
}
return sched;
},
job: () => sendEmailsFromQueue()
});
}
const esEn = Meteor.settings.private.twitter.es.enabled; const esEn = Meteor.settings.private.twitter.es.enabled;
const enEn = Meteor.settings.private.twitter.en.enabled; const enEn = Meteor.settings.private.twitter.en.enabled;
@ -49,10 +66,10 @@ Meteor.startup(() => {
// userID: 'xyz' // userID: 'xyz'
// }, // },
schedule: (parser) => { schedule: (parser) => {
// this.magic = true; // Context is accesible here as this context. // this.magic = true; // Context is accesible here as this context.
// parser is a later.parse object // parser is a later.parse object
// return parser.text('every 2 minutes'); // return parser.text('every 2 minutes');
// http://bunkat.github.io/later/ // http://bunkat.github.io/later/
const sched = parser.text(Meteor.settings.private.twitter.es.when); const sched = parser.text(Meteor.settings.private.twitter.es.when);
if (sched.error !== -1) { if (sched.error !== -1) {
console.error(`Twitter cron 'when' field parsed with errors: ${sched.error}`); console.error(`Twitter cron 'when' field parsed with errors: ${sched.error}`);
@ -60,12 +77,12 @@ Meteor.startup(() => {
return sched; return sched;
}, },
job: () => tweetIberiaFires() job: () => tweetIberiaFires()
/* console.log('cron is working'); /* console.log('cron is working');
* console.log(this.userID) // Context Object becomes this argument * console.log(this.userID) // Context Object becomes this argument
* console.log(this.magic) / * console.log(this.magic) /
* var numbersCrunched = CrushSomeNumbers(); * var numbersCrunched = CrushSomeNumbers();
* return numbersCrunched; * return numbersCrunched;
return tweetFires(); */ return tweetFires(); */
}); });
} }
@ -84,7 +101,8 @@ Meteor.startup(() => {
}); });
} }
if (esEn || enEn) {
if (isMailServerMaster || esEn || enEn) {
SyncedCron.start(); SyncedCron.start();
} }
}); });

View file

@ -1,5 +1,4 @@
import { Meteor } from 'meteor/meteor'; import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import nodemailer from 'nodemailer'; import nodemailer from 'nodemailer';
import { MailTime } from 'meteor/ostrio:mailer'; import { MailTime } from 'meteor/ostrio:mailer';
import i18n from 'i18next'; import i18n from 'i18next';
@ -23,13 +22,14 @@ export const hr = `<table cellspacing="0" cellpadding="0" border="0" width="100%
let MailQueue; let MailQueue;
const isMailServerMaster = Meteor.settings.private.isMailServer && isMaster(); export const isMailServerMaster = Meteor.settings.private.isMailServer && isMaster();
if (isMailServerMaster) { if (isMailServerMaster) {
console.log('I\'m the mail server'); console.log('I\'m the mail server');
MailQueue = new MailTime({ MailQueue = new MailTime({
db, db,
type: 'server', type: 'client', // instead of 'server' for now to avoid use the JosK task manager at 250ms, we use our cron
// see https://github.com/VeliovGroup/Mail-Time/issues/5
strategy: 'balancer', // Transports will be used in round robin chain strategy: 'balancer', // Transports will be used in round robin chain
transports, transports,
from(transport) { from(transport) {
@ -86,31 +86,6 @@ if (Meteor.settings.private.testMailer) {
sendMail(emailOpts, true); sendMail(emailOpts, true);
} }
if (isMailServerMaster) { export const sendEmailsFromQueue = () => {
// Set interval to greater than 256 MailQueue.___send(() => { });
// https://github.com/VeliovGroup/Mail-Time/issues/5 };
const MailJobs = new Mongo.Collection('__JobTasks__mailTimeQueue', { idGeneration: 'MONGO' });
const updateJob = (mailJob, updated) => {
const delay = 60000;
if (Meteor.isDevelopment) console.log(`${updated ? 'Update old' : 'Update just added'} mailjob with ${delay} delay`);
if (mailJob.delay !== delay) {
MailJobs.update(mailJob._id, { $set: { delay } });
}
};
const mailJob = MailJobs.findOne();
if (mailJob) {
updateJob(mailJob);
}
MailJobs.find().observe({
added: function notifAdded(item) {
updateJob(item, false);
},
changed: function notifChanged(item) {
updateJob(item, true);
}
});
}