panda_pal 5.16.2 → 5.16.3

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: 7c39df6be8d76f63ab12597a80696f629aac5da4a400adfa30119f303177251c
4
- data.tar.gz: 2ab2639ea53d3e698244d65313f94b5ebc6c5edcb5039efc0b7dd801658a1603
3
+ metadata.gz: a2c0409f3edd41806fcd606b52eb0c6441c43dac7b9e1eeb89c1871b6e08525f
4
+ data.tar.gz: 14b3313348391ab1c364ee9bb404af319bf93b831fc5bd552059faf41a179b71
5
5
  SHA512:
6
- metadata.gz: '06705595bcabf1959f7a2d72e7174d6944acc0876770878aa700d926d5b4beefdbb4a8af9462009a253d6eaf4a3fc3870f915291baa6490f5d2ecd756f84a275'
7
- data.tar.gz: dac25e88efe02b2982e6441506e5d17328b3ee4500627d400523f37da6f7e6093c2a5d85492a73eb5946e616bc0ae7839730620ed66deb8986c952e416fc8f83
6
+ metadata.gz: e2c460dd7ac5ff05da55ef6db233f0d266983927e6a295b469d1d19002ea833b26479c5b7db53fb6306761afc683bf3494fc07c63f1ac401f736d73ad803248b
7
+ data.tar.gz: bcccc2e5ad50d546f3258e513118e97a37f8104901436b3b3920d64986ad83159034a905cce7ded7b81529989d16d8151f0583ec7b273732e2d38be443a6dc8e
@@ -250,94 +250,71 @@ module PandaPal::Plugins::ApartmentCache
250
250
  end
251
251
  ActiveSupport::Cache::Store.send(:prepend, PandaPal::Plugins::ApartmentCache)
252
252
 
253
- if defined?(ActionCable)
254
- module PandaPal::Plugins::ActionCableApartment
255
- module Connection
256
- def tenant=(name)
257
- @tenant = name
258
- end
259
-
260
- def tenant
261
- @tenant || 'public'
262
- end
253
+ ActiveSupport.on_load(:action_cable) do
254
+ ActionCable::Connection::Base.module_eval do
255
+ def tenant=(name)
256
+ @tenant = name
263
257
  end
264
- end
265
258
 
266
- if Rails.version < '7.0'
267
- module ActionCable
268
- module Channel
269
- class Base
270
- def self.broadcasting_for(model)
271
- # Rails 5 #stream_for passes #channel_name as part of model. Rails 6 doesn't and includes it via #broadcasting_for.
272
- model = [channel_name, model] unless model.is_a?(Array)
273
- serialize_broadcasting([ Apartment::Tenant.current, model ])
274
- end
275
-
276
- def self.serialize_broadcasting(object)
277
- case
278
- when object.is_a?(Array)
279
- object.map { |m| serialize_broadcasting(m) }.join(":")
280
- when object.respond_to?(:to_gid_param)
281
- object.to_gid_param
282
- else
283
- object.to_param
284
- end
285
- end
286
- end
287
- end
259
+ def tenant
260
+ @tenant || 'public'
288
261
  end
262
+ end
289
263
 
290
- module PandaPal::Plugins::ActionCableApartment::Connection
291
- def dispatch_websocket_message(*args, **kwargs)
292
- Apartment::Tenant.switch(tenant) do
293
- super
294
- end
295
- end
264
+ # Include the Current Tenant in any broadcastings
265
+ if Rails.version >= '6.0'
266
+ ActionCable::Channel::Base.define_singleton_method(:broadcasting_for) do |*args|
267
+ cconn = ActionCable.server.worker_pool.connection
268
+ items = [cconn&.tenant || Apartment::Tenant.current, channel_name, *args]
269
+ serialize_broadcasting(items)
296
270
  end
297
271
  else
298
272
  module ActionCable
299
273
  module Channel
300
274
  class Base
301
- def self.broadcasting_for(*args)
302
- cconn = ActionCable.server.worker_pool.connection
303
- items = [cconn&.tenant || Apartment::Tenant.current, channel_name, *args]
304
- serialize_broadcasting(items)
275
+ def broadcasting_for(model)
276
+ super([ Apartment::Tenant.current, model ])
305
277
  end
306
278
  end
307
279
  end
308
280
  end
281
+ end
309
282
 
310
- ActionCable::Server::Worker.set_callback :work, :around do |_, blk|
311
- pten = Thread.current[:cable_tenant]
312
- Thread.current[:cable_tenant] = connection.tenant
313
- blk.call
314
- ensure
315
- Thread.current[:cable_tenant] = pten
316
- end
283
+ # Lazily switch any worker threads to the correct tenant when they are working
284
+ # Actively calling `switch_tenant` for checks out a DB connection and calls `SET search_path`.
285
+ # The message processing may not interface with the DB, so this would be a huge waste.
286
+ # Instead, we ensure that the thread will trigger a :checkout if it needs a connection,
287
+ # at which time we hack-in the correct tenant/schema.
288
+
289
+ ActionCable::Server::Worker.set_callback :work, :around do |_, blk|
290
+ # Bit of a hack, but ensures the adapter is initialized (since such may incur DB calls (requiring a connection)
291
+ # which would then cause `checkout` to recurse)
292
+ Apartment::Tenant.adapter
293
+
294
+ # If the current thread already has a connection checked out, release it back to the pool so that the later after_checkout
295
+ # callback can set the schema properly.
296
+ pool = Apartment.connection_class.connection_pool
297
+ pool.release_connection if pool.active_connection?
298
+
299
+ Thread.current[:cable_tenant] = connection.tenant
300
+ blk.call
301
+ ensure
302
+ Thread.current[:cable_tenant] = nil
303
+ end
317
304
 
305
+ ActiveSupport.on_load(:active_record) do
318
306
  if Apartment::Tenant.adapter.is_a?(Apartment::Adapters::PostgresqlSchemaAdapter)
319
- module ApartmentConnPoolMixin
320
- # Allows cable channels to lazy-load the tenant schema on first use
321
- # (Using `switch` will ping the DB even if no queries are made)
322
- def acquire_connection(...)
323
- super.tap do |conn|
324
- if ct = Thread.current[:cable_tenant]
325
- Thread.current[:cable_tenant] = nil
326
- adapter = Apartment::Tenant.adapter
327
- # Apartment::Tenant.switch!(ct)
328
- Thread.current[:cable_tenant] = ct
329
- adapter.instance_variable_set(:@current, ct)
330
- conn.schema_search_path = adapter.send :full_search_path
331
- end
332
- end
307
+ ActiveRecord::Base.connection.class.set_callback :checkout, :after do |conn|
308
+ next unless conn.pool == Apartment.connection_class.connection_pool
309
+
310
+ if (ct = Thread.current[:cable_tenant]).present?
311
+ adapter = Apartment::Tenant.adapter
312
+ adapter.instance_variable_set(:@current, ct)
313
+ conn.schema_search_path = adapter.send :full_search_path
333
314
  end
334
315
  end
335
-
336
- ActiveRecord::ConnectionAdapters::ConnectionPool.prepend(ApartmentConnPoolMixin)
337
316
  end
338
317
  end
339
-
340
- ActionCable::Connection::Base.prepend(PandaPal::Plugins::ActionCableApartment::Connection)
341
318
  end
342
319
 
343
320
  if defined?(Delayed)
@@ -1,3 +1,3 @@
1
1
  module PandaPal
2
- VERSION = "5.16.2"
2
+ VERSION = "5.16.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: panda_pal
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.16.2
4
+ version: 5.16.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Instructure CustomDev