Class: Racecar::Ctl

Inherits:
Object
  • Object
show all
Defined in:
lib/racecar/ctl.rb

Defined Under Namespace

Classes: ProduceMessage

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ Ctl

Returns a new instance of Ctl.



31
32
33
# File 'lib/racecar/ctl.rb', line 31

def initialize(command)
  @command = command
end

Class Method Details

.main(args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/racecar/ctl.rb', line 12

def self.main(args)
  command = args.shift

  if command.nil?
    puts "No command specified. Commands:"
    puts " - status"
    puts " - stop"
    puts " - produce"
  else
    ctl = new(command)

    if ctl.respond_to?(command)
      ctl.send(command, args)
    else
      raise Racecar::Error, "invalid command: #{command}"
    end
  end
end

Instance Method Details

#liveness_probe(args) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/racecar/ctl.rb', line 35

def liveness_probe(args)
  require "racecar/liveness_probe"
  parse_options!(args)

  if ENV["RAILS_ENV"] && File.exist?("config/racecar.yml")
    Racecar.config.load_file("config/racecar.yml", ENV["RAILS_ENV"])
  end

  if File.exist?("config/racecar.rb")
    require "./config/racecar"
  end

  Racecar.config.liveness_probe.check_liveness_within_interval!
end

#produce(args) ⇒ Object



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
# File 'lib/racecar/ctl.rb', line 81

def produce(args)
  message = ProduceMessage.new

  parser = OptionParser.new do |opts|
    opts.banner = "Usage: racecarctl produce [options]"

    opts.on("-v", "--value VALUE", "Set the message value") do |value|
      message.value = value
    end

    opts.on("-k", "--key KEY", "Set the message key") do |key|
      message.key = key
    end

    opts.on("-t", "--topic TOPIC", "Set the message topic") do |topic|
      message.topic = topic
    end
  end

  parser.parse!(args)

  if message.topic.nil?
    raise Racecar::Error, "no topic specified"
  end

  if message.value.nil?
    raise Racecar::Error, "no message value specified"
  end

  RailsConfigFileLoader.load!

  Racecar.config.validate!

  producer = Rdkafka::Config.new({
    "bootstrap.servers":  Racecar.config.brokers.join(","),
    "client.id":          Racecar.config.client_id,
    "message.timeout.ms": Racecar.config.message_timeout * 1000,
  }.merge(Racecar.config.rdkafka_producer)).producer

  handle = producer.produce(payload: message.value, key: message.key, topic: message.topic)
  begin
    handle.wait(max_wait_timeout: Racecar.config.message_timeout)
  rescue Rdkafka::RdkafkaError => e
    raise MessageDeliveryError.new(e, handle)
  end

  $stderr.puts "=> Delivered message to Kafka cluster"
end

#status(args) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/racecar/ctl.rb', line 50

def status(args)
  parse_options!(args)

  pidfile = Racecar.config.pidfile
  daemon = Daemon.new(pidfile)

  if daemon.running?
    puts "running (PID = #{daemon.pid})"
  else
    puts daemon.pid_status
  end
end

#stop(args) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/racecar/ctl.rb', line 63

def stop(args)
  parse_options!(args)

  pidfile = Racecar.config.pidfile
  daemon = Daemon.new(pidfile)

  if daemon.running?
    daemon.stop!
    while daemon.running?
      puts "Waiting for Racecar process to stop..."
      sleep 5
    end
    puts "Racecar stopped"
  else
    puts "Racecar is not currently running"
  end
end