@@ -33,6 +33,9 @@ type batchProcessor struct {
33
33
// session is required to commit the offsets on succesfull processing
34
34
session sarama.ConsumerGroupSession
35
35
36
+ // mainContext for cancellations
37
+ mainContext context.Context
38
+
36
39
// s3Sink
37
40
s3sink * s3sink.S3Sink
38
41
@@ -100,6 +103,7 @@ func newBatchProcessor(
100
103
topic string ,
101
104
partition int32 ,
102
105
session sarama.ConsumerGroupSession ,
106
+ mainContext context.Context ,
103
107
kafkaConfig kafka.KafkaConfig ,
104
108
saramaConfig kafka.SaramaConfig ,
105
109
kafkaLoaderTopicPrefix string ,
@@ -146,6 +150,7 @@ func newBatchProcessor(
146
150
partition : partition ,
147
151
autoCommit : saramaConfig .AutoCommit ,
148
152
session : session ,
153
+ mainContext : mainContext ,
149
154
s3sink : sink ,
150
155
s3BucketDir : viper .GetString ("s3sink.bucketDir" ),
151
156
bodyBuf : bytes .NewBuffer (make ([]byte , 0 , 4096 )),
@@ -179,8 +184,10 @@ func removeEmptyNullValues(value map[string]*string) map[string]*string {
179
184
// TODO: get rid of this https://github.com/herryg91/gobatch/issues/2
180
185
func (b * batchProcessor ) ctxCancelled () bool {
181
186
select {
182
- case <- b .session .Context ().Done ():
183
- klog .Infof (
187
+ case <- b .mainContext .Done ():
188
+ err := b .mainContext .Err ()
189
+ klog .Warningf ("Batch processing stopped, mainContext done, ctxErr: %v" , err )
190
+ klog .V (2 ).Infof (
184
191
"topic:%s, batchId:%d, lastCommittedOffset:%d: Cancelled.\n " ,
185
192
b .topic , b .batchId , b .lastCommittedOffset ,
186
193
)
@@ -393,12 +400,12 @@ func (b *batchProcessor) processMessage(message *serializer.Message, id int) {
393
400
// otherwise return false in case of gracefull shutdown signals being captured,
394
401
// this helps in cleanly shutting down the batch processing.
395
402
func (b * batchProcessor ) processBatch (
396
- ctx context.Context , datas []interface {}) bool {
403
+ mainContext context.Context , datas []interface {}) bool {
397
404
398
405
b .s3Key = ""
399
406
for id , data := range datas {
400
407
select {
401
- case <- ctx .Done ():
408
+ case <- mainContext .Done ():
402
409
return false
403
410
default :
404
411
b .processMessage (data .(* serializer.Message ), id )
@@ -423,7 +430,7 @@ func (b *batchProcessor) process(workerID int, datas []interface{}) {
423
430
b .topic , b .batchId , len (datas ),
424
431
)
425
432
426
- done := b .processBatch (b .session . Context () , datas )
433
+ done := b .processBatch (b .mainContext , datas )
427
434
if ! done {
428
435
b .handleShutdown ()
429
436
return
0 commit comments