@@ -14,8 +14,8 @@ class ImapNotifier extends EventEmitter {
1414 super ( ) ;
1515
1616 this . database = options . database ;
17- this . publisher = options . redis || new Redis ( tools . redisConfig ( config . dbs . redis ) ) ;
18- this . counters = counters ( this . publisher ) ;
17+ this . redis = options . redis || new Redis ( tools . redisConfig ( config . dbs . redis ) ) ;
18+ this . counters = counters ( this . redis ) ;
1919
2020 this . logger = options . logger || {
2121 info : log . silly . bind ( log , 'IMAP' ) ,
@@ -210,7 +210,7 @@ class ImapNotifier extends EventEmitter {
210210 let pushToJournal = async ( ) => {
211211 for ( let entry of entries ) {
212212 if ( entry . command === 'EXISTS' && entry . junk ) {
213- await publish ( this . publisher , {
213+ await publish ( this . redis , {
214214 ev : entry . junk > 0 ? MARKED_SPAM : MARKED_HAM ,
215215 user : entry . user . toString ( ) ,
216216 mailbox : entry . mailbox . toString ( ) ,
@@ -225,7 +225,11 @@ class ImapNotifier extends EventEmitter {
225225 ordered : false
226226 } ) ;
227227
228- setImmediate ( ( ) => this . updateCounters ( entries ) ) ;
228+ try {
229+ await this . updateCounters ( entries ) ;
230+ } catch ( err ) {
231+ this . logger . error ( 'Failed to update mailbox counters' , err . message ) ;
232+ }
229233
230234 return r . insertedCount ;
231235 } ;
@@ -298,7 +302,7 @@ class ImapNotifier extends EventEmitter {
298302 e : user . toString ( ) ,
299303 p : payload
300304 } ) ;
301- this . publisher . publish ( 'wd_events' , data ) ;
305+ this . redis . publish ( 'wd_events' , data ) ;
302306 } ) ;
303307 }
304308
@@ -322,10 +326,11 @@ class ImapNotifier extends EventEmitter {
322326 . toArray ( callback ) ;
323327 }
324328
325- updateCounters ( entries ) {
329+ async updateCounters ( entries ) {
326330 if ( ! entries ) {
327331 return ;
328332 }
333+
329334 let counters = new Map ( ) ;
330335 ( Array . isArray ( entries ) ? entries : [ ] . concat ( entries || [ ] ) ) . forEach ( entry => {
331336 let m = entry . mailbox . toString ( ) ;
@@ -358,33 +363,24 @@ class ImapNotifier extends EventEmitter {
358363 }
359364 } ) ;
360365
361- let pos = 0 ;
362- let rows = Array . from ( counters ) ;
363- let updateCounter = ( ) => {
364- if ( pos >= rows . length ) {
365- return ;
366- }
367- let row = rows [ pos ++ ] ;
366+ for ( let row of Array . from ( counters ) ) {
368367 if ( ! row || ! row . length ) {
369- return updateCounter ( ) ;
368+ continue ;
370369 }
370+
371371 let mailbox = row [ 0 ] ;
372372 let delta = row [ 1 ] ;
373373
374- this . counters . cachedcounter ( 'total:' + mailbox , delta . total , consts . MAILBOX_COUNTER_TTL , ( ) => {
375- if ( delta . unseenChange ) {
376- // Message info changed in mailbox, so just te be sure, clear the unseen counter as well
377- // Unseen counter is more volatile and also easier to count (usually only a small number on indexed messages)
378- this . publisher . del ( 'unseen:' + mailbox , updateCounter ) ;
379- } else if ( delta . unseen ) {
380- this . counters . cachedcounter ( 'unseen:' + mailbox , delta . unseen , consts . MAILBOX_COUNTER_TTL , updateCounter ) ;
381- } else {
382- setImmediate ( updateCounter ) ;
383- }
384- } ) ;
385- } ;
374+ await this . redis . cachedcounter ( `total:${ mailbox } ` , delta . total , consts . MAILBOX_COUNTER_TTL ) ;
386375
387- updateCounter ( ) ;
376+ if ( delta . unseenChange ) {
377+ // Message info changed in mailbox, so just te be sure, clear the unseen counter as well
378+ // Unseen counter is more volatile and also easier to count (usually only a small number on indexed messages)
379+ await this . redis . del ( 'unseen:' + mailbox ) ;
380+ } else if ( delta . unseen ) {
381+ await this . redis . cachedcounter ( `unseen:${ mailbox } ` , delta . unseen , consts . MAILBOX_COUNTER_TTL ) ;
382+ }
383+ }
388384 }
389385
390386 allocateConnection ( data , callback ) {
0 commit comments