Skip to content

support nil logger and ActiveSupport::Logger not Logger #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/solid_queue/processes/poller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def poll
end

def with_polling_volume
if SolidQueue.silence_polling?
if SolidQueue.silence_polling? && ActiveRecord::Base.logger
ActiveRecord::Base.logger.silence { yield }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better just use safe-navigation ActiveRecord::Base.logger&.silence ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it will not yield

Copy link

@nbulaj nbulaj Mar 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But why? 🤔

irb(main):003> l = Logger.new(STDOUT)
irb(main):004> l.info { "OK" }
I, [2024-03-22T15:10:27.939697 #68513]  INFO -- : OK
=> true
irb(main):005> l&.info { "OK" }
I, [2024-03-22T15:10:31.957994 #68513]  INFO -- : OK
=> true


irb(main):008> n = nil
=> nil
irb(main):009> n&.info { "OK"  }
=> nil

Or I'd better have to think an example with yield, yes

else
yield
Expand Down
52 changes: 36 additions & 16 deletions test/unit/dispatcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,35 @@ class DispatcherTest < ActiveSupport::TestCase

test "polling queries are logged" do
log = StringIO.new
old_logger, ActiveRecord::Base.logger = ActiveRecord::Base.logger, ActiveSupport::Logger.new(log)
old_silence_polling, SolidQueue.silence_polling = SolidQueue.silence_polling, false

@dispatcher.start
sleep 0.5
with_active_record_logger(ActiveSupport::Logger.new(log)) do
with_polling(silence: false) do
@dispatcher.start
sleep 0.2
end
end

assert_match /SELECT .* FROM .solid_queue_scheduled_executions. WHERE/, log.string
ensure
ActiveRecord::Base.logger = old_logger
SolidQueue.silence_polling = old_silence_polling
end

test "polling queries can be silenced" do
log = StringIO.new
old_logger, ActiveRecord::Base.logger = ActiveRecord::Base.logger, ActiveSupport::Logger.new(log)
old_silence_polling, SolidQueue.silence_polling = SolidQueue.silence_polling, true

@dispatcher.start
sleep 0.5
with_active_record_logger(ActiveSupport::Logger.new(log)) do
with_polling(silence: true) do
@dispatcher.start
sleep 0.2
end
end

assert_no_match /SELECT .* FROM .solid_queue_scheduled_executions. WHERE/, log.string
ensure
ActiveRecord::Base.logger = old_logger
SolidQueue.silence_polling = old_silence_polling
end

test "silencing polling queries when there's no Active Record logger" do
with_active_record_logger(nil) do
with_polling(silence: true) do
@dispatcher.start
sleep 0.2
end
end
end

test "run more than one instance of the dispatcher without recurring tasks" do
Expand Down Expand Up @@ -121,4 +126,19 @@ class DispatcherTest < ActiveSupport::TestCase
assert_equal 1, run_at_times[i + 1] - run_at_times[i]
end
end

private
def with_polling(silence:)
old_silence_polling, SolidQueue.silence_polling = SolidQueue.silence_polling, silence
yield
ensure
SolidQueue.silence_polling = old_silence_polling
end

def with_active_record_logger(logger)
old_logger, ActiveRecord::Base.logger = ActiveRecord::Base.logger, logger
yield
ensure
ActiveRecord::Base.logger = old_logger
end
end
52 changes: 36 additions & 16 deletions test/unit/worker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,35 @@ class WorkerTest < ActiveSupport::TestCase

test "polling queries are logged" do
log = StringIO.new
old_logger, ActiveRecord::Base.logger = ActiveRecord::Base.logger, ActiveSupport::Logger.new(log)
old_silence_polling, SolidQueue.silence_polling = SolidQueue.silence_polling, false

@worker.start
sleep 0.2
with_active_record_logger(ActiveSupport::Logger.new(log)) do
with_polling(silence: false) do
@worker.start
sleep 0.2
end
end

assert_match /SELECT .* FROM .solid_queue_ready_executions. WHERE .solid_queue_ready_executions...queue_name./, log.string
ensure
ActiveRecord::Base.logger = old_logger
SolidQueue.silence_polling = old_silence_polling
end

test "polling queries can be silenced" do
log = StringIO.new
old_logger, ActiveRecord::Base.logger = ActiveRecord::Base.logger, ActiveSupport::Logger.new(log)
old_silence_polling, SolidQueue.silence_polling = SolidQueue.silence_polling, true

@worker.start
sleep 0.2
with_active_record_logger(ActiveSupport::Logger.new(log)) do
with_polling(silence: true) do
@worker.start
sleep 0.2
end
end

assert_no_match /SELECT .* FROM .solid_queue_ready_executions. WHERE .solid_queue_ready_executions...queue_name./, log.string
ensure
ActiveRecord::Base.logger = old_logger
SolidQueue.silence_polling = old_silence_polling
end

test "silencing polling queries when there's no Active Record logger" do
with_active_record_logger(nil) do
with_polling(silence: true) do
@worker.start
sleep 0.2
end
end
end

test "run inline" do
Expand All @@ -100,4 +105,19 @@ class WorkerTest < ActiveSupport::TestCase

assert_equal 5, JobResult.where(queue_name: :background, status: "completed", value: :immediate).count
end

private
def with_polling(silence:)
old_silence_polling, SolidQueue.silence_polling = SolidQueue.silence_polling, silence
yield
ensure
SolidQueue.silence_polling = old_silence_polling
end

def with_active_record_logger(logger)
old_logger, ActiveRecord::Base.logger = ActiveRecord::Base.logger, logger
yield
ensure
ActiveRecord::Base.logger = old_logger
end
end
Loading