Bot controller
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
# 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
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
class ParseQueue
|
||||
include Mongoid::Document
|
||||
store_in :bot_parse_queue
|
||||
|
||||
key :track_id, Integer
|
||||
key :artist_id, Integer
|
||||
key :artist_name, String
|
||||
key :track_name, String
|
||||
key :bot_name, String
|
||||
key :status, Integer
|
||||
key :date, Array
|
||||
key :times_failed, Integer
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user