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
96 97 98 |
# File 'app/lib/katello/event_daemon/runner.rb', line 96 def register_service(name, klass) @services[name] = klass end |
.runnable? ⇒ Boolean
89 90 91 92 93 94 |
# File 'app/lib/katello/event_daemon/runner.rb', line 89 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
100 101 102 |
# File 'app/lib/katello/event_daemon/runner.rb', line 100 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 |
# 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 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
81 82 83 84 85 86 87 |
# File 'app/lib/katello/event_daemon/runner.rb', line 81 def start_monitor_thread @monitor_thread = Thread.new do Rails.application.executor.wrap do Katello::EventDaemon::Monitor.new(@services).start end end end |
.started? ⇒ Boolean
74 75 76 77 78 79 |
# File 'app/lib/katello/event_daemon/runner.rb', line 74 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 |