From 65b01371df290882d2cee5585b2b13cc744850fc Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 8 May 2017 16:05:38 +0200 Subject: [PATCH] Improved scheduler and websocket server logging. --- script/scheduler.rb | 31 +++++++++++++++++++++----- script/websocket-server.rb | 45 +++++++++++++++++++++++++++----------- 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/script/scheduler.rb b/script/scheduler.rb index 8ea47d270..5ddfc90f5 100755 --- a/script/scheduler.rb +++ b/script/scheduler.rb @@ -11,11 +11,33 @@ RAILS_ENV = ENV['RAILS_ENV'] || 'development' require 'rails/all' require 'bundler' -Bundler.require(:default, Rails.env) require File.join(dir, 'config', 'environment') - require 'daemons' +def before_fork + + # remember open file handles + @files_to_reopen = [] + ObjectSpace.each_object(File) do |file| + @files_to_reopen << file unless file.closed? + end +end + +def after_fork(dir) + Dir.chdir dir + + # Re-open file handles + @files_to_reopen.each do |file| + file.reopen file.path, 'a+' + file.sync = true + end + + $stdout.reopen( "#{dir}/log/scheduler_out.log", 'w') + $stderr.reopen( "#{dir}/log/scheduler_err.log", 'w') +end + +before_fork + daemon_options = { multiple: false, dir_mode: :normal, @@ -31,10 +53,7 @@ Daemons.run_proc(name, daemon_options) do ARGV.clear end - Dir.chdir dir - - $stdout.reopen( dir + '/log/' + name + '_out.log', 'w') - $stderr.reopen( dir + '/log/' + name + '_err.log', 'w') + after_fork(dir) require 'scheduler' Scheduler.threads diff --git a/script/websocket-server.rb b/script/websocket-server.rb index e72c5d994..740d3af39 100755 --- a/script/websocket-server.rb +++ b/script/websocket-server.rb @@ -3,12 +3,6 @@ $LOAD_PATH << './lib' require 'rubygems' -require 'eventmachine' -require 'em-websocket' -require 'json' -require 'fileutils' -require 'optparse' -require 'daemons' # load rails env dir = File.expand_path(File.join(File.dirname(__FILE__), '..')) @@ -17,11 +11,39 @@ RAILS_ENV = ENV['RAILS_ENV'] || 'development' require 'rails/all' require 'bundler' -Bundler.require(:default, Rails.env) require File.join(dir, 'config', 'environment') - +require 'eventmachine' +require 'em-websocket' +require 'json' +require 'fileutils' +require 'optparse' +require 'daemons' require 'sessions' +def before_fork + + # remember open file handles + @files_to_reopen = [] + ObjectSpace.each_object(File) do |file| + @files_to_reopen << file unless file.closed? + end +end + +def after_fork(dir) + Dir.chdir dir + + # Re-open file handles + @files_to_reopen.each do |file| + file.reopen file.path, 'a+' + file.sync = true + end + + $stdout.reopen( "#{dir}/log/websocket-server_out.log", 'w') + $stderr.reopen( "#{dir}/log/websocket-server_err.log", 'w') +end + +before_fork + # Look for -o with argument, and -I and -D boolean arguments @options = { p: 6042, @@ -86,13 +108,10 @@ if ARGV[0] == 'start' && @options[:d] Daemons.daemonize - Dir.chdir dir - name = 'websocket-server' - $stdout.reopen( dir + '/log/' + name + '_out.log', 'w') - $stderr.reopen( dir + '/log/' + name + '_err.log', 'w') + after_fork(dir) # 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.puts(Process.pid.to_s) daemon_pid.close