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 +4 -4
- data/lib/pgbus/configuration.rb +39 -5
- data/lib/pgbus/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0dd5ee830e98f086b75601600307b11a2729d20076495563151ba6a0745b43bd
|
|
4
|
+
data.tar.gz: 2bb7b5f38f4a2c3186aae1406f7aa843a7d18601aab1065dbbcc3303784682e8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e33636a93205ce7bb09bef53b626bb275905c980cf6792ec9716baed0bc3265d150816dcaffd5f77f6cd08541ab03fceb0cf76315e9bce9ad4bfe76035b4dad8
|
|
7
|
+
data.tar.gz: 1aff3f96586bc6a8593cac6cb7ea7f7206899ee1d43907b3717f9cd1433cc9c818537f6ad1d8ecad2dad7f492dbb216d17c0e4b80dd0276f6a45b5e4c619dc6d
|
data/lib/pgbus/configuration.rb
CHANGED
|
@@ -202,15 +202,49 @@ module Pgbus
|
|
|
202
202
|
elsif connection_params
|
|
203
203
|
connection_params
|
|
204
204
|
elsif defined?(ActiveRecord::Base)
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
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