charkost / prosopite

:mag: Rails N+1 queries auto-detection with zero false positives / false negatives
Apache License 2.0
1.53k stars 46 forks source link

Add `Prosopite.pause` and `Prosopite.resume` #32

Closed ghiculescu closed 2 years ago

ghiculescu commented 2 years ago

I'm trying to migrate over from Bullet. We have a Delayed Job plugin at the moment, that disables Bullet while a background job is running and re-enables it when the job is complete. This way we can run Bullet in tests but have it only run on foreground code.

It looks this:

module Delayed
  module Plugins
    class BulletDisable < Plugin
      callbacks do |lifecycle|
        lifecycle.around(:invoke_job) do |job, *args, &block|
          @bullet_was_enabled = Bullet.enabled?
          Bullet.enabled = false
          block.call(job, *args)
          Bullet.enabled = @bullet_was_enabled
        end
      end
    end
  end
end

This PR adds Prosopite.pause and Prosopite.resume which allows you to do the same thing with Prosopite. So our plugin would become this:

module Delayed
  module Plugins
    class ProsopitePause < Plugin
      callbacks do |lifecycle|
        lifecycle.around(:invoke_job) do |job, *args, &block|
          Prosopite.pause
          block.call(job, *args)
          Prosopite.resume
        end
      end
    end
  end
end
charkost commented 2 years ago

Awesome @ghiculescu, thank you!