wal 0.0.23 → 0.0.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3a7144e35e8bfe1df54e18fa1e4b9c94e4bb6926ed0de011e0e4006ce08c44cd
4
- data.tar.gz: 21196f54b92390273288aac5fed7b5fdcd79842dda0d63cbb0cada583bca8cd3
3
+ metadata.gz: 7ff2f0a5d9392268db27728b053f29ac897649314c72b857b0d43303bb8face1
4
+ data.tar.gz: a378a4d4c4e5f3f1e6d9a988fe61947683c52614223e27a67371b4042434a2cb
5
5
  SHA512:
6
- metadata.gz: 88be295b55c90871dc4aa8731e21c03fff1b890e59a3b9ec000276c5d0b2dad96660b1686328af6e208084fc5389f90c8925dbc27fb29de56e535063eaf20899
7
- data.tar.gz: a13bd85ed047f5b7bfcfe839bb0ff8643587ae6c6ca81ef4f56809a96e4daff6a6f7423c9eff3a715cfa72f27e84b99765f003b4d40b0eacabf0d73cc7311bbc
6
+ metadata.gz: 5514c83c92062744f25cdc2f7f08aaa79cdbd4bc7cd49c6028de964f3d5c02a0a0b8d2d3b8f3a6cdf69954adb45f5a89866f5fdd193cebfd1cb1fc24049d3ba9
7
+ data.tar.gz: a29bb6e5c5cc9795e82d88d15f4e820cc88b651f58950c67120dc988454aa6230d0e85af25e016289402c5bb5a6b1db9341e039fdc350155a86710190be6bad2
data/exe/wal CHANGED
@@ -69,7 +69,7 @@ class Wal::LoggingReplicator
69
69
  end
70
70
  end
71
71
 
72
- if cli["watch"]
72
+ def watch(db_config, cli)
73
73
  watcher = cli["--watcher"].constantize.new
74
74
  use_temporary_slot = cli["--tmp-slot"] || false
75
75
  replication_slot = cli["--slot"]
@@ -82,46 +82,50 @@ if cli["watch"]
82
82
  replicator = Wal::LoggingReplicator.new(replication_slot, replicator)
83
83
  replicator.replicate_forever(watcher, publications:)
84
84
  puts "Watcher finished for #{replication_slot}"
85
+ end
85
86
 
86
- elsif cli["start"]
87
- slots = YAML.load_file(cli["<config-file>"])["slots"]
88
-
89
- workers = slots.map do |slot, config|
90
- watcher = config["watcher"].constantize.new
91
- temporary = config["temporary"] || false
92
- publications = config["publications"] || []
93
- replicator_class = config["replicator"].presence&.constantize || Wal::Replicator
94
- max_retries = config["retries"]&.to_i || (2**32 - 1)
95
- retries = 0
96
- backoff = config["retry_backoff"]&.to_f || 1
97
- backoff_expoent = config["retry_backoff_expoent"]&.to_f
98
-
99
- Thread.new(slot, watcher, temporary, publications) do |replication_slot, watcher, use_temporary_slot, publications|
100
- replication_slot = "#{replication_slot}_#{SecureRandom.alphanumeric(4)}" if use_temporary_slot
101
- puts "Watcher started for #{replication_slot} slot (#{publications.join(", ")})"
102
-
87
+ def start_worker(db_config, slot, config)
88
+ watcher = config["watcher"].constantize.new
89
+ temporary = config["temporary"] || false
90
+ publications = config["publications"] || []
91
+ replicator_class = config["replicator"].presence&.constantize || Wal::Replicator
92
+ max_retries = config["retries"]&.to_i || (2**32 - 1)
93
+ retries = 0
94
+ backoff = config["retry_backoff"]&.to_f || 1
95
+ backoff_expoent = config["retry_backoff_expoent"]&.to_f
96
+
97
+ Thread.new(slot, watcher, temporary, publications) do |replication_slot, watcher, use_temporary_slot, publications|
98
+ replication_slot = "#{replication_slot}_#{SecureRandom.alphanumeric(4)}" if use_temporary_slot
99
+ puts "Watcher started for #{replication_slot} slot (#{publications.join(", ")})"
100
+
101
+ begin
103
102
  replicator = replicator_class.new(replication_slot:, use_temporary_slot:, db_config:)
104
103
  replicator = Wal::LoggingReplicator.new(replication_slot, replicator)
