pgbus 0.3.8 → 0.3.9

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: 76f2d5b122b3a379c36fcef393e8b7eabcfdbc7c8534d76948b1349dd97cb58b
4
- data.tar.gz: 4059948c68407aebeebd6f45d3b550158c6d38455c95cf255ccf1faf2d10889a
3
+ metadata.gz: 0dd5ee830e98f086b75601600307b11a2729d20076495563151ba6a0745b43bd
4
+ data.tar.gz: 2bb7b5f38f4a2c3186aae1406f7aa843a7d18601aab1065dbbcc3303784682e8
5
5
  SHA512:
6
- metadata.gz: b21741c2de21716ce47c0246c8de08bb440e9a4dfc750bc60e95484b97fe9c7626b357c47c4b2b7583c6d8273fef08b363aa84ed5424bf21a64bb64519ce64b7
7
- data.tar.gz: 48983a1abcbfc000936caf9ff2cac0d7451633aee2ee05c68c4168b84c0aed4ee38e4108501214b91c5045e9cc97f269f55c3e9e6050d778ac06b07a11c6c45d
6
+ metadata.gz: e33636a93205ce7bb09bef53b626bb275905c980cf6792ec9716baed0bc3265d150816dcaffd5f77f6cd08541ab03fceb0cf76315e9bce9ad4bfe76035b4dad8
7
+ data.tar.gz: 1aff3f96586bc6a8593cac6cb7ea7f7206899ee1d43907b3717f9cd1433cc9c818537f6ad1d8ecad2dad7f492dbb216d17c0e4b80dd0276f6a45b5e4c619dc6d
@@ -202,15 +202,49 @@ module Pgbus
202
202
  elsif connection_params
203
203
  connection_params
204
204
  elsif defined?(ActiveRecord::Base)
205
- if connects_to
206
- -> { Pgbus::BusRecord.connection.raw_connection }
207
- else
208
- -> { ActiveRecord::Base.connection.raw_connection }
209
- end
205
+ # Extract connection config from ActiveRecord so pgmq-ruby creates its
206
+ # own dedicated PG connections. Sharing AR's raw_connection via a Proc
207
+ # is NOT thread-safe: the ConnectionPool caches the PG::Connection from
208
+ # whichever thread first called the Proc, then hands that same object to
209
+ # other threads — causing nil results, segfaults, and data corruption
210
+ # when AR and PGMQ issue concurrent queries on the same connection.
211
+ extract_ar_connection_hash
210
212
  else
211
213
  raise ConfigurationError, "No database connection configured. " \
212
214
  "Set Pgbus.configuration.database_url, connection_params, or use with Rails."
213
215
  end
214
216
  end
217
+
218
+ private
219
+
220
+ def extract_ar_connection_hash
221
+ base = connects_to ? Pgbus::BusRecord : ActiveRecord::Base
222
+ db_config = base.connection_db_config
223
+
224
+ # Rails 7.1+ db_config.configuration_hash returns the full config
225
+ config_hash = db_config.configuration_hash
226
+
227
+ {
228
+ host: config_hash[:host] || "localhost",
229
+ port: (config_hash[:port] || 5432).to_i,
230
+ dbname: config_hash[:database],
231
+ user: config_hash[:username],
232
+ password: config_hash[:password]
233
+ }.compact
234
+ rescue StandardError => e
235
+ # Fallback to Proc path if AR config extraction fails (e.g., adapter
236
+ # doesn't expose standard config keys). Log a warning since this path
237
+ # is not thread-safe.
238
+ Pgbus.logger.warn do
239
+ "[Pgbus] Could not extract AR connection config (#{e.class}: #{e.message}). " \
240
+ "Falling back to shared raw_connection — this is NOT thread-safe with multiple workers. " \
241
+ "Set Pgbus.configuration.database_url explicitly for thread-safe operation."
242
+ end
243
+ if connects_to
244
+ -> { Pgbus::BusRecord.connection.raw_connection }
245
+ else
246
+ -> { ActiveRecord::Base.connection.raw_connection }
247
+ end
248
+ end
215
249
  end
216
250
  end
data/lib/pgbus/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pgbus
4
- VERSION = "0.3.8"
4
+ VERSION = "0.3.9"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgbus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.8
4
+ version: 0.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikael Henriksson