Class: ActiveJob::Temporal::CertificateWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/activejob/temporal/certificate_watcher.rb

Overview

Watches TLS certificate files and runs a reload callback when they change.

Constant Summary collapse

DEFAULT_DEBOUNCE_SECONDS =
1.0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths:, reload_callback:, listener_factory: Listen, debounce_seconds: DEFAULT_DEBOUNCE_SECONDS) ⇒ CertificateWatcher

Returns a new instance of CertificateWatcher.



19
20
21
22
23
24
25
26
27
# File 'lib/activejob/temporal/certificate_watcher.rb', line 19

def initialize(paths:, reload_callback:, listener_factory: Listen, debounce_seconds: DEFAULT_DEBOUNCE_SECONDS)
  @paths = paths.map { |path| File.expand_path(path) }.uniq
  @reload_callback = reload_callback
  @listener_factory = listener_factory
  @debounce_seconds = debounce_seconds
  @mutex = Mutex.new
  @last_reload_at = nil
  @listener = nil
end

Class Method Details

.paths_from_config(configuration) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/activejob/temporal/certificate_watcher.rb', line 11

def self.paths_from_config(configuration)
  [
    configuration.tls_cert_path,
    configuration.tls_key_path,
    configuration.tls_server_root_ca_cert_path
  ].compact.reject { |path| path.to_s.strip.empty? }
end

Instance Method Details

#handle_changes(changed_paths) ⇒ Object



44
45
46
47
48
49
# File 'lib/activejob/temporal/certificate_watcher.rb', line 44

def handle_changes(changed_paths)
  return unless relevant_change?(changed_paths)
  return if debounced?

  @reload_callback.call
end

#startObject



29
30
31
32
33
34
35
36
37
# File 'lib/activejob/temporal/certificate_watcher.rb', line 29

def start
  return self if @paths.empty? || @listener

  @listener = @listener_factory.to(*directories) do |modified, added, removed|
    handle_changes(modified + added + removed)
  end
  @listener.start
  self
end

#stopObject



39
40
41
42
# File 'lib/activejob/temporal/certificate_watcher.rb', line 39

def stop
  @listener&.stop
  @listener = nil
end