Class: Pgbus::Generators::ConfigConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/pgbus/generators/config_converter.rb

Overview

Converts a config/pgbus.yml file into a Ruby initializer (config/initializers/pgbus.rb) using the Ruby DSL added in Pgbus 0.5+ — capsule string DSL, ActiveSupport::Duration coercion, auto-tuned pool size, named capsules, etc.

Drops settings that:

- match the gem default (no point restating it)
- are deprecated (e.g. pool_size, which is now auto-tuned)

Converts seconds to durations when they evenly divide into a clean unit (7 days, 30 days, 10 minutes). Falls back to the raw integer otherwise.

When the YAML has multiple environments with different values for the same setting, emits Rails.env-aware code:

- 2 envs with same value → unconditional line
- 2 envs, one differs → `unless Rails.env.X?` modifier
- 3+ envs with differences → `case Rails.env when ... end`

Backwards compatible: the original YAML file is NOT touched. The generator’s CLI wrapper writes the new initializer and tells the user to delete the YAML when ready.

Defined Under Namespace

Classes: Error

Constant Summary collapse

DURATION_SETTINGS =

Setters that accept ActiveSupport::Duration (PR 5).

%w[
  visibility_timeout archive_retention idempotency_ttl
  outbox_retention stats_retention recurring_execution_retention
].freeze
DEPRECATED_SETTINGS =

Settings that no longer exist in the public API. The converter silently drops these from the generated initializer so users on legacy YAML get a clean migration.

- pool_size              -> auto-tuned from worker thread counts
- notify_throttle_ms     -> Pgbus::Client::NOTIFY_THROTTLE_MS
- circuit_breaker_*      -> Pgbus::CircuitBreaker constants
- archive_compaction_*   -> Pgbus::Process::Dispatcher constants
- dead_letter_queue_suffix -> Pgbus::DEAD_LETTER_SUFFIX (frozen)
%w[
  pool_size
  notify_throttle_ms
  circuit_breaker_threshold circuit_breaker_base_backoff circuit_breaker_max_backoff
  archive_compaction_interval archive_compaction_batch_size
  dead_letter_queue_suffix
].freeze
KNOWN_SETTINGS =

Settings whose default we know how to compute by inspecting Pgbus::Configuration.new. Any setting not listed here is emitted as-is (we can’t tell if it matches the default).

%w[
  queue_prefix default_queue pool_timeout listen_notify
  visibility_timeout max_retries idempotency_ttl
  max_jobs_per_worker max_memory_mb max_worker_lifetime
  dispatch_interval prefetch_limit
  circuit_breaker_enabled
  archive_retention
  outbox_enabled outbox_poll_interval outbox_batch_size outbox_retention
  stats_enabled stats_retention
  recurring_schedule_interval recurring_execution_retention skip_recurring
  polling_interval default_priority priority_levels
  return_to_app_url workers
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(envs_hash) ⇒ ConfigConverter

Returns a new instance of ConfigConverter.



84
85
86
87
88
# File 'lib/pgbus/generators/config_converter.rb', line 84

def initialize(envs_hash)
  @envs = (envs_hash || {}).reject { |env, _| env.start_with?("default") }
  @envs = { "production" => envs_hash } if @envs.empty? && envs_hash
  @defaults = build_defaults
end

Class Method Details

.from_hash(envs_hash) ⇒ Object



80
81
82
# File 'lib/pgbus/generators/config_converter.rb', line 80

def self.from_hash(envs_hash)
  new(envs_hash).render
end

.from_yaml(path) ⇒ Object

Raises:



73
74
75
76
77
78
# File 'lib/pgbus/generators/config_converter.rb', line 73

def self.from_yaml(path)
  raise Error, "config file not found: #{path}" unless File.exist?(path)

  parsed = YAML.safe_load_file(path, aliases: true, permitted_classes: [Symbol])
  from_hash(parsed)
end

Instance Method Details

#renderObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pgbus/generators/config_converter.rb', line 90

def render
  lines = []
  lines << "# frozen_string_literal: true"
  lines << "#"
  lines << "# Generated by `rails generate pgbus:update` from config/pgbus.yml."
  lines << "# Review and adjust as needed, then delete config/pgbus.yml."
  lines << ""
  lines << "Pgbus.configure do |c|"

  body = render_body
  body.each { |line| lines << "  #{line}" }

  lines << "end"
  "#{lines.join("\n")}\n"
end