Moved to deamon support.

This commit is contained in:
Martin Edenhofer 2013-03-11 00:14:31 +01:00
parent 8245109197
commit 766655b52a
4 changed files with 86 additions and 33 deletions

View file

@ -104,12 +104,6 @@ module Session
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
if !File::exists?( @path )
FileUtils.mkpath @path
@ -354,13 +348,13 @@ class UserState
# recent viewed
cache_key = @cache_key + '_recent_viewed'
if CacheIn.expired(cache_key)
recent_viewed = History.recent_viewed(user)
recent_viewed = History.recent_viewed( user )
recent_viewed_cache = CacheIn.get( cache_key, { :re_expire => true } )
self.log 'notice', 'fetch recent_viewed - ' + cache_key
if recent_viewed != recent_viewed_cache
self.log 'notify', 'fetch recent_viewed changed - ' + cache_key
recent_viewed_full = History.recent_viewed_fulldata(user)
recent_viewed_full = History.recent_viewed_fulldata( user )
CacheIn.set( cache_key, recent_viewed, { :expires_in => 5.seconds } )
CacheIn.set( cache_key + '_push', recent_viewed_full )
end

View file

@ -22,7 +22,7 @@ APP_PORT=3000
WS_PORT=6042
ZAMMAD_CONFIG=/etc/sysconfig/zammad
# Read config
# Read config
[ -f "$ZAMMAD_CONFIG" ] && . "$ZAMMAD_CONFIG"
@ -38,15 +38,15 @@ else
exit 1
fi
rvm use ruby-1.9.3-p286
rvm use ruby-1.9.3
RUBY=$(which ruby)
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
WORKER_PIDFILE=$DAEMON_HOME/tmp/pids/sessionworker.pid
SCHEDULER_PIDFILE=$DAEMON_HOME/tmp/pids/scheduler_runner.pid
start() {
@ -55,37 +55,38 @@ start() {
# precompile assets
if [ "$RAILS_ENV" = "production" ]; then
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
fi
# start web server
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
status -p $APP_PIDFILE &> /dev/null && echo_success || echo_failure
echo
# start websocket server
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
status -p $WS_PIDFILE &> /dev/null && echo_success || echo_failure
echo
# start session worker
echo -n $"Starting ${NAME}/${RAILS_ENV} session worker"
nohup $RAILS runner 'Session.jobs' &> /dev/null &
# start scheduler worker
echo -n $"Starting ${NAME}/${RAILS_ENV} scheduler worker"
script/scheduler.rb start
sleep 10
status -p $WORKER_PIDFILE &> /dev/null && echo_success || echo_failure
status -p $SCHEDULER_PIDFILE &> /dev/null && echo_success || echo_failure
echo
}
stop() {
# stop session worker
echo -n $"Shutting down ${NAME}/${RAILS_ENV} session worker"
if test -e $WORKER_PIDFILE; then
killproc -p $WORKER_PIDFILE
# stop scheduler worker
echo -n $"Shutting down ${NAME}/${RAILS_ENV} scheduler worker"
if test -e $SCHEDULER_PIDFILE; then
killproc -p $SCHEDULER_PIDFILE
fi
echo
@ -119,7 +120,7 @@ case "$1" in
status)
status -p $APP_PIDFILE "${NAME} ${RAILS_ENV} - application 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
View 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

43
script/websocket-server.rb Normal file → Executable file
View file

@ -1,3 +1,5 @@
#!/usr/bin/env ruby
$LOAD_PATH << './lib'
require 'rubygems'
require 'eventmachine'
@ -6,24 +8,34 @@ require 'json'
require 'fileutils'
require 'session'
require 'optparse'
require 'daemons'
# Look for -o with argument, and -I and -D boolean arguments
@options = {
:p => 6042,
:b => '0.0.0.0',
:s => false,
:v => false,
:d => false,
:k => '/path/to/server.key',
:c => '/path/to/server.crt',
: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 = {}
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
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|
@options[:p] = p
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 options.inspect
# create pid file
$daemon_pid = File.new( @options[:i].to_s,"w" )
$daemon_pid.sync = true
$daemon_pid.puts(Process.pid.to_s)
$daemon_pid.close
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
$daemon_pid = File.new( @options[:i].to_s,"w" )
$daemon_pid.sync = true
$daemon_pid.puts(Process.pid.to_s)
$daemon_pid.close
end
@clients = {}
@spool = []
@ -277,7 +304,7 @@ EventMachine.run {
end
def log( level, data, client_id = '-' )
if !@options[:d]
if !@options[:v]
return if level == 'debug'
end
puts "#{Time.now}:client(#{ client_id }) #{ data }"