scheduler.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env ruby
  2. # Copyright (C) 2012-2022 Zammad Foundation, https://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 'daemons'
  12. DEPRECATION_WARNING = "'script/scheduler.rb' is deprecated and will be removed with Zammad 6. Please use 'script/background-worker.rb' instead - note that this will not daemonize but always stay in the foreground.".freeze
  13. warn "DEPRECATION WARNING: #{DEPRECATION_WARNING}"
  14. def before_fork
  15. # remember open file handles
  16. @files_to_reopen = []
  17. ObjectSpace.each_object(File) do |file|
  18. @files_to_reopen << file if !file.closed?
  19. end
  20. end
  21. def after_fork(dir)
  22. Dir.chdir dir
  23. # Re-open file handles
  24. @files_to_reopen.each do |file|
  25. file.reopen file.path, 'a+'
  26. file.sync = true
  27. end
  28. # Spring redirects STDOUT and STDERR to /dev/null
  29. # before we get here. This causes the `reopen` lines
  30. # below to fail because the handles are already
  31. # opened for write
  32. if defined?(Spring)
  33. $stdout.close
  34. $stderr.close
  35. end
  36. $stdout.reopen("#{dir}/log/scheduler_out.log", 'w')
  37. $stderr.reopen("#{dir}/log/scheduler_err.log", 'w')
  38. end
  39. before_fork
  40. daemon_options = {
  41. multiple: false,
  42. dir_mode: :normal,
  43. dir: File.join(dir, 'tmp', 'pids'),
  44. backtrace: true
  45. }
  46. Daemons.run_proc('scheduler', daemon_options) do
  47. after_fork(dir)
  48. require File.join(dir, 'config', 'environment')
  49. Rails.logger.info 'Scheduler started.'
  50. ActiveSupport::Deprecation.warn DEPRECATION_WARNING
  51. at_exit do
  52. # use process title for stop log entry
  53. # if differs from default process title
  54. title = 'Scheduler'
  55. if $PROGRAM_NAME != 'scheduler.rb'
  56. title = $PROGRAM_NAME
  57. end
  58. Rails.logger.info "#{title} stopped."
  59. end
  60. begin
  61. config = BackgroundServices::ServiceConfig.configuration_from_env(ENV)
  62. BackgroundServices.new(config).run
  63. rescue Interrupt
  64. nil
  65. end
  66. end