forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.rb
executable file
·84 lines (66 loc) · 2.01 KB
/
scheduler.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env ruby
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
begin
load File.expand_path('../bin/spring', __dir__)
rescue LoadError => e
raise if e.message.exclude?('spring')
end
dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
Dir.chdir dir
require 'bundler'
require 'daemons'
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
warn "DEPRECATION WARNING: #{DEPRECATION_WARNING}"
def before_fork
# remember open file handles
@files_to_reopen = []
ObjectSpace.each_object(File) do |file|
@files_to_reopen << file if !file.closed?
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
# 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
$stdout.reopen("#{dir}/log/scheduler_out.log", 'w')
$stderr.reopen("#{dir}/log/scheduler_err.log", 'w')
end
before_fork
daemon_options = {
multiple: false,
dir_mode: :normal,
dir: File.join(dir, 'tmp', 'pids'),
backtrace: true
}
Daemons.run_proc('scheduler', daemon_options) do
after_fork(dir)
require File.join(dir, 'config', 'environment')
Rails.logger.info 'Scheduler started.'
ActiveSupport::Deprecation.warn DEPRECATION_WARNING
at_exit do
# 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."
end
begin
config = BackgroundServices::ServiceConfig.configuration_from_env(ENV)
BackgroundServices.new(config).run
rescue Interrupt
nil
end
end