Class: Slnky::CLI::Command

Inherits:
Base
  • Object
show all
Defined in:
lib/slnky/cli/command.rb

Instance Method Summary collapse

Instance Method Details

#amqp(msg) ⇒ Object



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
# File 'lib/slnky/cli/command.rb', line 35

def amqp(msg)
  response = msg.response
  tx = Slnky::Transport.instance

  tx.start!(self) do |_|
    tx.exchange('response', :direct)
    queue = tx.queue(response, 'response', durable: false, auto_delete: true, routing_key: response)
    queue.subscribe do |raw|
      message = Slnky::Message.parse(raw)
      level = message.level.to_sym
      if level == :complete
        tx.stop!
      elsif level == :start
        # start tracking responders?
      else
        out message.level, message.message, message.service
      end
    end

    EventMachine.add_periodic_timer(timeout) do
      out :error, "timed out after #{timeout} seconds"
      tx.stop!('Timed out')
    end

    Slnky.notify(msg)
  end
end

#executeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/slnky/cli/command.rb', line 17

def execute
  @name = service
  Slnky::Config.configure(@name, environment: environment)
  data = {
      name: "slnky.#{service}.command",
      command: service == 'help' ? nil : command,
      args: args,
      response: "command-#{$$}",
  }
  msg = Slnky::Message.new(data)
  puts JSON.pretty_generate(msg.to_h) if dry_run?
  amqp(msg) unless dry_run?
end

#nameObject



31
32
33
# File 'lib/slnky/cli/command.rb', line 31

def name
  @name
end

#out(level, message, service = :local) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/slnky/cli/command.rb', line 63

def out(level, message, service=:local)
  # unless @remote[service]
  #   say "<%= color('response from service: #{data.service}', BOLD) %>"
  #   @first = true
  # end
  # color = level.to_s == 'info' ? 'GREEN' : 'RED'
  # say "<%= color(\"#{service}\", GRAY) %> <%= color(\"#{message}\", #{color}) %>"
  lines = message.split("\n")
  lines.each do |line|
    puts "#{service} [#{level}] #{line}"
  end
end