-
-
Notifications
You must be signed in to change notification settings - Fork 200
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
Handle database connection drops #296
Comments
@nelvalx thanks for reporting this! GoodJob can handle this state better. I think the solution is to detect the condition and add delay before GoodJob tries to reconnect again. And provide a better log message. Do you think 15 seconds is an appropriate delay before retrying? I'll have to do some research about how ActiveRecord manages connection lifecycle. The GoodJob::Notifier was modeled on ActionCable's PG adapter, and it may have a solution or suffer from the same problem: https://github.com/rails/rails/blob/83217025a171593547d1268651b446d3533e2019/actioncable/lib/action_cable/subscription_adapter/postgresql.rb |
For now I have a 30 seconds delay, but I agree that 15 seconds is a good average. This is how I patch the configuration until it's solved. The other 5 seconds delay is preventing 100% cpu usage over any unexpected error. GoodJob.on_thread_error = lambda do |exception|
# https://github.com/bensheldon/good_job/issues/296
case exception
when ActiveRecord::ConnectionNotEstablished, PG::UnableToSend
sleep(30)
when ActiveRecord::StatementInvalid
GoodJob::Job.advisory_lockable_column = 'id' if GoodJob::Job._advisory_lockable_column.blank?
end
sleep(5)
end |
@nelvalx I'm curious, are you seeing this in production? Could you give me a bit more context about how this happens? (I initially imagined that this was what happens to me: I'm doing development and forget to start the database process.) |
I'm in development. If you shutdown the database after rails start you only get the error loops while the database is down. My current configuration: # app/jobs/application_job.rb
class ApplicationJob < ActiveJob::Base
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked
retry_on StandardError, wait: :exponentially_longer, attempts: 20
# Most jobs are safe to ignore if the underlying records are no longer available
discard_on ActiveJob::DeserializationError
end # config/initializers/good_job.rb
GoodJob.retry_on_unhandled_error = false
GoodJob.preserve_job_records = true
GoodJob.on_thread_error = lambda do |exception|
# https://github.com/bensheldon/good_job/issues/296
case exception
when ActiveRecord::ConnectionNotEstablished, PG::UnableToSend
sleep(30)
when ActiveRecord::StatementInvalid
GoodJob::Job.advisory_lockable_column = 'id' if GoodJob::Job._advisory_lockable_column.blank?
end
sleep(5)
end # config/application.rb
...
# Active Job adapter
config.active_job.queue_adapter = :good_job
config.good_job.execution_mode = :async_server
config.good_job.max_threads = 2
config.good_job.poll_interval = 5.minutes.to_i
config.good_job.shutdown_timeout = 25.seconds.to_i
... |
@nelvalx I just released a fix for this in |
If I break the database connection (with a service postgresql restart):
The notifier is still unsubscribed! Only polling is working but executing only one job at the time. |
If the database connection is lost, good_job will be crazy looping with the error:
If the connection to postgresql is inexistent before start good_job, when that connection become available good_job has the following error when polling:
The text was updated successfully, but these errors were encountered: