websocket-server.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env ruby
  2. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  3. dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
  4. Dir.chdir dir
  5. require 'bundler'
  6. require File.join(dir, 'config', 'environment')
  7. require 'eventmachine'
  8. require 'em-websocket'
  9. require 'json'
  10. require 'fileutils'
  11. require 'optparse'
  12. require 'daemons'
  13. def before_start
  14. # remember open file handles
  15. @files_to_reopen = []
  16. ObjectSpace.each_object(File) do |file|
  17. @files_to_reopen << file if !file.closed?
  18. end
  19. end
  20. def after_start(dir)
  21. Dir.chdir dir
  22. # Re-open file handles
  23. @files_to_reopen.each do |file|
  24. file.reopen file.path, 'a+'
  25. file.sync = true
  26. end
  27. $stdout.reopen("#{dir}/log/websocket-server_out.log", 'w').sync = true
  28. $stderr.reopen("#{dir}/log/websocket-server_err.log", 'w').sync = true
  29. end
  30. before_start
  31. # Look for -o with argument, and -I and -D boolean arguments
  32. @options = {
  33. p: 6042,
  34. b: '0.0.0.0',
  35. s: false,
  36. v: false,
  37. n: false,
  38. d: false,
  39. k: '/path/to/server.key',
  40. c: '/path/to/server.crt',
  41. i: "#{dir}/tmp/pids/websocket.pid"
  42. }
  43. OptionParser.new do |opts|
  44. opts.banner = 'Usage: websocket-server.rb start|stop [options]'
  45. opts.on('-d', '--daemon', 'start as daemon') do |d|
  46. @options[:d] = d
  47. end
  48. opts.on('-v', '--verbose', 'enable debug messages') do |v|
  49. @options[:v] = v
  50. end
  51. opts.on('-n', '--info', 'enable info messages') do |n|
  52. @options[:n] = n
  53. end
  54. opts.on('-p', '--port [OPT]', 'port of websocket server') do |p|
  55. @options[:p] = p
  56. end
  57. opts.on('-b', '--bind [OPT]', 'bind address') do |b|
  58. @options[:b] = IPAddr.new(b).to_s
  59. end
  60. opts.on('-s', '--secure', 'enable secure connections') do |s|
  61. @options[:s] = s
  62. end
  63. opts.on('-i', '--pid [OPT]', 'pid, default is tmp/pids/websocket.pid') do |i|
  64. @options[:i] = i
  65. end
  66. opts.on('-k', '--private-key [OPT]', '/path/to/server.key for secure connections') do |k|
  67. @options[:tls_options] ||= {}
  68. @options[:tls_options][:private_key_file] = k
  69. end
  70. opts.on('-c', '--certificate [OPT]', '/path/to/server.crt for secure connections') do |c|
  71. @options[:tls_options] ||= {}
  72. @options[:tls_options][:cert_chain_file] = c
  73. end
  74. opts.on('-l', '--to-logfile', 'enable logging to files') do |l|
  75. @options[:logfile] = l
  76. end
  77. end.parse!
  78. if ARGV[0] != 'start' && ARGV[0] != 'stop'
  79. puts "Usage: #{File.basename(__FILE__)} start|stop [options]"
  80. exit
  81. end
  82. if ARGV[0] == 'stop'
  83. pid = File.read(@options[:i]).to_i
  84. puts "Stopping websocket server (pid: #{pid})"
  85. # IMPORTANT: Use SIGTERM (15), not SIGKILL (9)
  86. # Daemons.rb cleans up the PID file automatically on termination;
  87. # SIGKILL ends the process immediately and bypasses cleanup.
  88. # See https://major.io/2010/03/18/sigterm-vs-sigkill/ for more.
  89. Process.kill(:SIGTERM, pid)
  90. exit
  91. end
  92. if ARGV[0] == 'start'
  93. if @options[:d]
  94. puts "Starting websocket server on #{@options[:b]}:#{@options[:p]} (secure: #{@options[:s]}, pidfile: #{@options[:i]})"
  95. # Use Daemons.rb's built-in facility for generating PID files
  96. Daemons.daemonize(
  97. app_name: File.basename(@options[:i], '.pid'),
  98. dir_mode: :normal,
  99. dir: File.dirname(@options[:i])
  100. )
  101. end
  102. if @options[:d] || @options[:logfile]
  103. after_start(dir)
  104. end
  105. end
  106. if %w[1 true].include? ENV['WEBSOCKET_SERVER_LOG_TO_STDOUT']
  107. Zammad::Logging.extend_logging_to_stdout
  108. elsif Rails.env.development?
  109. puts 'WebsocketServer do not log to STDOUT. You can enable this by setting WEBSOCKET_SERVER_LOG_TO_STDOUT=1.'
  110. end
  111. WebsocketServer.run(@options)