Module: Pgbus::Recurring::ConfigLoader

Defined in:
lib/pgbus/recurring/config_loader.rb

Class Method Summary collapse

Class Method Details

.detect_envObject



50
51
52
53
54
55
56
# File 'lib/pgbus/recurring/config_loader.rb', line 50

def detect_env
  if defined?(Rails) && Rails.respond_to?(:env) && Rails.env
    Rails.env.to_s
  else
    ENV.fetch("PGBUS_ENV", "development")
  end
end

.load(path, env: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pgbus/recurring/config_loader.rb', line 11

def load(path, env: nil)
  return {} unless path && File.exist?(path.to_s)

  env ||= detect_env
  raw = File.read(path)
  parsed = YAML.safe_load(ERB.new(raw).result, permitted_classes: [Symbol], aliases: true)
  return {} unless parsed.is_a?(Hash)

  # If the parsed hash has an environment key, use that subtree
  parsed.key?(env) ? parsed.fetch(env, {}) : parsed
rescue StandardError => e
  Pgbus.logger.error { "[Pgbus] Failed to load recurring config from #{path}: #{e.message}" }
  {}
end

.load_all(paths, env: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pgbus/recurring/config_loader.rb', line 26

def load_all(paths, env: nil)
  normalized = Array(paths).compact.map { |p| p.respond_to?(:to_path) ? p.to_path : p.to_s }.reject(&:empty?)
  return {} if normalized.empty?

  env ||= detect_env

  normalized.each_with_object({}) do |path, acc|
    unless File.exist?(path.to_s)
      Pgbus.logger.warn { "[Pgbus] Recurring file not found, skipping: #{path}" }
      next
    end

    parsed = load(path, env: env)
    unless parsed.is_a?(Hash)
      Pgbus.logger.error { "[Pgbus] Invalid recurring config in #{path}: expected Hash, got #{parsed.class}" }
      next
    end
    parsed.each_key do |key|
      Pgbus.logger.debug { "[Pgbus] Recurring task '#{key}' overridden by #{path}" } if acc.key?(key)
    end
    acc.merge!(parsed)
  end
end