Moved to deamon support.
This commit is contained in:
parent
8245109197
commit
766655b52a
4 changed files with 86 additions and 33 deletions
|
@ -104,12 +104,6 @@ module Session
|
||||||
|
|
||||||
def self.jobs
|
def self.jobs
|
||||||
|
|
||||||
# create pid file
|
|
||||||
$daemon_pid = File.new( @pid.to_s,"w" )
|
|
||||||
$daemon_pid.sync = true
|
|
||||||
$daemon_pid.puts(Process.pid.to_s)
|
|
||||||
$daemon_pid.close
|
|
||||||
|
|
||||||
# just make sure that spool path exists
|
# just make sure that spool path exists
|
||||||
if !File::exists?( @path )
|
if !File::exists?( @path )
|
||||||
FileUtils.mkpath @path
|
FileUtils.mkpath @path
|
||||||
|
|
|
@ -38,15 +38,15 @@ else
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
rvm use ruby-1.9.3-p286
|
rvm use ruby-1.9.3
|
||||||
|
|
||||||
RUBY=$(which ruby)
|
RUBY=$(which ruby)
|
||||||
RAILS=$(which rails)
|
RAILS=$(which rails)
|
||||||
BUNDLE=$(which rails)
|
BUNDLE=$(which bundle)
|
||||||
|
|
||||||
APP_PIDFILE=$DAEMON_HOME/tmp/pids/server.pid
|
APP_PIDFILE=$DAEMON_HOME/tmp/pids/thin.pid
|
||||||
WS_PIDFILE=$DAEMON_HOME/tmp/pids/websocket.pid
|
WS_PIDFILE=$DAEMON_HOME/tmp/pids/websocket.pid
|
||||||
WORKER_PIDFILE=$DAEMON_HOME/tmp/pids/sessionworker.pid
|
SCHEDULER_PIDFILE=$DAEMON_HOME/tmp/pids/scheduler_runner.pid
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
|
|
||||||
|
@ -55,37 +55,38 @@ start() {
|
||||||
# precompile assets
|
# precompile assets
|
||||||
if [ "$RAILS_ENV" = "production" ]; then
|
if [ "$RAILS_ENV" = "production" ]; then
|
||||||
echo -n $"Precompile assets for ${NAME}/${RAILS_ENV}"
|
echo -n $"Precompile assets for ${NAME}/${RAILS_ENV}"
|
||||||
$BUNDLE exec rake assets:precompile && echo_success || echo_failure
|
$BUNDLE exec rake assets:precompile &> /dev/null && echo_success || echo_failure
|
||||||
echo
|
echo
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# start web server
|
# start web server
|
||||||
echo -n $"Starting ${NAME}/${RAILS_ENV} application server on port: ${APP_PORT}"
|
echo -n $"Starting ${NAME}/${RAILS_ENV} application server on port: ${APP_PORT}"
|
||||||
$RAILS server -d -p $APP_PORT --pid $APP_PIDFILE &> /dev/null
|
# $RAILS server -d -p $APP_PORT --pid $APP_PIDFILE &> /dev/null
|
||||||
|
thin start --threaded -d -p $APP_PORT --pid $APP_PIDFILE
|
||||||
sleep 2
|
sleep 2
|
||||||
status -p $APP_PIDFILE &> /dev/null && echo_success || echo_failure
|
status -p $APP_PIDFILE &> /dev/null && echo_success || echo_failure
|
||||||
echo
|
echo
|
||||||
|
|
||||||
# start websocket server
|
# start websocket server
|
||||||
echo -n $"Starting ${NAME}/${RAILS_ENV} web socket server on port: ${WS_PORT}"
|
echo -n $"Starting ${NAME}/${RAILS_ENV} web socket server on port: ${WS_PORT}"
|
||||||
nohup $RUBY script/websocket-server.rb -p $WS_PORT -i $WS_PIDFILE &> /dev/null &
|
script/websocket-server.rb start -d -p $WS_PORT -i $WS_PIDFILE &> /dev/null &
|
||||||
sleep 2
|
sleep 2
|
||||||
status -p $WS_PIDFILE &> /dev/null && echo_success || echo_failure
|
status -p $WS_PIDFILE &> /dev/null && echo_success || echo_failure
|
||||||
echo
|
echo
|
||||||
|
|
||||||
# start session worker
|
# start scheduler worker
|
||||||
echo -n $"Starting ${NAME}/${RAILS_ENV} session worker"
|
echo -n $"Starting ${NAME}/${RAILS_ENV} scheduler worker"
|
||||||
nohup $RAILS runner 'Session.jobs' &> /dev/null &
|
script/scheduler.rb start
|
||||||
sleep 10
|
sleep 10
|
||||||
status -p $WORKER_PIDFILE &> /dev/null && echo_success || echo_failure
|
status -p $SCHEDULER_PIDFILE &> /dev/null && echo_success || echo_failure
|
||||||
echo
|
echo
|
||||||
}
|
}
|
||||||
|
|
||||||
stop() {
|
stop() {
|
||||||
# stop session worker
|
# stop scheduler worker
|
||||||
echo -n $"Shutting down ${NAME}/${RAILS_ENV} session worker"
|
echo -n $"Shutting down ${NAME}/${RAILS_ENV} scheduler worker"
|
||||||
if test -e $WORKER_PIDFILE; then
|
if test -e $SCHEDULER_PIDFILE; then
|
||||||
killproc -p $WORKER_PIDFILE
|
killproc -p $SCHEDULER_PIDFILE
|
||||||
fi
|
fi
|
||||||
echo
|
echo
|
||||||
|
|
||||||
|
@ -119,7 +120,7 @@ case "$1" in
|
||||||
status)
|
status)
|
||||||
status -p $APP_PIDFILE "${NAME} ${RAILS_ENV} - application server"
|
status -p $APP_PIDFILE "${NAME} ${RAILS_ENV} - application server"
|
||||||
status -p $WS_PIDFILE "${NAME} ${RAILS_ENV} - web socket server"
|
status -p $WS_PIDFILE "${NAME} ${RAILS_ENV} - web socket server"
|
||||||
status -p $WORKER_PIDFILE "${NAME} ${RAILS_ENV} - session worker"
|
status -p $SCHEDULER_PIDFILE "${NAME} ${RAILS_ENV} - scheduler worker"
|
||||||
;;
|
;;
|
||||||
|
|
||||||
*)
|
*)
|
||||||
|
|
31
script/scheduler.rb
Executable file
31
script/scheduler.rb
Executable file
|
@ -0,0 +1,31 @@
|
||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
$LOAD_PATH << './lib'
|
||||||
|
require 'rubygems'
|
||||||
|
require 'daemons'
|
||||||
|
dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
||||||
|
|
||||||
|
daemon_options = {
|
||||||
|
:multiple => false,
|
||||||
|
:dir_mode => :normal,
|
||||||
|
:dir => File.join(dir, 'tmp', 'pids'),
|
||||||
|
:backtrace => true
|
||||||
|
}
|
||||||
|
|
||||||
|
Daemons.run_proc('scheduler_runner', daemon_options) do
|
||||||
|
if ARGV.include?('--')
|
||||||
|
ARGV.slice! 0..ARGV.index('--')
|
||||||
|
else
|
||||||
|
ARGV.clear
|
||||||
|
end
|
||||||
|
|
||||||
|
Dir.chdir dir
|
||||||
|
RAILS_ENV = ARGV.first || ENV['RAILS_ENV'] || 'development'
|
||||||
|
|
||||||
|
$stdout.reopen( dir + "/log/scheduler_out.log", "w")
|
||||||
|
$stderr.reopen( dir + "/log/scheduler_err.log", "w")
|
||||||
|
require File.join(dir, "config", "environment")
|
||||||
|
require 'scheduler'
|
||||||
|
|
||||||
|
Scheduler.run
|
||||||
|
end
|
33
script/websocket-server.rb
Normal file → Executable file
33
script/websocket-server.rb
Normal file → Executable file
|
@ -1,3 +1,5 @@
|
||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
$LOAD_PATH << './lib'
|
$LOAD_PATH << './lib'
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'eventmachine'
|
require 'eventmachine'
|
||||||
|
@ -6,24 +8,34 @@ require 'json'
|
||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
require 'session'
|
require 'session'
|
||||||
require 'optparse'
|
require 'optparse'
|
||||||
|
require 'daemons'
|
||||||
|
|
||||||
# Look for -o with argument, and -I and -D boolean arguments
|
# Look for -o with argument, and -I and -D boolean arguments
|
||||||
@options = {
|
@options = {
|
||||||
:p => 6042,
|
:p => 6042,
|
||||||
:b => '0.0.0.0',
|
:b => '0.0.0.0',
|
||||||
:s => false,
|
:s => false,
|
||||||
|
:v => false,
|
||||||
:d => false,
|
:d => false,
|
||||||
:k => '/path/to/server.key',
|
:k => '/path/to/server.key',
|
||||||
:c => '/path/to/server.crt',
|
:c => '/path/to/server.crt',
|
||||||
:i => Dir.pwd.to_s + '/tmp/pids/websocket.pid'
|
:i => Dir.pwd.to_s + '/tmp/pids/websocket.pid'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ARGV[0] != 'start' && ARGV[0] != 'stop'
|
||||||
|
puts "Usage: websocket-server.rb start|stop [options]"
|
||||||
|
exit;
|
||||||
|
end
|
||||||
tls_options = {}
|
tls_options = {}
|
||||||
OptionParser.new do |opts|
|
OptionParser.new do |opts|
|
||||||
opts.banner = "Usage: websocket-server.rb [options]"
|
opts.banner = "Usage: websocket-server.rb start|stop [options]"
|
||||||
|
|
||||||
opts.on("-d", "--debug", "enable debug messages") do |d|
|
opts.on("-d", "--daemon", "start as daemon") do |d|
|
||||||
@options[:d] = d
|
@options[:d] = d
|
||||||
end
|
end
|
||||||
|
opts.on("-v", "--verbose", "enable debug messages") do |d|
|
||||||
|
@options[:v] = d
|
||||||
|
end
|
||||||
opts.on("-p", "--port [OPT]", "port of websocket server") do |p|
|
opts.on("-p", "--port [OPT]", "port of websocket server") do |p|
|
||||||
@options[:p] = p
|
@options[:p] = p
|
||||||
end
|
end
|
||||||
|
@ -47,11 +59,26 @@ end.parse!
|
||||||
puts "Starting websocket server on #{ @options[:b] }:#{ @options[:p] } (secure:#{ @options[:s].to_s },pid:#{@options[:i].to_s})"
|
puts "Starting websocket server on #{ @options[:b] }:#{ @options[:p] } (secure:#{ @options[:s].to_s },pid:#{@options[:i].to_s})"
|
||||||
#puts options.inspect
|
#puts options.inspect
|
||||||
|
|
||||||
|
if ARGV[0] == 'stop'
|
||||||
|
|
||||||
|
# read pid
|
||||||
|
pid =File.open( @options[:i].to_s ).read
|
||||||
|
pid.gsub!(/\r|\n/, "")
|
||||||
|
|
||||||
|
# kill
|
||||||
|
Process.kill( 9, pid.to_i )
|
||||||
|
exit
|
||||||
|
end
|
||||||
|
if ARGV[0] == 'start' && @options[:d]
|
||||||
|
|
||||||
|
Daemons.daemonize
|
||||||
|
|
||||||
# create pid file
|
# create pid file
|
||||||
$daemon_pid = File.new( @options[:i].to_s,"w" )
|
$daemon_pid = File.new( @options[:i].to_s,"w" )
|
||||||
$daemon_pid.sync = true
|
$daemon_pid.sync = true
|
||||||
$daemon_pid.puts(Process.pid.to_s)
|
$daemon_pid.puts(Process.pid.to_s)
|
||||||
$daemon_pid.close
|
$daemon_pid.close
|
||||||
|
end
|
||||||
|
|
||||||
@clients = {}
|
@clients = {}
|
||||||
@spool = []
|
@spool = []
|
||||||
|
@ -277,7 +304,7 @@ EventMachine.run {
|
||||||
end
|
end
|
||||||
|
|
||||||
def log( level, data, client_id = '-' )
|
def log( level, data, client_id = '-' )
|
||||||
if !@options[:d]
|
if !@options[:v]
|
||||||
return if level == 'debug'
|
return if level == 'debug'
|
||||||
end
|
end
|
||||||
puts "#{Time.now}:client(#{ client_id }) #{ data }"
|
puts "#{Time.now}:client(#{ client_id }) #{ data }"
|
||||||
|
|
Loading…
Reference in a new issue