@@ -232,6 +232,7 @@ defmodule KafkaEx.GenConsumer do
232232 """
233233 @ callback init ( topic :: binary , partition :: non_neg_integer ) ::
234234 { :ok , state :: term }
235+ | { :stop , reason :: term }
235236
236237 @ doc """
237238 Invoked when the server is started. `start_link/5` will block until it
@@ -255,7 +256,7 @@ defmodule KafkaEx.GenConsumer do
255256 topic :: binary ,
256257 partition :: non_neg_integer ,
257258 extra_args :: map ( )
258- ) :: { :ok , state :: term }
259+ ) :: { :ok , state :: term } | { :stop , reason :: term }
259260
260261 @ doc """
261262 Invoked for each message set consumed from a Kafka topic partition.
@@ -287,6 +288,8 @@ defmodule KafkaEx.GenConsumer do
287288 """
288289 @ callback handle_call ( call :: term , from :: GenServer . from ( ) , state :: term ) ::
289290 { :reply , reply_value :: term , new_state :: term }
291+ | { :stop , reason :: term , reply_value :: term , new_state :: term }
292+ | { :stop , reason :: term , new_state :: term }
290293
291294 @ doc """
292295 Invoked by `KafkaEx.GenConsumer.cast/2`.
@@ -296,6 +299,7 @@ defmodule KafkaEx.GenConsumer do
296299 """
297300 @ callback handle_cast ( cast :: term , state :: term ) ::
298301 { :noreply , new_state :: term }
302+ | { :stop , reason :: term , new_state :: term }
299303
300304 @ doc """
301305 Invoked by sending messages to the consumer.
@@ -305,6 +309,7 @@ defmodule KafkaEx.GenConsumer do
305309 """
306310 @ callback handle_info ( info :: term , state :: term ) ::
307311 { :noreply , new_state :: term }
312+ | { :stop , reason :: term , new_state :: term }
308313
309314 defmacro __using__ ( _opts ) do
310315 quote do
@@ -541,44 +546,49 @@ defmodule KafkaEx.GenConsumer do
541546 api_versions = Keyword . get ( opts , :api_versions , % { } )
542547 api_versions = Map . merge ( default_api_versions , api_versions )
543548
544- { :ok , consumer_state } =
545- consumer_module . init ( topic , partition , extra_consumer_args )
549+ case consumer_module . init ( topic , partition , extra_consumer_args ) do
550+ { :ok , consumer_state } ->
551+ worker_opts = Keyword . take ( opts , [ :uris , :use_ssl , :ssl_options ] )
546552
547- worker_opts = Keyword . take ( opts , [ :uris , :use_ssl , :ssl_options ] )
553+ { :ok , worker_name } =
554+ KafkaEx . create_worker (
555+ :no_name ,
556+ [ consumer_group: group_name ] ++ worker_opts
557+ )
548558
549- { :ok , worker_name } =
550- KafkaEx . create_worker (
551- :no_name ,
552- [ consumer_group: group_name ] ++ worker_opts
553- )
559+ default_fetch_options = [
560+ auto_commit: false ,
561+ worker_name: worker_name
562+ ]
554563
555- default_fetch_options = [
556- auto_commit: false ,
557- worker_name: worker_name
558- ]
564+ given_fetch_options = Keyword . get ( opts , :fetch_options , [ ] )
559565
560- given_fetch_options = Keyword . get ( opts , : fetch_options, [ ] )
561- fetch_options = Keyword . merge ( default_fetch_options , given_fetch_options )
562-
563- state = % State {
564- consumer_module: consumer_module ,
565- consumer_state: consumer_state ,
566- commit_interval: commit_interval ,
567- commit_threshold: commit_threshold ,
568- auto_offset_reset: auto_offset_reset ,
569- worker_name: worker_name ,
570- group: group_name ,
571- topic: topic ,
572- partition: partition ,
573- generation_id: generation_id ,
574- member_id: member_id ,
575- fetch_options: fetch_options ,
576- api_versions: api_versions
577- }
566+ fetch_options =
567+ Keyword . merge ( default_fetch_options , given_fetch_options )
568+
569+ state = % State {
570+ consumer_module: consumer_module ,
571+ consumer_state: consumer_state ,
572+ commit_interval: commit_interval ,
573+ commit_threshold: commit_threshold ,
574+ auto_offset_reset: auto_offset_reset ,
575+ worker_name: worker_name ,
576+ group: group_name ,
577+ topic: topic ,
578+ partition: partition ,
579+ generation_id: generation_id ,
580+ member_id: member_id ,
581+ fetch_options: fetch_options ,
582+ api_versions: api_versions
583+ }
578584
579- Process . flag ( :trap_exit , true )
585+ Process . flag ( :trap_exit , true )
580586
581- { :ok , state , 0 }
587+ { :ok , state , 0 }
588+
589+ { :stop , reason } ->
590+ { :stop , reason }
591+ end
582592 end
583593
584594 def handle_call ( :partition , _from , state ) do
@@ -597,14 +607,23 @@ defmodule KafkaEx.GenConsumer do
597607 # which we turn into a timeout = 0 clause so that we continue to consume.
598608 # any other GenServer flow control could have unintended consequences,
599609 # so we leave that for later consideration
600- { :reply , reply , new_consumer_state } =
610+ consumer_reply =
601611 consumer_module . handle_call (
602612 message ,
603613 from ,
604614 consumer_state
605615 )
606616
607- { :reply , reply , % { state | consumer_state: new_consumer_state } , 0 }
617+ case consumer_reply do
618+ { :reply , reply , new_consumer_state } ->
619+ { :reply , reply , % { state | consumer_state: new_consumer_state } , 0 }
620+
621+ { :stop , reason , new_consumer_state } ->
622+ { :stop , reason , % { state | consumer_state: new_consumer_state } }
623+
624+ { :stop , reason , reply , new_consumer_state } ->
625+ { :stop , reason , reply , % { state | consumer_state: new_consumer_state } }
626+ end
608627 end
609628
610629 def handle_cast (
@@ -618,13 +637,19 @@ defmodule KafkaEx.GenConsumer do
618637 # which we turn into a timeout = 0 clause so that we continue to consume.
619638 # any other GenServer flow control could have unintended consequences,
620639 # so we leave that for later consideration
621- { :noreply , new_consumer_state } =
640+ consumer_reply =
622641 consumer_module . handle_cast (
623642 message ,
624643 consumer_state
625644 )
626645
627- { :noreply , % { state | consumer_state: new_consumer_state } , 0 }
646+ case consumer_reply do
647+ { :noreply , new_consumer_state } ->
648+ { :noreply , % { state | consumer_state: new_consumer_state } , 0 }
649+
650+ { :stop , reason , new_consumer_state } ->
651+ { :stop , reason , % { state | consumer_state: new_consumer_state } }
652+ end
628653 end
629654
630655 def handle_info (
@@ -660,13 +685,19 @@ defmodule KafkaEx.GenConsumer do
660685 # which we turn into a timeout = 0 clause so that we continue to consume.
661686 # any other GenServer flow control could have unintended consequences,
662687 # so we leave that for later consideration
663- { :noreply , new_consumer_state } =
688+ consumer_reply =
664689 consumer_module . handle_info (
665690 message ,
666691 consumer_state
667692 )
668693
669- { :noreply , % { state | consumer_state: new_consumer_state } , 0 }
694+ case consumer_reply do
695+ { :noreply , new_consumer_state } ->
696+ { :noreply , % { state | consumer_state: new_consumer_state } , 0 }
697+
698+ { :stop , reason , new_consumer_state } ->
699+ { :stop , reason , % { state | consumer_state: new_consumer_state } }
700+ end
670701 end
671702
672703 def terminate ( _reason , % State { } = state ) do
@@ -689,7 +720,8 @@ defmodule KafkaEx.GenConsumer do
689720 KafkaEx . fetch (
690721 topic ,
691722 partition ,
692- Keyword . merge ( fetch_options ,
723+ Keyword . merge (
724+ fetch_options ,
693725 offset: offset ,
694726 api_version: Map . fetch! ( state . api_versions , :fetch )
695727 )
@@ -850,9 +882,12 @@ defmodule KafkaEx.GenConsumer do
850882 # one of these needs to match, depending on which client
851883 case partition_response do
852884 # old client
853- ^ partition -> :ok
885+ ^ partition ->
886+ :ok
887+
854888 # new client
855- % { error_code: :no_error , partition: ^ partition } -> :ok
889+ % { error_code: :no_error , partition: ^ partition } ->
890+ :ok
856891 end
857892
858893 Logger . debug ( fn ->
0 commit comments