scheduler.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env ruby
  2. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  3. $LOAD_PATH << './lib'
  4. require 'rubygems'
  5. # load rails env
  6. dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
  7. Dir.chdir dir
  8. RAILS_ENV = ENV['RAILS_ENV'] || 'development'
  9. require 'rails/all'
  10. require 'bundler'
  11. require File.join(dir, 'config', 'environment')
  12. require 'daemons'
  13. def before_fork
  14. # clear all connections before for, reconnect later ActiveRecord::Base.connection.reconnect!
  15. # issue #1405 - Scheduler not running because of Bad file descriptor in PGConsumeInput()
  16. # https://github.com/zammad/zammad/issues/1405
  17. # see also https://bitbucket.org/ged/ruby-pg/issues/260/frequent-crashes-with-multithreading
  18. ActiveRecord::Base.clear_all_connections!
  19. # remember open file handles
  20. @files_to_reopen = []
  21. ObjectSpace.each_object(File) do |file|
  22. @files_to_reopen << file unless 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. $stdout.reopen("#{dir}/log/scheduler_out.log", 'w')
  33. $stderr.reopen("#{dir}/log/scheduler_err.log", 'w')
  34. end
  35. before_fork
  36. daemon_options = {
  37. multiple: false,
  38. dir_mode: :normal,
  39. dir: File.join(dir, 'tmp', 'pids'),
  40. backtrace: true
  41. }
  42. name = 'scheduler'
  43. Daemons.run_proc(name, daemon_options) do
  44. if ARGV.include?('--')
  45. ARGV.slice! 0..ARGV.index('--')
  46. else
  47. ARGV.clear
  48. end
  49. after_fork(dir)
  50. Rails.logger.info 'Scheduler started.'
  51. at_exit do
  52. Rails.logger.info 'Scheduler stopped.'
  53. end
  54. require 'scheduler'
  55. Scheduler.threads
  56. end