Class: Ruboty::ExecCommand::Actions::Command

Inherits:
Actions::Base
  • Object
show all
Defined in:
lib/ruboty/exec_command/actions/command.rb

Instance Method Summary collapse

Instance Method Details

#callObject



5
6
7
8
9
# File 'lib/ruboty/exec_command/actions/command.rb', line 5

def call
  # TODO: add timeout
  c = Ruboty::ExecCommand::Command.new(command_args: command_body)
  run_and_monitor(c)
end

#command_bodyObject



60
61
62
# File 'lib/ruboty/exec_command/actions/command.rb', line 60

def command_body
  message.body.sub(robot_prefix_pattern,'')
end

#command_slotObject



11
12
13
# File 'lib/ruboty/exec_command/actions/command.rb', line 11

def command_slot
  message.robot.brain.data[:command_slot] ||= Ruboty::ExecCommand::CommandSlot.new
end

#kill_commandObject



19
20
21
22
23
24
25
26
27
# File 'lib/ruboty/exec_command/actions/command.rb', line 19

def kill_command
  # TODO: command list lock
  # kill running process, command is "kill command <index>"
  killed = command_slot.kill(message.body.split.last.to_i)

  if killed.nil?
    message.reply("Command [#{message.body.split.last}] not found.")
  end
end

#list_commandsObject



15
16
17
# File 'lib/ruboty/exec_command/actions/command.rb', line 15

def list_commands
  message.reply(command_slot.list_commands)
end

#robot_prefix_patternObject



56
57
58
# File 'lib/ruboty/exec_command/actions/command.rb', line 56

def robot_prefix_pattern
  Ruboty::Action.prefix_pattern(message.original[:robot].name)
end

#run_and_monitor(comm) ⇒ Object



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
# File 'lib/ruboty/exec_command/actions/command.rb', line 29

def run_and_monitor(comm)
  pid = command_slot.run(comm)
  message.reply("[#{comm.command_name}] invoked.")

  # Waiter thread
  thread = Thread.new do
    ignore_pid, status = Process.wait2(pid)
    command_slot.forget(pid)

    if status.exitstatus == 0
      message.reply("[#{comm.command_name}] completed successfully.")
      message.reply(comm.stdout_log.chomp)
    elsif status.signaled?
      message.reply("[#{comm.command_name}] killed by signal #{status.termsig}")
    else
      message.reply("[#{comm.command_name}] exit status with #{status}\n" +
                  comm.stdout_log +
                  "stderr: " + comm.stderr_log.chomp
                )
    end
  end

  if ENV['RUBOTY_ENV'] == 'blocked_test'
    thread.join
  end
end