Class: Ruboty::ExecCommand::CommandSlot
- Inherits:
-
Object
- Object
- Ruboty::ExecCommand::CommandSlot
- Defined in:
- lib/ruboty/exec_command/command_slot.rb
Instance Method Summary collapse
- #command_in_list(idx_or_pid) ⇒ Object
- #forget(pid) ⇒ Object
-
#initialize ⇒ CommandSlot
constructor
A new instance of CommandSlot.
- #kill(idx_or_pid) ⇒ Object
- #list_commands ⇒ Object
- #remember(comm) ⇒ Object
- #run(command) ⇒ Object
- #running_commands ⇒ Object
Constructor Details
#initialize ⇒ CommandSlot
Returns a new instance of CommandSlot.
5 6 7 |
# File 'lib/ruboty/exec_command/command_slot.rb', line 5 def initialize @commands = [] end |
Instance Method Details
#command_in_list(idx_or_pid) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ruboty/exec_command/command_slot.rb', line 27 def command_in_list(idx_or_pid) found = @commands.index { |c| c.pid == idx_or_pid } if found.nil? i = idx_or_pid.to_i num_commands = @commands.size if i <= 0 or i > num_commands nil else @commands[i-1] end else @commands[found] end end |
#forget(pid) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/ruboty/exec_command/command_slot.rb', line 20 def forget(pid) # remove thread object @commands.delete_if do |c| c.pid == pid end end |
#kill(idx_or_pid) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/ruboty/exec_command/command_slot.rb', line 60 def kill(idx_or_pid) command = command_in_list(idx_or_pid) unless command.nil? Process.kill(-9, command.pid) # kill process group forget(command.pid) end command end |
#list_commands ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ruboty/exec_command/command_slot.rb', line 48 def list_commands if @commands.size == 0 "No command running." else number = 0 @commands.map do |c| number += 1 "#{number}: #{c.command_name} (PID[#{c.pid}], started at #{c.start_at})\n" end.join.chomp end end |
#remember(comm) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/ruboty/exec_command/command_slot.rb', line 13 def remember(comm) # remember # comm: command object # TODO: add owner info @commands << comm end |
#run(command) ⇒ Object
43 44 45 46 |
# File 'lib/ruboty/exec_command/command_slot.rb', line 43 def run(command) remember(command) command.run_bg(command.opt_args) end |
#running_commands ⇒ Object
9 10 11 |
# File 'lib/ruboty/exec_command/command_slot.rb', line 9 def running_commands @commands end |