Class: Katello::EventDaemon::Runner
- Inherits:
-
Object
- Object
- Katello::EventDaemon::Runner
- Defined in:
- app/lib/katello/event_daemon/runner.rb
Constant Summary collapse
- STATUS_CACHE_KEY =
"katello_event_daemon_status".freeze
Class Method Summary collapse
- .lock_file ⇒ Object
- .pid ⇒ Object
- .pid_dir ⇒ Object
- .pid_file ⇒ Object
- .register_service(name, klass) ⇒ Object
- .runnable? ⇒ Boolean
- .service_status(service_name = nil) ⇒ Object
- .settings ⇒ Object
- .start ⇒ Object
- .start_monitor_thread ⇒ Object
- .started? ⇒ Boolean
- .stop ⇒ Object
- .tmp_dir ⇒ Object
- .write_pid_file ⇒ Object
Class Method Details
.lock_file ⇒ Object
33 34 35 |
# File 'app/lib/katello/event_daemon/runner.rb', line 33 def lock_file tmp_dir.join('katello_event_daemon.lock') end |
.pid ⇒ Object
13 14 15 16 17 18 19 |
# File 'app/lib/katello/event_daemon/runner.rb', line 13 def pid return unless pid_file File.read(pid_file)&.to_i rescue Errno::ENOENT nil end |
.pid_dir ⇒ Object
29 30 31 |
# File 'app/lib/katello/event_daemon/runner.rb', line 29 def pid_dir tmp_dir.join('pids') end |
.pid_file ⇒ Object
21 22 23 |
# File 'app/lib/katello/event_daemon/runner.rb', line 21 def pid_file pid_dir.join('katello_event_daemon.pid') end |
.register_service(name, klass) ⇒ Object
97 98 99 |
# File 'app/lib/katello/event_daemon/runner.rb', line 97 def register_service(name, klass) @services[name] = klass end |
.runnable? ⇒ Boolean
90 91 92 93 94 95 |
# File 'app/lib/katello/event_daemon/runner.rb', line 90 def runnable? # avoid accessing the disk on each request @cache.fetch('katello_event_daemon_runnable', expires_in: 1.minute) do !started? && settings[:enabled] && !::Foreman.in_rake? && !Rails.env.test? end end |
.service_status(service_name = nil) ⇒ Object
101 102 103 |
# File 'app/lib/katello/event_daemon/runner.rb', line 101 def service_status(service_name = nil) Rails.cache.read(STATUS_CACHE_KEY)&.dig(service_name) end |
.settings ⇒ Object
9 10 11 |
# File 'app/lib/katello/event_daemon/runner.rb', line 9 def settings SETTINGS[:katello][:event_daemon] end |
.start ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/lib/katello/event_daemon/runner.rb', line 52 def start return unless runnable? FileUtils.mkdir_p(tmp_dir) FileUtils.touch(lock_file) File.open(lock_file, 'r') do |lockfile| lockfile.flock(File::LOCK_EX) return nil if started? # ensure it wasn't started while we waited for the lock @monitor = Katello::EventDaemon::Monitor.new(@services) start_monitor_thread write_pid_file at_exit do stop end Rails.logger.info("Katello event daemon started process=#{Process.pid}") ensure lockfile.flock(File::LOCK_UN) end end |
.start_monitor_thread ⇒ Object
82 83 84 85 86 87 88 |
# File 'app/lib/katello/event_daemon/runner.rb', line 82 def start_monitor_thread @monitor_thread = Thread.new do Rails.application.executor.wrap do @monitor.start end end end |
.started? ⇒ Boolean
75 76 77 78 79 80 |
# File 'app/lib/katello/event_daemon/runner.rb', line 75 def started? Process.kill(0, pid) true rescue Errno::ESRCH, TypeError # process no longer exists or we had no PID cached false end |
.stop ⇒ Object
44 45 46 47 48 49 50 |
# File 'app/lib/katello/event_daemon/runner.rb', line 44 def stop return unless pid == Process.pid @monitor_thread.kill @cache.clear @services.values.each(&:close) FileUtils.rm_f(pid_file) if pid_file end |
.tmp_dir ⇒ Object
25 26 27 |
# File 'app/lib/katello/event_daemon/runner.rb', line 25 def tmp_dir Rails.root.join('tmp') end |
.write_pid_file ⇒ Object
37 38 39 40 41 42 |
# File 'app/lib/katello/event_daemon/runner.rb', line 37 def write_pid_file return unless pid_file FileUtils.mkdir_p(pid_dir) File.write(pid_file, Process.pid) end |