@@ -18,8 +18,6 @@ const logger = require('./common/logger');
18
18
const errors = require ( './common/errors' ) ;
19
19
const models = require ( './models' ) ;
20
20
21
- let emailTries = { } ;
22
-
23
21
/**
24
22
* Configure Kafka consumer.
25
23
* @param {Object } handlers the handlers
@@ -32,7 +30,12 @@ function configureKafkaConsumer(handlers) {
32
30
}
33
31
const pauseTime = parseInt ( config . EMAIL_PAUSE_TIME ) ;
34
32
const maxErrors = parseInt ( config . EMAIL_MAX_ERRORS ) ;
35
- const consumer = new Kafka . SimpleConsumer ( options ) ;
33
+
34
+ // email tries
35
+ let emailTries = 0 ;
36
+
37
+ // Kafka Consumer
38
+ const consumer = new Kafka . GroupConsumer ( options ) ;
36
39
37
40
// data handler
38
41
const dataHandler = ( messageSet , topic , partition ) => Promise . each ( messageSet , ( m ) => {
@@ -50,10 +53,11 @@ function configureKafkaConsumer(handlers) {
50
53
// return null to ignore this message
51
54
return null ;
52
55
}
56
+
53
57
let emailModel = { } ;
54
58
const messageJSON = JSON . parse ( message ) ;
55
59
const handlerAsync = Promise . promisify ( handler ) ;
56
- // use handler to create notification instances for each recipient
60
+
57
61
return models . Email . create (
58
62
Object . assign ( { status : 'PENDING' } , {
59
63
topicName,
@@ -72,23 +76,23 @@ function configureKafkaConsumer(handlers) {
72
76
status : result . success ? 'Message accepted' : 'Message rejected' ,
73
77
} ) ;
74
78
if ( result . success ) {
75
- emailTries [ topicName ] = 0 ;
79
+ emailTries = 0 ;
76
80
emailModel . status = 'SUCCESS' ;
77
81
return emailModel . save ( ) ;
78
82
} else {
79
- emailTries [ topicName ] += 1 ;
83
+ emailTries += 1 ;
80
84
emailModel . status = 'FAILED' ;
81
85
return emailModel . save ( ) . then ( ( ) => {
82
- const currentTries = emailTries [ topicName ] ;
86
+ const currentTries = emailTries ;
83
87
if ( currentTries > maxErrors ) {
84
88
logger . debug ( `Failed to send email. Will sleep for ${ pauseTime } s` ) ;
85
- emailTries [ topicName ] = 0 ;
89
+ emailTries = 0 ;
86
90
87
91
schedule . scheduleJob ( new Date ( now . getTime ( ) + pauseTime * 1000 ) , ( ) => {
88
- consumer . subscribe ( topic , dataHandler ) ;
92
+ return startKafkaConsumer ( consumer , handlers , dataHandler ) ;
89
93
} ) ;
90
94
91
- return consumer . unsubscribe ( topic , partition ) . then ( ( ) => {
95
+ return consumer . end ( ) . then ( ( ) => {
92
96
throw result . error
93
97
} ) ;
94
98
} else {
0 commit comments