-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathbackground_daemon
More file actions
executable file
·82 lines (70 loc) · 2.71 KB
/
background_daemon
File metadata and controls
executable file
·82 lines (70 loc) · 2.71 KB
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
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'rubygems'
require 'fileutils'
require 'active_support'
require 'active_support/core_ext'
require_relative '../config/environment'
require 'background_task_registry'
require 'logger'
root_dir = File.expand_path('../', File.dirname(__FILE__))
pid_dir = root_dir + '/tmp/pids'
log_dir = root_dir + '/log'
FileUtils.mkdir_p(pid_dir)
FileUtils.mkdir_p(log_dir)
def exception_to_str(e)
e.message + "\n" + e.backtrace.join("\n from ")
end
Dir.chdir root_dir
require root_dir + '/config/environment'
stdout_logger = Logger.new(STDOUT)
stdout_logger.formatter = proc do |severity, datetime, progname, msg|
"#{datetime}: #{severity} - #{msg}\n"
end
file_logger = Logger.new(Rails.root.join('log', 'background_daemon.log'))
file_logger.formatter = proc do |severity, datetime, progname, msg|
"#{datetime}: #{severity} - #{msg}\n"
end
Rails.logger = ActiveSupport::BroadcastLogger.new(stdout_logger, file_logger)
FileUtils.mkdir_p(Rails.root.join('log', 'background_daemon'))
Rails.logger.auto_flushing = true if Rails.logger.respond_to? :auto_flushing=
Rails.logger.info 'Starting background_daemon'
# ActiveRecord::Base.connection_config[:pool] = 25
# Tasks may be longrunning so we want each one in a thread so one task won't
# block other ones.
processes = BackgroundTaskRegistry.all_tasks.map do |task|
Process.fork do
# Create individual logger for this task
task_log_file = Rails.root.join('log', 'background_daemon', "#{task.class.name.underscore}.log")
task_logger = Logger.new(task_log_file)
task_logger.formatter = proc do |severity, datetime, progname, msg|
"#{datetime}: #{severity} - #{msg}\n"
end
task_logger.auto_flushing = true if task_logger.respond_to? :auto_flushing=
# Set up Rails logger for this process to use the task-specific logger
Rails.logger = task_logger
Rails.logger.info "Starting #{task.class.name} background task"
last_reminder_time = nil
loop do
begin
if last_reminder_time.nil? || Time.zone.now - last_reminder_time > 1.minutes
Rails.logger.info "Occasional reminder that #{task.class.name} is running."
last_reminder_time = Time.zone.now
end
task.run
rescue StandardError => e
Rails.logger.error "#{task.class.name} exception: " + exception_to_str(e)
begin
ActiveRecord::Base.connection_handler.clear_active_connections!
rescue StandardError => e2
Rails.logger.error 'Failed to get rid of used db connections: ' + exception_to_str(e2)
end
end
sleep task.wait_delay
end
end
end
# TODO: we may not detect if only one of the threads dies.
processes.each do |pid|
Process.wait(pid)
end