websocket-server.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env ruby
  2. # Copyright (C) 2012-2023 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_fork
  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_fork(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_fork
  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. d: false,
  38. k: '/path/to/server.key',
  39. c: '/path/to/server.crt',
  40. i: "#{dir}/tmp/pids/websocket.pid"
  41. }
  42. OptionParser.new do |opts|
  43. opts.banner = 'Usage: websocket-server.rb start|stop [options]'
  44. opts.on('-d', '--daemon', 'start as daemon') do |d|
  45. @options[:d] = d
  46. end
  47. opts.on('-v', '--verbose', 'enable debug messages') do |d|
  48. @options[:v] = d
  49. end
  50. opts.on('-p', '--port [OPT]', 'port of websocket server') do |p|
  51. @options[:p] = p
  52. end
  53. opts.on('-b', '--bind [OPT]', 'bind address') do |b|
  54. @options[:b] = IPAddr.new(b).to_s
  55. end
  56. opts.on('-s', '--secure', 'enable secure connections') do |s|
  57. @options[:s] = s
  58. end
  59. opts.on('-i', '--pid [OPT]', 'pid, default is tmp/pids/websocket.pid') do |i|
  60. @options[:i] = i
  61. end
  62. opts.on('-k', '--private-key [OPT]', '/path/to/server.key for secure connections') do |k|
  63. @options[:tls_options] ||= {}
  64. @options[:tls_options][:private_key_file] = k
  65. end
  66. opts.on('-c', '--certificate [OPT]', '/path/to/server.crt for secure connections') do |c|
  67. @options[:tls_options] ||= {}
  68. @options[:tls_options][:cert_chain_file] = c
  69. end
  70. end.parse!
  71. if ARGV[0] != 'start' && ARGV[0] != 'stop'
  72. puts "Usage: #{File.basename(__FILE__)} start|stop [options]"
  73. exit
  74. end
  75. if ARGV[0] == 'stop'
  76. pid = File.read(@options[:i]).to_i
  77. puts "Stopping websocket server (pid: #{pid})"
  78. # IMPORTANT: Use SIGTERM (15), not SIGKILL (9)
  79. # Daemons.rb cleans up the PID file automatically on termination;
  80. # SIGKILL ends the process immediately and bypasses cleanup.
  81. # See https://major.io/2010/03/18/sigterm-vs-sigkill/ for more.
  82. Process.kill(:SIGTERM, pid)
  83. exit
  84. end
  85. if ARGV[0] == 'start' && @options[:d]
  86. puts "Starting websocket server on #{@options[:b]}:#{@options[:p]} (secure: #{@options[:s]}, pidfile: #{@options[:i]})"
  87. # Use Daemons.rb's built-in facility for generating PID files
  88. Daemons.daemonize(
  89. app_name: File.basename(@options[:i], '.pid'),
  90. dir_mode: :normal,
  91. dir: File.dirname(@options[:i])
  92. )
  93. after_fork(dir)
  94. end
  95. WebsocketServer.run(@options)