Module: Rumpy

Defined in:
lib/rumpy.rb,
lib/rumpy/version.rb

Defined Under Namespace

Modules: Bot

Constant Summary collapse

VERSION =
'0.9.24'

Class Method Summary collapse

Class Method Details

.pid_file(bot) ⇒ Object

Determine the name of file where thid pid will stored to



46
47
48
49
50
# File 'lib/rumpy.rb', line 46

def self.pid_file(bot)
  pid_file = bot.pid_file
  pid_file = bot.class.to_s.downcase + '.pid' unless pid_file
  pid_file
end

.run(bot) ⇒ Object

Start bot without detaching



41
42
43
# File 'lib/rumpy.rb', line 41

def self.run(bot)
  bot.start
end

.start(bot) ⇒ Object

Start bot in new process, detach this process and save the pid of process in pid_file



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

def self.start(bot)
  pf = pid_file bot
  return false if File.exist? pf

  bot.log_file = "#{bot.class.to_s.downcase}.log"

  pid = fork do
    bot.start
  end
  Process.detach pid
  File.open(pf, 'w') do |file|
    file.puts pid
  end
  true
end

.stop(bot) ⇒ Object

Determine the name of pid_file, read pid from this file and try to kill process with this pid



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rumpy.rb', line 27

def self.stop(bot)
  pf = pid_file bot
  return false unless File.exist? pf
  begin
    File.open(pf) do |file|
      Process.kill :TERM, file.gets.strip.to_i
    end
  ensure
    File.unlink pf
  end
  true
end