Class: Kaal::SchedulerFileLoader::PayloadLoader
- Inherits:
-
Object
- Object
- Kaal::SchedulerFileLoader::PayloadLoader
- Defined in:
- lib/kaal/scheduler_file/payload_loader.rb
Overview
Loads and validates scheduler YAML payloads from disk.
Instance Method Summary collapse
- #extract_jobs(payload) ⇒ Object
- #handle_missing_file(path) ⇒ Object
-
#initialize(configuration:, runtime_context:, logger:, hash_transform:) ⇒ PayloadLoader
constructor
A new instance of PayloadLoader.
- #load ⇒ Object
- #validate_unique_keys(jobs) ⇒ Object
Constructor Details
#initialize(configuration:, runtime_context:, logger:, hash_transform:) ⇒ PayloadLoader
Returns a new instance of PayloadLoader.
14 15 16 17 18 19 |
# File 'lib/kaal/scheduler_file/payload_loader.rb', line 14 def initialize(configuration:, runtime_context:, logger:, hash_transform:) @configuration = configuration @runtime_context = runtime_context @logger = logger @hash_transform = hash_transform end |
Instance Method Details
#extract_jobs(payload) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/kaal/scheduler_file/payload_loader.rb', line 36 def extract_jobs(payload) environment_name = @runtime_context.environment_name defaults = fetch_hash(payload, 'defaults') env_payload = fetch_hash(payload, environment_name) default_jobs = defaults.fetch('jobs', []) env_jobs = env_payload.fetch('jobs', []) raise SchedulerConfigError, "Expected 'defaults.jobs' to be an array" unless default_jobs.is_a?(Array) raise SchedulerConfigError, "Expected '#{environment_name}.jobs' to be an array" unless env_jobs.is_a?(Array) default_jobs + env_jobs end |
#handle_missing_file(path) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/kaal/scheduler_file/payload_loader.rb', line 28 def handle_missing_file(path) = "Scheduler file not found at #{path}" raise SchedulerConfigError, if @configuration.scheduler_missing_file_policy == :error @logger&.warn() [] end |
#load ⇒ Object
21 22 23 24 25 26 |
# File 'lib/kaal/scheduler_file/payload_loader.rb', line 21 def load path = scheduler_file_path return [path, nil] unless File.exist?(path) [path, parse_yaml(path)] end |
#validate_unique_keys(jobs) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/kaal/scheduler_file/payload_loader.rb', line 48 def validate_unique_keys(jobs) keys = jobs.map do |job_payload| raise SchedulerConfigError, "Each jobs entry must be a mapping, got #{job_payload.class}" unless job_payload.is_a?(Hash) @hash_transform.stringify_keys(job_payload)['key'].to_s.strip end duplicates = keys.group_by(&:itself).select { |key, arr| !key.empty? && arr.size > 1 }.keys return if duplicates.empty? raise SchedulerConfigError, "Duplicate job keys in scheduler file: #{duplicates.join(', ')}" end |