-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkafka_consumer_worker.rb
74 lines (59 loc) · 1.65 KB
/
kafka_consumer_worker.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require "sd_notify"
require "systemd-daemon"
# A wrapper around {KafkaConsumer} that processes messages in a
# loop. It integrates with systemd and facilitates spawning consumers as
# as long-running processes.
#
# @see {KafkaConsumer}
class KafkaConsumerWorker
# @param consumer [KafkaConsumer]
def initialize(consumer)
@consumer = consumer
@watchdog = SystemdDaemon::Notify.watchdog?
end
# Implements the consume-process loop.
def work
Signal.trap("TERM") { trigger_shutdown }
Signal.trap("INT") { trigger_shutdown }
SdNotify.ready
set_status("Spawned")
Rails.logger.tagged("kafka", "consumer", @consumer.to_s) do
Rails.logger.warn "Started consuming '#{@consumer.topic}'..."
begin
loop do
watchdog_ping
break if @shutdown
msg = @consumer.process_next(4)
if msg.nil?
set_status("Working (consume timeout)...")
next
end
@offset = msg.offset
@partition = msg.partition
# throttle a bit
set_status("Working...") if (@offset % 10).zero?
end
ensure
@consumer.shutdown
end
Rails.logger.warn "Bye"
end
# so that systemd does not consider us failed
exit 0
end
def trigger_shutdown
@shutdown = true
SdNotify.stopping
set_status("Shutting down...")
end
private
def set_status(x)
SdNotify.status(
"#{@consumer} (offset=#{@offset},partition=#{@partition}) | " \
"#{Time.now.strftime('%H:%M:%S')} #{x}")
Process.setproctitle("consumer=#{@consumer} #{x}")
end
def watchdog_ping
SdNotify.watchdog if @watchdog
end
end