105
-
106
- begin
107
- replicator.replicate_forever(watcher, publications:)
108
- rescue StandardError => err
109
- if retries < max_retries
110
- Wal.logger&.error("[#{replication_slot}] Error #{err}")
111
- retries += 1
112
- backoff_time = backoff_expoent ? (backoff * retries) ** backoff_expoent : backoff
113
- puts "Restarting #{replication_slot} in #{backoff_time.floor(2)}s..."
114
- sleep backoff_time
115
- puts "Restarting #{replication_slot}"
116
- retry
117
- end
118
- raise
104
+ replicator.replicate_forever(watcher, publications:)
105
+ rescue StandardError => err
106
+ if retries < max_retries
107
+ Wal.logger&.error("[#{replication_slot}] Error #{err}")
108
+ retries += 1
109
+ backoff_time = backoff_expoent ? (backoff * retries) ** backoff_expoent : backoff
110
+ puts "Restarting #{replication_slot} in #{backoff_time.floor(2)}s..."
111
+ sleep backoff_time
112
+ puts "Restarting #{replication_slot}"
113
+ retry
119
114
  end
115
+ raise
116
+ end
120
117
 
121
- puts "Watcher finished for #{replication_slot}"
118
+ puts "Watcher finished for #{replication_slot}"
122
119
 
123
- Process.kill("TERM", Process.pid)
124
- end
120
+ Process.kill("TERM", Process.pid)
121
+ end
122
+ end
123
+
124
+ def start(db_config, cli)
125
+ slots = YAML.load_file(cli["<config-file>"])["slots"]
126
+
127
+ workers = slots.map do |slot, config|
128
+ start_worker(db_config, slot, config)
125
129
  end
126
130
 
127
131
  ping = Thread.new do
@@ -147,3 +151,9 @@ elsif cli["start"]
147
151
 
148
152
  workers.each(&:join)
149
153
  end
154
+
155
+ if cli["watch"]
156
+ watch(db_config, cli)
157
+ elsif cli["start"]
158
+ start(db_config, cli)
159
+ end
data/lib/wal/railtie.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "rails/railtie"
4
4
 
5
- module Waldit
5
+ module Wal
6
6
  class Railtie < Rails::Railtie
7
7
  generators do
8
8
  require_relative "generators/migration"
@@ -13,5 +13,19 @@ module Waldit
13
13
  ActiveRecord::Migration.include Wal::Migration
14
14
  ActiveRecord::Schema.include Wal::Migration
15
15
  end
16
+
17
+ config.after_initialize do
18
+ # "Borrowed" from Sidekiq
19
+ # https://github.com/sidekiq/sidekiq/blob/61e27d20b1ed62f203eee6ae2b549f2e53db14c9/lib/sidekiq/rails.rb#L38-L58
20
+ unless Rails.logger == Wal.logger || ActiveSupport::Logger.logger_outputs_to?(Rails.logger, $stdout)
21
+ if Rails.logger.respond_to?(:broadcast_to)
22
+ Rails.logger.broadcast_to(Wal.logger)
23
+ elsif ActiveSupport::Logger.respond_to?(:broadcast)
24
+ Rails.logger.extend(ActiveSupport::Logger.broadcast(Wal.logger))
25
+ else
26
+ Rails.logger = ActiveSupport::BroadcastLogger.new(Rails.logger, Wal.logger)
27
+ end
28
+ end
29
+ end
16
30
  end
17
31
  end
data/lib/wal/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wal
4
- VERSION = "0.0.23"
4
+ VERSION = "0.0.25"
5
5
  end
data/lib/wal.rb CHANGED
@@ -14,6 +14,10 @@ require_relative "wal/version"
14
14
  module Wal
15
15
  class << self
16
16
  attr_accessor :logger
17
+
18
+ def logger
19
+ @logger ||= Logger.new($stdout, level: :info)
20
+ end
17
21
  end
18
22
 
19
23
  def self.configure(&block)
data/rbi/wal.rbi CHANGED
@@ -7,7 +7,7 @@ module Wal
7
7
  UpdateEvent,
8
8
  DeleteEvent,
9
9
  ) }
10
- VERSION = "0.0.21"
10
+ VERSION = "0.0.25"
11
11
 
12
12
  class << self
13
13
  sig { returns(T.class_of(Logger)) }
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.23
4
+ version: 0.0.25
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Navarro
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-10-29 00:00:00.000000000 Z
10
+ date: 2026-01-12 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: pg