You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.5 KiB

2 months ago
  1. module Semantic
  2. module NotificationUtil
  3. class << self
  4. # pattern could be either a string 'start_processing.action_controller' or a regex /\.action_controller$/
  5. # FIXME: weird behaviour, order impact!!!!
  6. # For instance:
  7. # OK
  8. # NotificationUtil.clear_subscribers(/\.action_controller$/)
  9. # NotificationUtil.clear_subscribers(/\.action_view$/)
  10. # NOPE
  11. # NotificationUtil.clear_subscribers(/\.action_view$/)
  12. # NotificationUtil.clear_subscribers(/\.action_controller$/)
  13. def clear_subscribers(pattern)
  14. ActiveSupport::LogSubscriber.subscribers.each { |sub| unattach(sub, pattern) }
  15. end
  16. private
  17. def subscriber_patterns(subscriber)
  18. subscriber.patterns.respond_to?(:keys) ? subscriber.patterns.keys : subscriber.patterns
  19. end
  20. def unattach(subscriber, pattern)
  21. subscriber_patterns(subscriber).each do |sub_pattern|
  22. ActiveSupport::Notifications.notifier.listeners_for(sub_pattern).each do |sub|
  23. next unless sub.instance_variable_get(:@delegate) == subscriber
  24. next unless pattern.match(sub_pattern)
  25. puts "FOUND subscriber=#{subscriber} for sub_pattern=#{sub_pattern} with logger #{subscriber.logger.name}"
  26. puts subscriber.class.module_parent.const_source_location(subscriber.class.to_s)&.first
  27. ActiveSupport::Notifications.unsubscribe(sub)
  28. end
  29. end
  30. # ActiveSupport::LogSubscriber.subscribers.delete(subscriber)
  31. end
  32. end
  33. end
  34. end