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
31 32 33 |
# File 'app/lib/katello/event_daemon/runner.rb', line 31 def lock_file tmp_dir.join('katello_event_daemon.lock') end |
.pid ⇒ Object
13 14 15 16 17 |
# File 'app/lib/katello/event_daemon/runner.rb', line 13 def pid return unless pid_file && File.exist?(pid_file) File.read(pid_file).to_i end |
.pid_dir ⇒ Object
27 28 29 |
# File 'app/lib/katello/event_daemon/runner.rb', line 27 def pid_dir tmp_dir.join('pids') end |
.pid_file ⇒ Object
19 20 21 |
# File 'app/lib/katello/event_daemon/runner.rb', line 19 def pid_file pid_dir.join('katello_event_daemon.pid') end |
.register_service(name, klass) ⇒ Object
92 93 94 |
# File 'app/lib/katello/event_daemon/runner.rb', line 92 def register_service(name, klass) @services[name] = klass end |
.runnable? ⇒ Boolean
85 86 87 88 89 90 |
# File 'app/lib/katello/event_daemon/runner.rb', line 85 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
96 97 98 |
# File 'app/lib/katello/event_daemon/runner.rb', line 96 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
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/lib/katello/event_daemon/runner.rb', line 50 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
79 80 81 82 83 |
# File 'app/lib/katello/event_daemon/runner.rb', line 79 def start_monitor_thread @monitor_thread = Thread.new do Katello::EventDaemon::Monitor.new(@services).start end end |
.started? ⇒ Boolean
72 73 74 75 76 77 |
# File 'app/lib/katello/event_daemon/runner.rb', line 72 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
42 43 44 45 46 47 48 |
# File 'app/lib/katello/event_daemon/runner.rb', line 42 def stop return unless pid == Process.pid @monitor_thread.kill @cache.clear @services.values.each(&:close) File.unlink(pid_file) if pid_file && File.exist?(pid_file) end |
.tmp_dir ⇒ Object
23 24 25 |
# File 'app/lib/katello/event_daemon/runner.rb', line 23 def tmp_dir Rails.root.join('tmp') end |
.write_pid_file ⇒ Object
35 36 37 38 39 40 |
# File 'app/lib/katello/event_daemon/runner.rb', line 35 def write_pid_file return unless pid_file FileUtils.mkdir_p(pid_dir) File.write(pid_file, Process.pid) end |