Skip to content

Commit

Permalink
Merge branch 'f-add_auto_process_bot' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyMorugin committed Nov 24, 2015
2 parents 30b47c6 + 4837e7c commit a1c15e8
Show file tree
Hide file tree
Showing 34 changed files with 577 additions and 39 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/s1tyles.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/s1tyles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the S1tyles controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
13 changes: 13 additions & 0 deletions app/controllers/admin_pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class AdminPagesController < ApplicationController
include WorkerHelper
def main
end

Expand All @@ -8,4 +9,16 @@ def images

def users
end

def startbot
start_bot
redirect_to admin_pages_main_path
return
end

def startprocess
start_workers
redirect_to admin_pages_main_path
return
end
end
74 changes: 74 additions & 0 deletions app/controllers/contents_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class ContentsController < ApplicationController
before_action :set_content, only: [:show, :edit, :update, :destroy]

# GET /contents
# GET /contents.json
def index
@contents = Content.all.order('created_at DESC')
end

# GET /contents/1
# GET /contents/1.json
def show
end

# GET /contents/new
def new
@content = Content.new
end

# GET /contents/1/edit
def edit
end

# POST /contents
# POST /contents.json
def create
@content = Content.new(content_params)

respond_to do |format|
if @content.save
format.html { redirect_to @content, notice: 'content was successfully created.' }
format.json { render :show, status: :created, location: @content }
else
format.html { render :new }
format.json { render json: @content.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /contents/1
# PATCH/PUT /contents/1.json
def update
respond_to do |format|
if @content.update(content_params)
format.html { redirect_to @content, notice: 'content was successfully updated.' }
format.json { render :show, status: :ok, location: @content }
else
format.html { render :edit }
format.json { render json: @content.errors, status: :unprocessable_entity }
end
end
end

# DELETE /contents/1
# DELETE /contents/1.json
def destroy
@content.destroy
respond_to do |format|
format.html { redirect_to contents_url, notice: 'content was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_content
@content = Content.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def content_params
params.require(:content).permit(:image, :init, :status, :use_counter)
end
end
74 changes: 74 additions & 0 deletions app/controllers/styles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class StylesController < ApplicationController
before_action :set_style, only: [:show, :edit, :update, :destroy]

# GET /styles
# GET /styles.json
def index
@styles = Style.all.order('created_at DESC')
end

# GET /styles/1
# GET /styles/1.json
def show
end

# GET /styles/new
def new
@style = Style.new
end

# GET /styles/1/edit
def edit
end

# POST /styles
# POST /styles.json
def create
@style = Style.new(style_params)

respond_to do |format|
if @style.save
format.html { redirect_to @style, notice: 'style was successfully created.' }
format.json { render :show, status: :created, location: @style }
else
format.html { render :new }
format.json { render json: @style.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /styles/1
# PATCH/PUT /styles/1.json
def update
respond_to do |format|
if @style.update(style_params)
format.html { redirect_to @style, notice: 'style was successfully updated.' }
format.json { render :show, status: :ok, location: @style }
else
format.html { render :edit }
format.json { render json: @style.errors, status: :unprocessable_entity }
end
end
end

# DELETE /styles/1
# DELETE /styles/1.json
def destroy
@style.destroy
respond_to do |format|
format.html { redirect_to styles_url, notice: 'style was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_style
@style = Style.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def style_params
params.require(:style).permit(:image, :init, :status, :use_counter)
end
end
6 changes: 6 additions & 0 deletions app/helpers/const_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ module ConstHelper
STATUS_NOT_PROCESSED = 1
STATUS_IN_PROCESS = 2
STATUS_PROCESSED = 11
STATUS_VISIBLE_FOR_BOT = 101
##
BOT_STYLE_IMAGE = 101
BOT_CONTENT_IMAGE = 101


end
2 changes: 2 additions & 0 deletions app/helpers/styles_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module StylesHelper
end
22 changes: 22 additions & 0 deletions app/helpers/worker_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,26 @@ def start_workers
#@result = pri.execute
end
end


def start_bot
worker_name = :bot1

if true#debug
if Resque.size(worker_name) == 0
#ResqueJob.queue_class = worker_name
Resque.enqueue(BotResqueJob, worker_name)
end
#@result = Resque.size(:server1).to_s
else
img_job = ImageJob.new(:server1)
#img_job.set_config()
img_job.execute
#ResqueJob.perform(worker_name)
#pri = ImageJob.new(:s2)
#@result = pri.execute
end
end


end
83 changes: 83 additions & 0 deletions app/jobs/bot_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
class BotJob
include DebHelper
include ConstHelper
include WorkerHelper


def initialize
@worker_name = :bot1
@admin_email = "cmorugin@gmail.com"
end



def execute

log('-----------------------Start-------------------')
adm = Client.find_by(email: @admin_email)
log("Admin_id = #{adm.id}")
loop do
sleep 10
log('------Loop start-------')
if !check_idle
log("Queue busy")
next
end
ci = get_random_content
if ci.nil?
log("No content")
next
end
si = get_random_style
if si.nil?
log("No style")
next
end
log("Content: #{ci.attributes}")
log("Style: #{si.attributes}")
qi = QueueImage.new
qi.status = STATUS_NOT_PROCESSED
qi.end_status = STATUS_VISIBLE_FOR_BOT
qi.content_id = ci.id
qi.style_id = si.id

qi.client_id = adm.id
qi.save

start_workers
log("Queue: #{qi.attributes}")
log('----------Loop stop----------')
#
#break
end
end

private

def check_idle
q = QueueImage.where("status = #{STATUS_NOT_PROCESSED} or status = #{STATUS_IN_PROCESS}")
q.count == 0
end

def get_random_style
si = Style.where(status: BOT_STYLE_IMAGE)
count = si.count
return nil if count == 0
r = rand(count)
si[r]
end

def get_random_content
ci = Content.where(status: BOT_CONTENT_IMAGE)
count = ci.count
return nil if count == 0
r = rand(count)
ci[r]
end

def log(msg)
write_log(msg, @worker_name.to_s)
end


end
15 changes: 15 additions & 0 deletions app/jobs/bot_resque_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class BotResqueJob
@queue = :job1

def initialize

end

def self.perform(*arg)
#return false if arg.blank? || arg[0].blank?

bot_job = BotJob.new
bot_job.execute
end

end
27 changes: 14 additions & 13 deletions app/jobs/image_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
require 'net/scp'
class ImageJob
include DebHelper
include ConstHelper
#@queue = :error_server
#
STATUS_ERROR = -1
STATUS_DELETED = 0
STATUS_NOT_PROCESSED = 1
STATUS_IN_PROCESS = 2
STATUS_PROCESSED = 11
#STATUS_ERROR = -1
#STATUS_DELETED = 0
#STATUS_NOT_PROCESSED = 1
#STATUS_IN_PROCESS = 2
#STATUS_PROCESSED = 11
#
@hostname = "localhost"
@username = "root"
Expand Down Expand Up @@ -72,7 +73,7 @@ def execute
def get_images_from_queue
cl = Client.find_by_sql("select * from clients c where lastprocess is null and exists (select * from queue_images q where c.id = q.client_id and status = 1) order by created_at ASC")
if cl.count == 0
cl = Client.find_by_sql("select * from clients c where exists (select * from queue_images q where c.id = q.client_id and status = 1) order by lastprocess ASC")
cl = Client.find_by_sql("select * from clients c where exists (select * from queue_images q where c.id = q.client_id and status = #{STATUS_NOT_PROCESSED}) order by lastprocess ASC")
end
return nil if cl.count == 0
cl = cl.first
Expand Down Expand Up @@ -142,26 +143,26 @@ def execute_image(item)
log "-----------------------"
log "execute_image item.id = #{item.id}"
#Change status to IN_PROCESS
log "0"
#log "0"
item.update({:status => STATUS_IN_PROCESS, :stime => process_time})
# Check connection to workserver
log "1"
#log "1"
return "get_server_name: false" if get_server_name.nil?
log "2"
#log "2"
# Clear remote tmp folger
return "rm_file_on_server: false" unless rm_file_on_server
log "3"
#log "3"
#Upload images to workserver
@content_image_name = "content.#{item.content.image.to_s.split('.').last}"
@style_image_name = "style.#{item.style.image.to_s.split('.').last}"
log "4"
#log "4"
return "upload_content_image: false" unless upload_image(item.content.image.to_proc.url, "output/#{@content_image_name}")
return "upload_stule_image: false" unless upload_image(item.style.image, "output/#{@style_image_name}")
log "5"
#log "5"
#Run process
send_start_process_comm()
sleep 10
log "6"
#log "6"
# Wait processed images
errors = wait_images(item)
#
Expand Down
7 changes: 5 additions & 2 deletions app/views/admin_pages/main.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
<h1>AdminPages#main</h1>
<p>Find me in app/views/admin_pages/main.html.erb</p>
<p><%= link_to 'Очередь ', admin_pages_images_path %><p/>
<p><%= link_to 'Contents', contents_path %><p/>
<p><%= link_to 'Styles', styles_path %><p/>
<p><%= link_to 'Start bot', admin_pages_startbot_path %><p/>
<p><%= link_to 'Start process', admin_pages_startprocess_path %><p/>
Loading

0 comments on commit a1c15e8

Please sign in to comment.