Class: Racecar::Runner
- Inherits:
-
Object
- Object
- Racecar::Runner
- Defined in:
- lib/racecar/runner.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#consumer ⇒ Object
readonly
Returns the value of attribute consumer.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#processor ⇒ Object
readonly
Returns the value of attribute processor.
Instance Method Summary collapse
-
#initialize(processor, config:, logger:, instrumenter: NullInstrumenter) ⇒ Runner
constructor
A new instance of Runner.
- #run ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(processor, config:, logger:, instrumenter: NullInstrumenter) ⇒ Runner
Returns a new instance of Runner.
7 8 9 10 |
# File 'lib/racecar/runner.rb', line 7 def initialize(processor, config:, logger:, instrumenter: NullInstrumenter) @processor, @config, @logger = processor, config, logger @instrumenter = instrumenter end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
5 6 7 |
# File 'lib/racecar/runner.rb', line 5 def config @config end |
#consumer ⇒ Object (readonly)
Returns the value of attribute consumer.
5 6 7 |
# File 'lib/racecar/runner.rb', line 5 def consumer @consumer end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
5 6 7 |
# File 'lib/racecar/runner.rb', line 5 def logger @logger end |
#processor ⇒ Object (readonly)
Returns the value of attribute processor.
5 6 7 |
# File 'lib/racecar/runner.rb', line 5 def processor @processor end |
Instance Method Details
#run ⇒ Object
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/racecar/runner.rb', line 19 def run kafka = Kafka.new( client_id: config.client_id, seed_brokers: config.brokers, logger: logger, connect_timeout: config.connect_timeout, socket_timeout: config.socket_timeout, ssl_ca_cert: config.ssl_ca_cert, ssl_ca_cert_file_path: config.ssl_ca_cert_file_path, ssl_client_cert: config.ssl_client_cert, ssl_client_cert_key: config.ssl_client_cert_key, sasl_plain_username: config.sasl_plain_username, sasl_plain_password: config.sasl_plain_password, sasl_scram_username: config.sasl_scram_username, sasl_scram_password: config.sasl_scram_password, sasl_scram_mechanism: config.sasl_scram_mechanism, sasl_over_ssl: config.sasl_over_ssl, ssl_ca_certs_from_system: config.ssl_ca_certs_from_system, ssl_verify_hostname: config.ssl_verify_hostname ) @consumer = kafka.consumer( group_id: config.group_id, offset_commit_interval: config.offset_commit_interval, offset_commit_threshold: config.offset_commit_threshold, session_timeout: config.session_timeout, heartbeat_interval: config.heartbeat_interval, offset_retention_time: config.offset_retention_time, fetcher_max_queue_size: config.max_fetch_queue_size, ) # Stop the consumer on SIGINT, SIGQUIT or SIGTERM. trap("QUIT") { stop } trap("INT") { stop } trap("TERM") { stop } # Print the consumer config to STDERR on USR1. trap("USR1") { $stderr.puts config.inspect } config.subscriptions.each do |subscription| consumer.subscribe( subscription.topic, start_from_beginning: subscription.start_from_beginning, max_bytes_per_partition: subscription.max_bytes_per_partition, ) end # Configure the consumer with a producer so it can produce messages. producer = kafka.producer( compression_codec: config.producer_compression_codec, ) processor.configure(consumer: consumer, producer: producer) begin if processor.respond_to?(:process) consumer.(max_wait_time: config.max_wait_time, max_bytes: config.max_bytes) do || payload = { consumer_class: processor.class.to_s, topic: .topic, partition: .partition, offset: .offset, } @instrumenter.instrument("process_message.racecar", payload) do processor.process() producer. end end elsif processor.respond_to?(:process_batch) consumer.each_batch(max_wait_time: config.max_wait_time, max_bytes: config.max_bytes) do |batch| payload = { consumer_class: processor.class.to_s, topic: batch.topic, partition: batch.partition, first_offset: batch.first_offset, message_count: batch..count, } @instrumenter.instrument("process_batch.racecar", payload) do processor.process_batch(batch) producer. end end else raise NotImplementedError, "Consumer class must implement process or process_batch method" end rescue Kafka::ProcessingError => e @logger.error "Error processing partition #{e.topic}/#{e.partition} at offset #{e.offset}" if config.pause_timeout > 0 # Pause fetches from the partition. We'll continue processing the other partitions in the topic. # The partition is automatically resumed after the specified timeout, and will continue where we # left off. @logger.warn "Pausing partition #{e.topic}/#{e.partition} for #{config.pause_timeout} seconds" consumer.pause( e.topic, e.partition, timeout: config.pause_timeout, max_timeout: config.max_pause_timeout, exponential_backoff: config.pause_with_exponential_backoff?, ) elsif config.pause_timeout == -1 # A pause timeout of -1 means indefinite pausing, which in ruby-kafka is done by passing nil as # the timeout. @logger.warn "Pausing partition #{e.topic}/#{e.partition} indefinitely, or until the process is restarted" consumer.pause(e.topic, e.partition, timeout: nil) end config.error_handler.call(e.cause, { topic: e.topic, partition: e.partition, offset: e.offset, }) # Restart the consumer loop. retry rescue Kafka::InvalidSessionTimeout raise ConfigError, "`session_timeout` is set either too high or too low" rescue Kafka::Error => e error = "#{e.class}: #{e.}\n" + e.backtrace.join("\n") @logger.error "Consumer thread crashed: #{error}" config.error_handler.call(e) raise else @logger.info "Gracefully shutting down" end end |
#stop ⇒ Object
12 13 14 15 16 17 |
# File 'lib/racecar/runner.rb', line 12 def stop Thread.new do processor.teardown consumer.stop unless consumer.nil? end.join end |