Skip to content
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

Add JRuby / JDBC support for LISTEN #479

Merged
merged 1 commit into from
Jan 9, 2022
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
21 changes: 12 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
chef-utils (17.9.18)
concurrent-ruby
chef-utils (16.6.14)
childprocess (3.0.0)
coderay (1.1.3)
concurrent-ruby (1.1.9)
Expand All @@ -158,27 +157,31 @@ GEM
erubi (1.10.0)
et-orbi (1.2.6)
tzinfo
faraday (1.8.0)
faraday (1.9.3)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1)
faraday-httpclient (~> 1.0.1)
faraday-httpclient (~> 1.0)
faraday-multipart (~> 1.0)
faraday-net_http (~> 1.0)
faraday-net_http_persistent (~> 1.1)
faraday-net_http_persistent (~> 1.0)
faraday-patron (~> 1.0)
faraday-rack (~> 1.0)
multipart-post (>= 1.2, < 3)
faraday-retry (~> 1.0)
ruby2_keywords (>= 0.0.4)
faraday-em_http (1.0.0)
faraday-em_synchrony (1.0.0)
faraday-excon (1.1.0)
faraday-http-cache (2.2.0)
faraday (>= 0.8)
faraday-httpclient (1.0.1)
faraday-multipart (1.0.2)
multipart-post (>= 1.2, < 3)
faraday-net_http (1.0.1)
faraday-net_http_persistent (1.2.0)
faraday-patron (1.0.0)
faraday-rack (1.0.0)
faraday-retry (1.0.3)
ffi (1.15.4)
ffi (1.15.4-java)
fiber-local (1.0.0)
Expand Down Expand Up @@ -243,7 +246,7 @@ GEM
sawyer (~> 0.8.0, >= 0.5.3)
optimist (3.0.1)
parallel (1.21.0)
parser (3.0.3.2)
parser (3.1.0.0)
ast (~> 2.4.1)
pg (1.2.3)
protocol-hpack (1.4.2)
Expand Down Expand Up @@ -338,7 +341,7 @@ GEM
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.15.1)
parser (>= 3.0.1.1)
rubocop-performance (1.13.0)
rubocop-performance (1.13.1)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rails (2.13.0)
Expand Down Expand Up @@ -367,7 +370,7 @@ GEM
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
thor (1.1.0)
thor (1.2.1)
timers (4.3.3)
tomlrb (2.0.1)
tzinfo (2.0.4)
Expand Down
14 changes: 9 additions & 5 deletions lib/good_job/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ class Notifier

include Notifier::ProcessRegistration

# Raised if the Database adapter does not implement LISTEN.
AdapterCannotListenError = Class.new(StandardError)

# Default Postgres channel for LISTEN/NOTIFY
CHANNEL = 'good_job'
# Defaults for instance of Concurrent::ThreadPoolExecutor
Expand Down Expand Up @@ -129,8 +126,6 @@ def restart(timeout: -1)
# @!visibility private
# @return [void]
def listen_observer(_time, _result, thread_error)
return if thread_error.is_a? AdapterCannotListenError

if thread_error
GoodJob._on_thread_error(thread_error)
ActiveSupport::Notifications.instrument("notifier_notify_error.good_job", { error: thread_error })
Expand Down Expand Up @@ -214,6 +209,15 @@ def wait_for_notify
raw_connection.wait_for_notify(WAIT_INTERVAL) do |channel, _pid, payload|
yield(channel, payload)
end
elsif raw_connection.respond_to?(:jdbc_connection)
raw_connection.execute_query("SELECT 1")
notifications = raw_connection.jdbc_connection.getNotifications
Array(notifications).each do |notification|
channel = notification.getName
payload = notification.getParameter
yield(channel, payload)
end
sleep WAIT_INTERVAL
else
sleep WAIT_INTERVAL
end
Expand Down
7 changes: 4 additions & 3 deletions spec/lib/good_job/notifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
end
end

describe '#listen', skip_if_java: true do
describe '#listen' do
it 'loops until it receives a command' do
stub_const 'RECEIVED_MESSAGE', Concurrent::AtomicBoolean.new(false)

Expand All @@ -42,10 +42,11 @@

notifier = described_class.new
sleep_until(max: 5, increments_of: 0.5) { notifier.listening? }

described_class.notify(true)
notifier.shutdown
wait_until(max: 5, increments_of: 0.5) { expect(on_thread_error).to have_received(:call).at_least(:once).with instance_of(ExpectedError) }

expect(on_thread_error).to have_received(:call).at_least(:once).with instance_of(ExpectedError)
notifier.shutdown
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/test_app/config/initializers/good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

GoodJob.retry_on_unhandled_error = false
GoodJob.preserve_job_records = true
GoodJob.on_thread_error = -> (error) { Rails.logger.warn(error) }
GoodJob.on_thread_error = -> (error) { Rails.logger.warn("#{error}\n#{error.backtrace}") }

Rails.application.configure do
config.good_job.enable_cron = ActiveModel::Type::Boolean.new.cast(ENV.fetch('GOOD_JOB_ENABLE_CRON', true))
Expand Down