scheduler.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.include?('spring')
  7. end
  8. dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
  9. Dir.chdir dir
  10. require 'bundler'
  11. require 'daemons'
  12. def before_fork
  13. # remember open file handles
  14. @files_to_reopen = []
  15. ObjectSpace.each_object(File) do |file|
  16. @files_to_reopen << file if !file.closed?
  17. end
  18. end
  19. def after_fork(dir)
  20. Dir.chdir dir
  21. # Re-open file handles
  22. @files_to_reopen.each do |file|
  23. file.reopen file.path, 'a+'
  24. file.sync = true
  25. end
  26. # Spring redirects STDOUT and STDERR to /dev/null
  27. # before we get here. This causes the `reopen` lines
  28. # below to fail because the handles are already
  29. # opened for write
  30. if defined?(Spring)
  31. $stdout.close
  32. $stderr.close
  33. end
  34. $stdout.reopen("#{dir}/log/scheduler_out.log", 'w')
  35. $stderr.reopen("#{dir}/log/scheduler_err.log", 'w')
  36. end
  37. before_fork
  38. daemon_options = {
  39. multiple: false,
  40. dir_mode: :normal,
  41. dir: File.join(dir, 'tmp', 'pids'),
  42. backtrace: true
  43. }
  44. Daemons.run_proc('scheduler', daemon_options) do
  45. after_fork(dir)
  46. require File.join(dir, 'config', 'environment')
  47. Rails.logger.info 'Scheduler started.'
  48. at_exit do
  49. # use process title for stop log entry
  50. # if differs from default process title
  51. title = 'Scheduler'
  52. if $PROGRAM_NAME != 'scheduler.rb'
  53. title = $PROGRAM_NAME
  54. end
  55. Rails.logger.info "#{title} stopped."
  56. end
  57. begin
  58. Scheduler.threads
  59. rescue Interrupt
  60. nil
  61. end
  62. end