5 var env
= require('gitter-web-env');
6 var winston
= env
.logger
;
7 var nconf
= env
.config
;
8 var notificationGenerator
= require('../../server/services/notifications/email-notification-generator-service');
10 var MAX_TIME
= 60000 * 5;
12 var shutdown
= require('shutdown');
13 var opts
= require('yargs')
16 default: nconf
.get('notifications:emailNotificationsAfterMins'),
18 description
: 'Age in minutes of the unread items'
21 .alias('help', 'h').argv
;
23 winston
.info('Looking for all unread messages older than ' + opts
.age
+ ' minutes');
24 var sinceTime
= Date
.now() - opts
.age
* 60 * 1000;
25 var startTime
= Date
.now();
27 return notificationGenerator(sinceTime
).then(function(result
) {
29 /* Only keep iterating up to the maximum time */
30 if (Date
.now() - startTime
< MAX_TIME
) {
33 winston
.warn('Reached MAX_TIME (' + MAX_TIME
+ ') before notifying everyone.');
41 shutdown
.shutdownGracefully();
43 .catch(function(err
) {
45 console
.log(err
.stack
);
46 winston
.error('send-unread-notifications failed: ' + err
, { exception
: err
});
47 shutdown
.shutdownGracefully(1);