Module: Silas::Schedule::Compiler

Defined in:
lib/silas/schedule/compiler.rb

Overview

Discovery alone doesn't schedule anything: Solid Queue's scheduler reads config/recurring.yml at its own boot, and a running scheduler can't be appended to cleanly. So bin/rails silas:schedules compiles the discovered schedules into recurring.yml as a reviewable git diff — cron that fires real side effects stays explicit.

Merge is key-scoped: only silas_schedule_* keys are managed. Hand-written entries (e.g. silas_dead_job_rescuer) are preserved.

Constant Summary collapse

MANAGED_PREFIX =
"silas_schedule_".freeze

Class Method Summary collapse

Class Method Details

.drift(schedules, root:, env: "production") ⇒ Object

Names present as files but not compiled into recurring.yml (and vice versa) — the doctor for silas:schedules:list.



40
41
42
43
44
45
46
47
48
49
# File 'lib/silas/schedule/compiler.rb', line 40

def drift(schedules, root:, env: "production")
  path = root.join("config/recurring.yml")
  compiled = path.exist? ? ((YAML.safe_load(path.read) || {}).dig(env) || {}) : {}
  compiled_keys = compiled.keys.select { |k| k.to_s.start_with?(MANAGED_PREFIX) }
  discovered_keys = schedules.map(&:recurring_key)
  {
    uncompiled: schedules.reject { |s| compiled_keys.include?(s.recurring_key) }.map(&:name),
    orphaned: compiled_keys - discovered_keys
  }
end

.render(schedules) ⇒ Object

{ recurring_key => entry_hash } for the given schedules.



19
20
21
# File 'lib/silas/schedule/compiler.rb', line 19

def render(schedules)
  schedules.sort_by(&:name).to_h { |s| [ s.recurring_key, s.recurring_entry ] }
end

.write!(schedules, root:, env: "production") ⇒ Object

Merge managed entries into recurring.yml under env, preserving everything else. Returns the written YAML string.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/silas/schedule/compiler.rb', line 25

def write!(schedules, root:, env: "production")
  path = root.join("config/recurring.yml")
  doc = path.exist? ? (YAML.safe_load(path.read) || {}) : {}
  doc[env] ||= {}
  # Drop stale managed keys, then merge current ones.
  doc[env].reject! { |k, _| k.to_s.start_with?(MANAGED_PREFIX) }
  doc[env].merge!(render(schedules))
  doc[env] = doc[env].sort.to_h
  yaml = YAML.dump(doc)
  path.write(yaml)
  yaml
end