@@ -16,10 +16,11 @@ import { createProcessor } from './processor';
16
16
import { ClickHouseConfig , createWriter } from './writer' ;
17
17
18
18
enum Status {
19
- Waiting ,
20
- Connected ,
21
- Ready ,
22
- Stopped ,
19
+ Waiting = 'Waiting' ,
20
+ Connected = 'Connected' ,
21
+ Unhealthy = 'Unhealthy' ,
22
+ Ready = 'Ready' ,
23
+ Stopped = 'Stopped' ,
23
24
}
24
25
25
26
const levelMap = {
@@ -98,8 +99,7 @@ export function createIngestor(config: {
98
99
99
100
async function stop ( ) {
100
101
logger . info ( 'Started Usage Ingestor shutdown...' ) ;
101
-
102
- status = Status . Stopped ;
102
+ changeStatus ( Status . Stopped ) ;
103
103
await consumer . disconnect ( ) ;
104
104
writer . destroy ( ) ;
105
105
logger . info ( `Consumer disconnected` ) ;
@@ -114,6 +114,8 @@ export function createIngestor(config: {
114
114
consumer . on ( 'consumer.crash' , async ev => {
115
115
logger . error ( 'Consumer crashed (restart=%s, error=%s)' , ev . payload . restart , ev . payload . error ) ;
116
116
117
+ changeStatus ( Status . Unhealthy ) ;
118
+
117
119
if ( ev . payload . restart ) {
118
120
return ;
119
121
}
@@ -126,15 +128,22 @@ export function createIngestor(config: {
126
128
logger . warn ( 'Consumer disconnected' ) ;
127
129
} ) ;
128
130
131
+ consumer . on ( 'consumer.fetch' , async ( ) => {
132
+ if ( status !== Status . Ready ) {
133
+ logger . info ( 'Consumer successfully fetched messages after being in status: %s' , status ) ;
134
+ changeStatus ( Status . Ready ) ;
135
+ }
136
+ } ) ;
137
+
129
138
async function start ( ) {
130
139
logger . info ( 'Starting Usage Ingestor...' ) ;
131
140
132
- status = Status . Waiting ;
141
+ changeStatus ( Status . Waiting ) ;
133
142
134
143
logger . info ( 'Connecting Kafka Consumer' ) ;
135
144
await consumer . connect ( ) ;
136
145
137
- status = Status . Connected ;
146
+ changeStatus ( Status . Connected ) ;
138
147
139
148
logger . info ( 'Subscribing to Kafka topic: %s' , config . kafka . topic ) ;
140
149
await consumer . subscribe ( {
@@ -164,7 +173,7 @@ export function createIngestor(config: {
164
173
} ,
165
174
} ) ;
166
175
logger . info ( 'Kafka is ready' ) ;
167
- status = Status . Ready ;
176
+ changeStatus ( Status . Ready ) ;
168
177
}
169
178
170
179
const processor = createProcessor ( { logger } ) ;
@@ -175,6 +184,15 @@ export function createIngestor(config: {
175
184
176
185
let status : Status = Status . Waiting ;
177
186
187
+ function changeStatus ( newStatus : Status ) {
188
+ if ( status === newStatus ) {
189
+ return ;
190
+ }
191
+
192
+ logger . info ( 'Changing status to %s' , newStatus ) ;
193
+ status = newStatus ;
194
+ }
195
+
178
196
return {
179
197
readiness ( ) {
180
198
return status === Status . Ready ;
0 commit comments