-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
190 lines (167 loc) · 5.04 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// ---------------- define generic variables, so you won't need to search them across this file --------------
var argv = require('minimist')(process.argv.slice(2));
var infile = argv.infile;
var email_template = argv.email_template;
var email_from = argv.email_from;
var email_reply = argv.email_reply_to;
var email_subject = argv.email_subject;
var smtp_server = argv.smtp_server;
var behave = argv.behave; // fill_queue / deliver_queue
console.log('got arguments: ');
console.log(argv);
var bunyan = require('bunyan');
var nodemailer = require('nodemailer');
var smtpPool = require('nodemailer-smtp-pool');
var fs = require('fs'),
readline = require('readline');
var transporter = nodemailer.createTransport(smtpPool({
maxConnections: 4,
maxMessages: 10,
host: smtp_server,
secure: false,
ignoreTLS: true,
port: 25,
}));
// kue
var kue = require('kue');
var queue = kue.createQueue({
redis: {
host: 'redis',
}
});
var log = bunyan.createLogger(
{
name: "mailsender",
streams: [
{
level: 'info',
path: 'mailsender.log'
},
{
level: 'info',
stream: process.stdout
}
]
});
// reset counters
var email_total = 0;
var email_sent = 0;
// ---- count the number of emails we will send ----
var csv_file = fs.readFileSync(infile).toString().split("\n");
for (i in csv_file) {
email_total += 1;
}
email_total -= 1;
// ---- open a file descriptor for the csv -----
var rd = readline.createInterface({
input: fs.createReadStream(infile),
output: process.stdout,
terminal: false
});
// ----- read the template file into a variable ------
console.log('email template file: ' + email_template);
var email_torzs = fs.readFileSync(email_template).toString();
var textfile = email_template;
log.info('function: ' + behave);
kue.Job.rangeByType ('email', 'failed', 0, 10, 'asc', function (err, selectedJobs) {
selectedJobs.forEach(function (job) {
console.log(job.id + ', ' + job._state);
job.remove(function(err){
if (err) throw err;
console.log('removed failed job #%d, email to: ' + job.data.email_to, job.id);
});
});
});
// process queue
if ( behave == 'deliver_queue') {
queue_cnt = 0;
log.info('delivering queue');
// get active job count and exit when it's empty
queue.inactiveCount(function(err, total) {
console.log('inactive count before start proccessing queue: ' + total);
if(total == 0) {
log.info('no inactive job in the queue; nothing to do, exiting');
process.exit(1);
}
});
queue.process('email', function(job, done) {
queue.activeCount(function(err, total) {
console.log('active count: ' + total);
});
queue.inactiveCount(function(err, total) {
console.log('inactive count: ' + total);
});
queue.failedCount(function(err, total) {
console.log('failed count: ' + total);
});
queue.completeCount(function(err, total) {
console.log('complete count: ' + total);
});
queue_cnt += 1;
console.log('queue processing: ' + queue_cnt);
var mailOptions = {
from: job.data.email_from,
replyTo: job.data.email_reply,
to: job.data.email_to,
subject: job.data.email_subject,
html: job.data.email_text
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
done(new Error(error));
return log.info(error);
}
email_sent += 1;
log.info('[' + email_sent + '/' + email_total + '] Message sent to ' + job.data.email_to +': ' + info.response);
done();
queue.inactiveCount(function(err, total) {
console.log('inactive count while sending mail: ' + total);
if(total == 0) {
log.info('no more inactive job in queue, exiting.');
process.exit(1);
}
});
});
});
}
// ----- connect to redis and fill the queue -----------
if ( behave == 'fill_queue' ) {
var queue_cnt = 0;
log.info('filling queue with ' + email_total + ' messages');
rd.on('line', function(line) {
var data = line.split(";");
var email = data[0];
var content1 = data[1];
// -------- replace the template variable with we found in the csv file --------------
var text = email_torzs.replace('__CONTENT1__',content1);
var job = queue.create('email', {
email_subject: email_subject,
email_to: email,
email_text: text,
email_from: email_from,
email_reply: email_reply,
});
job.attempts(8).backoff( {delay: 5*1000, type:'fixed'} );
job.save( function(err){
if( !err ) {
queue_cnt += 1;
console.log( '[' + queue_cnt + '] queued ' + job.id );
} else {
log.info('Error queueing: ' + job.id);
}
console.log(queue_cnt + " / " + email_total);
if(queue_cnt == email_total) {
log.info('queue is filled, exiting. ');
process.exit(1);
}
});
// -------- construct the message ------------
var mailOptions = {
from: email_from,
replyTo: email_reply,
to: email,
subject: email_subject,
html: text
};
});
}