Class: Katello::EventDaemon::Runner

Inherits:
Object
  • Object
show all
Defined in:
app/lib/katello/event_daemon/runner.rb

Constant Summary collapse

STATUS_CACHE_KEY =
"katello_event_daemon_status".freeze

Class Method Summary collapse

Class Method Details

.lock_fileObject



33
34
35
# File 'app/lib/katello/event_daemon/runner.rb', line 33

def lock_file
  tmp_dir.join('katello_event_daemon.lock')
end

.pidObject



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_dirObject



29
30
31
# File 'app/lib/katello/event_daemon/runner.rb', line 29

def pid_dir
  tmp_dir.join('pids')
end

.pid_fileObject



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

.settingsObject



9
10
11
# File 'app/lib/katello/event_daemon/runner.rb', line 9

def settings
  SETTINGS[:katello][:event_daemon]
end

.startObject



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_threadObject



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

.stopObject



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_dirObject



25
26
27
# File 'app/lib/katello/event_daemon/runner.rb', line 25

def tmp_dir
  Rails.root.join('tmp')
end

.write_pid_fileObject



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