Class: Kaal::SchedulerFileLoader::PayloadLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/kaal/scheduler_file/payload_loader.rb,
sig/kaal/scheduler_file/payload_loader.rbs

Overview

Loads and validates scheduler YAML payloads from disk.

Instance Method Summary collapse

Constructor Details

#initialize(configuration:, runtime_context:, logger:, hash_transform:) ⇒ PayloadLoader

Returns a new instance of PayloadLoader.

Parameters:

  • configuration: (Kaal::rbs_any)
  • runtime_context: (Kaal::rbs_any)
  • logger: (Kaal::rbs_any)
  • hash_transform: (Kaal::rbs_any)


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) ⇒ Kaal::rbs_any

Parameters:

  • payload (Kaal::rbs_any)

Returns:

  • (Kaal::rbs_any)

Raises:



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

#fetch_hash(payload, key) ⇒ Kaal::rbs_any

Parameters:

  • payload (Kaal::rbs_any)
  • key (Kaal::rbs_any)

Returns:

  • (Kaal::rbs_any)


85
86
87
88
89
90
91
92
# File 'lib/kaal/scheduler_file/payload_loader.rb', line 85

def fetch_hash(payload, key)
  section = payload.fetch(key)
  raise SchedulerConfigError, "Expected '#{key}' section to be a mapping" unless section.is_a?(Hash)

  section
rescue KeyError
  {}
end

#handle_missing_file(path) ⇒ ::Array[Kaal::rbs_any]

Parameters:

  • path (Kaal::rbs_any)

Returns:

  • (::Array[Kaal::rbs_any])

Raises:



28
29
30
31
32
33
34
# File 'lib/kaal/scheduler_file/payload_loader.rb', line 28

def handle_missing_file(path)
  message = "Scheduler file not found at #{path}"
  raise SchedulerConfigError, message if @configuration.scheduler_missing_file_policy == :error

  @logger&.warn(message)
  []
end

#load ⇒ ::Array[Kaal::rbs_any | nil], ::Array[Kaal::rbs_any]

Returns:

  • (::Array[Kaal::rbs_any | nil], ::Array[Kaal::rbs_any])


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

#parse_yaml(path) ⇒ Kaal::rbs_any

Parameters:

  • path (Kaal::rbs_any)

Returns:

  • (Kaal::rbs_any)


69
70
71
72
73
74
75
76
77
# File 'lib/kaal/scheduler_file/payload_loader.rb', line 69

def parse_yaml(path)
  rendered = render_yaml_erb(path)
  parsed = YAML.safe_load(rendered) || {}
  raise SchedulerConfigError, "Expected scheduler YAML root to be a mapping in #{path}" unless parsed.is_a?(Hash)

  @hash_transform.stringify_keys(parsed)
rescue Psych::Exception => e
  raise SchedulerConfigError, "Failed to parse scheduler YAML at #{path}: #{e.message}"
end

#render_yaml_erb(path) ⇒ Kaal::rbs_any

Parameters:

  • path (Kaal::rbs_any)

Returns:

  • (Kaal::rbs_any)


79
80
81
82
83
# File 'lib/kaal/scheduler_file/payload_loader.rb', line 79

def render_yaml_erb(path)
  ERB.new(File.read(path), trim_mode: '-').result
rescue StandardError, SyntaxError => e
  raise SchedulerConfigError, "Failed to evaluate scheduler ERB at #{path}: #{e.message}"
end

#scheduler_file_path ⇒ Kaal::rbs_any

Returns:

  • (Kaal::rbs_any)

Raises:



62
63
64
65
66
67
# File 'lib/kaal/scheduler_file/payload_loader.rb', line 62

def scheduler_file_path
  configured_path = @configuration.scheduler_config_path.to_s.strip
  raise SchedulerConfigError, 'scheduler_config_path cannot be blank' if configured_path.empty?

  @runtime_context.resolve_path(configured_path)
end

#validate_unique_keys(jobs) ⇒ nil, Kaal::rbs_any

Parameters:

  • jobs (Kaal::rbs_any)

Returns:

  • (nil, Kaal::rbs_any)

Raises:



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