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



31
32
33
# File 'app/lib/katello/event_daemon/runner.rb', line 31

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

.pidObject



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_dirObject



27
28
29
# File 'app/lib/katello/event_daemon/runner.rb', line 27

def pid_dir
  tmp_dir.join('pids')
end

.pid_fileObject



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

Returns:

  • (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

.settingsObject



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

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

.startObject



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_threadObject



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

Returns:

  • (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

.stopObject



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_dirObject



23
24
25
# File 'app/lib/katello/event_daemon/runner.rb', line 23

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

.write_pid_fileObject



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