File tree Expand file tree Collapse file tree 2 files changed +29
-6
lines changed Expand file tree Collapse file tree 2 files changed +29
-6
lines changed Original file line number Diff line number Diff line change @@ -7,8 +7,9 @@ async function consumerStart() {
7
7
ssl : true ,
8
8
sasl : {
9
9
mechanism : 'plain' ,
10
- username : '<fill>' ,
11
- password : '<fill>' ,
10
+ } ,
11
+ rdKafka : {
12
+ 'enable.auto.commit' : false
12
13
}
13
14
} ) ;
14
15
@@ -23,6 +24,7 @@ async function consumerStart() {
23
24
]
24
25
} )
25
26
27
+ var batch = 0 ;
26
28
consumer . run ( {
27
29
eachMessage : async ( { topic, partition, message } ) => {
28
30
console . log ( {
@@ -32,13 +34,21 @@ async function consumerStart() {
32
34
key : message . key ?. toString ( ) ,
33
35
value : message . value . toString ( ) ,
34
36
} )
37
+
38
+ if ( ++ batch % 100 == 0 ) {
39
+ await consumer . commitOffsets ( ) ;
40
+ batch = 0 ;
41
+ }
35
42
} ,
36
43
} ) ;
37
44
38
- const disconnect = ( ) =>
45
+ const disconnect = ( ) => {
46
+ process . off ( 'SIGINT' , disconnect ) ;
47
+ process . off ( 'SIGTERM' , disconnect ) ;
39
48
consumer . disconnect ( ) . then ( ( ) => {
40
49
console . log ( "Disconnected successfully" ) ;
41
50
} ) ;
51
+ }
42
52
process . on ( 'SIGINT' , disconnect ) ;
43
53
process . on ( 'SIGTERM' , disconnect ) ;
44
54
}
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ class Consumer {
31
31
if ( this . #kJSConfig. groupId != null ) {
32
32
config [ "group.id" ] = this . #kJSConfig. groupId ;
33
33
}
34
+ config [ 'offset_commit_cb' ] = true ;
34
35
return config ;
35
36
}
36
37
@@ -101,8 +102,10 @@ class Consumer {
101
102
async #consumeSingle( ) {
102
103
return new Promise ( ( resolve , reject ) => {
103
104
this . #internalClient. consume ( 1 , function ( err , messages ) {
104
- if ( err )
105
+ if ( err ) {
105
106
reject ( `Consume error code ${ err . code } ` ) ;
107
+ return ;
108
+ }
106
109
107
110
const message = messages [ 0 ] ;
108
111
resolve ( message ) ;
@@ -152,8 +155,18 @@ class Consumer {
152
155
}
153
156
}
154
157
155
- async commitOffsets ( topicPartitions ) {
156
- this . #notImplemented( ) ;
158
+ commitOffsets ( topicPartitions = null ) {
159
+ if ( topicPartitions == null ) {
160
+ this . #internalClient. commitSync ( ) ;
161
+ } else {
162
+ const topicPartitions = topicPartitions . map ( ( tpo ) => ( {
163
+ topic : tpo . topic ,
164
+ partition : tpo . partition ,
165
+ offset : Number ( tpo . offset ) ,
166
+ } ) )
167
+ this . #internalClient. commitSync ( topicPartitions ) ;
168
+ }
169
+ return Promise . resolve ( )
157
170
}
158
171
159
172
seek ( topicPartitionOffset ) {
You can’t perform that action at this time.
0 commit comments