scheduler.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 'daemons'
  7. 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
  8. warn "DEPRECATION WARNING: #{DEPRECATION_WARNING}"
  9. def before_fork
  10. # remember open file handles
  11. @files_to_reopen = []
  12. ObjectSpace.each_object(File) do |file|
  13. @files_to_reopen << file if !file.closed?
  14. end
  15. end
  16. def after_fork(dir)
  17. Dir.chdir dir
  18. # Re-open file handles
  19. @files_to_reopen.each do |file|
  20. file.reopen file.path, 'a+'
  21. file.sync = true
  22. end
  23. $stdout.reopen("#{dir}/log/scheduler_out.log", 'w')
  24. $stderr.reopen("#{dir}/log/scheduler_err.log", 'w')
  25. end
  26. before_fork
  27. daemon_options = {
  28. multiple: false,
  29. dir_mode: :normal,
  30. dir: File.join(dir, 'tmp', 'pids'),
  31. backtrace: true
  32. }
  33. Daemons.run_proc('scheduler', daemon_options) do
  34. after_fork(dir)
  35. require File.join(dir, 'config', 'environment')
  36. Rails.logger.info 'Scheduler started.'
  37. ActiveSupport::Deprecation.warn DEPRECATION_WARNING
  38. at_exit do
  39. # use process title for stop log entry
  40. # if differs from default process title
  41. title = 'Scheduler'
  42. if $PROGRAM_NAME != 'scheduler.rb'
  43. title = $PROGRAM_NAME
  44. end
  45. Rails.logger.info "#{title} stopped."
  46. end
  47. begin
  48. config = BackgroundServices::ServiceConfig.configuration_from_env(ENV)
  49. BackgroundServices.new(config).run
  50. rescue Interrupt
  51. nil
  52. end
  53. end