Bot controller
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
class BotController < ApplicationController
|
||||
require 'daemons'
|
||||
|
||||
PIDS_PATH = "#{Rails.root.to_s}/tmp/pids"
|
||||
|
||||
@@accounts = YAML.load_file("#{Rails.root.to_s}/config/vk_accounts.yml")
|
||||
@error = ""
|
||||
|
||||
def list
|
||||
@bots = []
|
||||
@@accounts.each do |bot_name, data|
|
||||
bot = Bot.new(bot_name)
|
||||
@bots << {
|
||||
:name => bot_name,
|
||||
:started => bot.running?,
|
||||
:current_track => bot.getCurrentTrack,
|
||||
:started_at => bot.running? ? bot.startedAt : nil
|
||||
}
|
||||
end
|
||||
pp @bots
|
||||
end
|
||||
|
||||
def stats
|
||||
end
|
||||
|
||||
|
||||
def queue
|
||||
bot_name = params['bot_name']
|
||||
group_by_artist = params['group_by_artist']
|
||||
limit = params['limit']
|
||||
|
||||
if not bot_name.nil?
|
||||
if not @@accounts.include?(bot_name)
|
||||
throw "Wrong bot name."
|
||||
end
|
||||
end
|
||||
if not group_by_artist.nil?
|
||||
group_by_artist = true
|
||||
end
|
||||
if params['limit'].to_i != 0
|
||||
limit = params['limit'].to_i
|
||||
end
|
||||
|
||||
statuses = [0, 1, 4]
|
||||
data = ParseQueue.any_in(status: statuses)
|
||||
if not bot_name.nil?
|
||||
data = data.where(:bot_name => bot_name)
|
||||
end
|
||||
# if group_by_artist
|
||||
# data = data.only(:artist_name).group
|
||||
# end
|
||||
if not limit.nil?
|
||||
data = data.limit(limit)
|
||||
end
|
||||
@queue = []
|
||||
unless data.nil?
|
||||
data.each do |track|
|
||||
@queue << track
|
||||
end
|
||||
end
|
||||
pp @queue
|
||||
end
|
||||
|
||||
def start
|
||||
name = params['name']
|
||||
begin
|
||||
if name.nil? or name.strip.empty?
|
||||
throw "Empty bot name"
|
||||
end
|
||||
|
||||
bot = Bot.new(name)
|
||||
bot.start
|
||||
rescue
|
||||
return render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
|
||||
end
|
||||
end
|
||||
|
||||
def stop
|
||||
name = params['name']
|
||||
begin
|
||||
if name.nil? or name.strip.empty?
|
||||
throw "Empty bot name"
|
||||
end
|
||||
bot = Bot.new(name)
|
||||
bot.stop
|
||||
rescue
|
||||
return render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
|
||||
end
|
||||
end
|
||||
|
||||
# Add artist to parse_queue
|
||||
def add
|
||||
name = params['name']
|
||||
if name.nil? or name.strip.empty?
|
||||
redirect_to '/'
|
||||
end
|
||||
|
||||
artist = Artist.getByName(name)
|
||||
if artist.nil?
|
||||
@error = "There is no such artist."
|
||||
else
|
||||
data = ParseQueue.where(:artist_id => artist.id).first
|
||||
if not data.nil?
|
||||
throw "This artist already exist in queue."
|
||||
else
|
||||
tracks = []
|
||||
artist.albums.each do |album|
|
||||
unless album.releases.empty?
|
||||
album.releases.first.tracks.each do |track|
|
||||
tracks << track
|
||||
end
|
||||
end
|
||||
end
|
||||
tracks.each do |track|
|
||||
ParseQueue.collection.insert({
|
||||
:track_id => track.id,
|
||||
:artist_id => artist.id,
|
||||
:artist_name => artist.name,
|
||||
:track_name => track.name,
|
||||
:bot_name => nil,
|
||||
:status => 0,
|
||||
:date => nil,
|
||||
:times_failed => 0
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
<h1>Bot#list</h1>
|
||||
<p>Find me in app/views/bot/list.html.erb</p>
|
||||
@@ -0,0 +1,2 @@
|
||||
<h1>Bot#stats</h1>
|
||||
<p>Find me in app/views/bot/stats.html.erb</p>
|
||||
Reference in New Issue
Block a user