1
0
Fork 0
oldhaven/app/models/bot.rb

55 lines
1.2 KiB
Ruby

# encoding: UTF-8
class Bot < Daemons::PidFile
require "daemons"
PIDS_PATH = "#{Rails.root.to_s}/tmp/pids"
DAEMON_PATH = "#{Rails.root.to_s}/lib/daemons"
@@accounts = YAML.load_file("#{Rails.root.to_s}/config/vk_accounts.yml")
def initialize(name)
unless @@accounts.include?(name)
throw "Empty bot name."
end
@dir = File.expand_path(PIDS_PATH)
@progname = name
@multiple = false
@number = nil
end
def getCurrentTrack
current_track = ParseQueue.where(:status => 4, :bot_name => @progname).first
end
def startedAt
unless self.running?
return false
end
ts = eval('`date -d "\`ps -p ' + self.pid.to_s + ' -o lstart=\`" +"%Y-%m-%d %H:%M:%S"`')
ts = ts.strip
end
def start
if bot.running?
throw "This bot already started."
end
eval('`ruby ' + DAEMON_PATH + '/parse_controller.rb ' + Rails.root.to_s + ' ' + name + '`')
end
def stop
if not self.running?
throw "This bot already stopped."
end
signal = (RUBY_PLATFORM =~ /win32/ ? 'KILL' : 'TERM')
pid = self.pid
begin
Process.kill(signal, self.pid)
self.cleanup
rescue Errno::ESRCH => e
throw "#{e} #{pid}"
end
end
end