sudo yum install -y monit
config: https://www.igvita.com/2006/11/07/monit-makes-mongrel-play-nice/
run /usr/local/bin/monit -g composer [stop|start|restart] all
namespace :app do | |
desc "排版工作" | |
task :composer, [:start, :end] => :environment do |task, args| | |
keep_loop = true | |
puts "rake app:composer run. I have PID #{Process.pid}" | |
`echo "#{Process.pid}" > #{Rails.root}/tmp/composer.pid` | |
`echo "#{Process.ppid}" > #{Rails.root}/tmp/composer.ppid` | |
def shut_down | |
puts "\nShutting down gracefully..." | |
keep_loop = false | |
end | |
# Trap ^C | |
Signal.trap("INT") { | |
shut_down | |
exit | |
} | |
# Trap `Kill ` | |
Signal.trap("TERM") { | |
shut_down | |
exit | |
} | |
while keep_loop | |
puts "\nbegin compose at #{Time.now}" | |
begin | |
check_and_compose | |
rescue => e | |
puts e.message | |
end | |
sleep 10 #seconds | |
end | |
end # task :composer | |
def check_and_compose | |
driver = Selenium::WebDriver.for(:remote, :url => Rails.configuration.hejiao.web_driver) | |
# set window size using Dimension struct | |
target_size = Selenium::WebDriver::Dimension.new(1600, 1000) | |
driver.manage.window.size = target_size | |
questions = Question.where(need_compose: true).order("id asc") | |
puts "#{questions.count} questions need compose" | |
questions.each do |q| | |
url = q.compose_url | |
puts "navigate to #{url}" | |
driver.navigate.to url | |
max_sleep_seconds = 100 | |
current_sleep_seconds = 0 | |
per_sleep_seconds = 5 | |
keep_waiting = true | |
while keep_waiting | |
sleep per_sleep_seconds | |
current_sleep_seconds += per_sleep_seconds | |
question = Question.find(q.id) | |
if !question.need_compose | |
keep_waiting = false | |
else | |
keep_waiting = max_sleep_seconds > current_sleep_seconds | |
puts "current_sleep_seconds #{current_sleep_seconds}, keep_waiting #{keep_waiting}" | |
end | |
end | |
# save screenshot here for test | |
# filename = "/tmp/screenshots/#{q.id}_#{q.axx_id}.png" | |
# puts "save_screenshot to #{filename}" | |
# driver.save_screenshot(filename) | |
end | |
end | |
end |
#!/bin/bash | |
export PATH=$PATH:/home/gsreader/.rvm/rubies/ruby-2.3.3/bin/ | |
case $1 in | |
start) | |
cd /home/gsreader/site/courseware/current; | |
echo $$ > tmp/composer_master.pid | |
rake app:composer RAILS_ENV=production | |
;; | |
stop) | |
cd /home/gsreader/site/courseware/current; | |
kill `cat /tmp/composer.ppid` | |
kill `cat /tmp/composer.pid` | |
kill `cat /tmp/composer_master.pid` | |
rm -rf tmp/composer.pid | |
;; | |
*) | |
echo "usage: composer {start|stop}" ;; | |
esac | |
exit 0 |
sudo yum install -y monit
config: https://www.igvita.com/2006/11/07/monit-makes-mongrel-play-nice/
run /usr/local/bin/monit -g composer [stop|start|restart] all
check process composer with pidfile /home/gsreader/site/courseware/current/tmp/composer_master.pid | |
start program = "/home/gsreader/site/courseware/current/config/composer start" | |
stop program = "/home/gsreader/site/courseware/current/config/composer stop" |