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.



28
29
30
# File 'lib/racecar/ctl.rb', line 28

def initialize(command)
  @command = command
end

Class Method Details

.main(args) ⇒ Object



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

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

#produce(args) ⇒ Object



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

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!

  kafka = Kafka.new(
    client_id: Racecar.config.client_id,
    seed_brokers: Racecar.config.brokers,
    logger: Racecar.logger,
    connect_timeout: Racecar.config.connect_timeout,
    socket_timeout: Racecar.config.socket_timeout,
  )

  kafka.deliver_message(message.value, key: message.key, topic: message.topic)

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

#status(args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/racecar/ctl.rb', line 32

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



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

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