Skip to content

Commit

Permalink
Maintenance: Add possibility to log websocket-server messages to files.
Browse files Browse the repository at this point in the history
  • Loading branch information
fliebe92 committed Mar 27, 2023
1 parent 8a873d0 commit f208e03
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions script/websocket-server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
require 'optparse'
require 'daemons'

def before_fork
def before_start
# remember open file handles
@files_to_reopen = []
ObjectSpace.each_object(File) do |file|
@files_to_reopen << file if !file.closed?
end
end

def after_fork(dir)
def after_start(dir)
Dir.chdir dir

# Re-open file handles
Expand All @@ -36,7 +36,7 @@ def after_fork(dir)
$stderr.reopen("#{dir}/log/websocket-server_err.log", 'w').sync = true
end

before_fork
before_start

# Look for -o with argument, and -I and -D boolean arguments
@options = {
Expand Down Expand Up @@ -83,6 +83,9 @@ def after_fork(dir)
@options[:tls_options] ||= {}
@options[:tls_options][:cert_chain_file] = c
end
opts.on('-l', '--to-logfile', 'enable logging to files') do |l|
@options[:logfile] = l
end
end.parse!

if ARGV[0] != 'start' && ARGV[0] != 'stop'
Expand All @@ -103,17 +106,21 @@ def after_fork(dir)
exit
end

if ARGV[0] == 'start' && @options[:d]
puts "Starting websocket server on #{@options[:b]}:#{@options[:p]} (secure: #{@options[:s]}, pidfile: #{@options[:i]})"
if ARGV[0] == 'start'
if @options[:d]
puts "Starting websocket server on #{@options[:b]}:#{@options[:p]} (secure: #{@options[:s]}, pidfile: #{@options[:i]})"

# Use Daemons.rb's built-in facility for generating PID files
Daemons.daemonize(
app_name: File.basename(@options[:i], '.pid'),
dir_mode: :normal,
dir: File.dirname(@options[:i])
)
# Use Daemons.rb's built-in facility for generating PID files
Daemons.daemonize(
app_name: File.basename(@options[:i], '.pid'),
dir_mode: :normal,
dir: File.dirname(@options[:i])
)
end

after_fork(dir)
if @options[:d] || @options[:logfile]
after_start(dir)
end
end

WebsocketServer.run(@options)

0 comments on commit f208e03

Please sign in to comment.