Class: Ruboty::ExecCommand::CommandSlot

Inherits:
Object
  • Object
show all
Defined in:
lib/ruboty/exec_command/command_slot.rb

Instance Method Summary collapse

Constructor Details

#initializeCommandSlot

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?
    # look for the command with index
    i = idx_or_pid.to_i
    if i <= 0 or i > @commands.size
      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



58
59
60
61
62
63
64
65
66
# File 'lib/ruboty/exec_command/command_slot.rb', line 58

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)
  else
    false
  end
end

#list_commandsObject



48
49
50
51
52
53
54
55
56
# File 'lib/ruboty/exec_command/command_slot.rb', line 48

def list_commands
  if @commands.size == 0
    "No command running."
  else
    @commands.map.with_index do |c, number|
      "#{number+1}: #{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_commandsObject



9
10
11
# File 'lib/ruboty/exec_command/command_slot.rb', line 9

def running_commands
  @commands
end