2013-03-10 23:14:31 +00:00
|
|
|
#!/usr/bin/env ruby
|
2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2018-10-01 20:00:26 +00:00
|
|
|
begin
|
|
|
|
load File.expand_path('../bin/spring', __dir__)
|
|
|
|
rescue LoadError => e
|
2020-09-30 09:07:01 +00:00
|
|
|
raise if e.message.exclude?('spring')
|
2018-10-01 20:00:26 +00:00
|
|
|
end
|
2013-06-13 07:01:06 +00:00
|
|
|
|
2013-03-10 23:14:31 +00:00
|
|
|
dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
2017-04-27 07:34:40 +00:00
|
|
|
Dir.chdir dir
|
|
|
|
|
|
|
|
require 'bundler'
|
|
|
|
require 'daemons'
|
2013-03-10 23:14:31 +00:00
|
|
|
|
2017-05-08 14:05:38 +00:00
|
|
|
def before_fork
|
|
|
|
# remember open file handles
|
|
|
|
@files_to_reopen = []
|
|
|
|
ObjectSpace.each_object(File) do |file|
|
2018-05-04 14:05:10 +00:00
|
|
|
@files_to_reopen << file if !file.closed?
|
2017-05-08 14:05:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_fork(dir)
|
|
|
|
Dir.chdir dir
|
|
|
|
|
|
|
|
# Re-open file handles
|
|
|
|
@files_to_reopen.each do |file|
|
|
|
|
file.reopen file.path, 'a+'
|
|
|
|
file.sync = true
|
|
|
|
end
|
|
|
|
|
2018-10-01 20:00:26 +00:00
|
|
|
# Spring redirects STDOUT and STDERR to /dev/null
|
|
|
|
# before we get here. This causes the `reopen` lines
|
|
|
|
# below to fail because the handles are already
|
|
|
|
# opened for write
|
|
|
|
if defined?(Spring)
|
|
|
|
$stdout.close
|
|
|
|
$stderr.close
|
|
|
|
end
|
|
|
|
|
2017-05-15 13:09:02 +00:00
|
|
|
$stdout.reopen("#{dir}/log/scheduler_out.log", 'w')
|
|
|
|
$stderr.reopen("#{dir}/log/scheduler_err.log", 'w')
|
2017-05-08 14:05:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
before_fork
|
|
|
|
|
2013-03-10 23:14:31 +00:00
|
|
|
daemon_options = {
|
2018-10-01 20:00:26 +00:00
|
|
|
multiple: false,
|
|
|
|
dir_mode: :normal,
|
|
|
|
dir: File.join(dir, 'tmp', 'pids'),
|
2015-04-27 13:42:53 +00:00
|
|
|
backtrace: true
|
2013-03-10 23:14:31 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 20:00:26 +00:00
|
|
|
Daemons.run_proc('scheduler', daemon_options) do
|
2014-02-03 18:27:27 +00:00
|
|
|
|
2017-05-08 14:05:38 +00:00
|
|
|
after_fork(dir)
|
2014-02-03 18:27:27 +00:00
|
|
|
|
2018-10-01 20:00:26 +00:00
|
|
|
require File.join(dir, 'config', 'environment')
|
2017-08-14 10:03:03 +00:00
|
|
|
|
2018-10-01 20:00:26 +00:00
|
|
|
Rails.logger.info 'Scheduler started.'
|
2017-08-14 10:03:03 +00:00
|
|
|
at_exit do
|
2020-03-20 05:11:50 +00:00
|
|
|
|
|
|
|
# use process title for stop log entry
|
|
|
|
# if differs from default process title
|
|
|
|
title = 'Scheduler'
|
|
|
|
if $PROGRAM_NAME != 'scheduler.rb'
|
|
|
|
title = $PROGRAM_NAME
|
|
|
|
end
|
|
|
|
|
|
|
|
Rails.logger.info "#{title} stopped."
|
2017-08-14 10:03:03 +00:00
|
|
|
end
|
|
|
|
|
2019-01-02 16:31:12 +00:00
|
|
|
begin
|
|
|
|
Scheduler.threads
|
|
|
|
rescue Interrupt
|
|
|
|
nil
|
|
|
|
end
|
2014-02-03 18:27:27 +00:00
|
|
|
end
|