websocket-server.rb 3.3 KB

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