Skip to content

Commit

Permalink
Add LISTEN support for JRuby / JDBC
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon committed Jan 6, 2022
1 parent bf5d512 commit 0c98e48
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
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
2 changes: 1 addition & 1 deletion 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 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

0 comments on commit 0c98e48

Please sign in to comment.