Start async adapters once ActiveRecord
and ActiveJob
have loaded, potentially before Rails.application.initialized?
#483
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A project was having trouble with Rails async not starting up. In poking at it, it appeared that GoodJob was being initialized in the bootup phase after
after_initialize
hooks had been called, but beforeRails.application.initialized?
was true. The result being that the Adapter does not initialize the async executor.Details: AFAICT Rails loads routes after the
after_initialize
hook has been called. This project used Devise, anddevise_for :user
will load theUser
model, which kicks off a loading chain explained below. Because this happened afterafter_initialize
but beforeRails.application.initialized? == true
, the GoodJob async adapter was being initialized but never started. It must be more complex though because I use Devise in my own applications and have not seen this behavior myself 🤷The difference in behavior between
after_initialize
andinitialized?
is briefly discussed in rails/rails#32237This PR tracks GoodJob's dependencies (ActiveRecord and ActiveJob) during the Rails boot process and attempts to start the async executors when all dependency conditions have been met.
The risk here is that there could be potential deadlocks because the executors may speculatively be started slightly earlier in the Rails boot process; but it concretely fixes a problem for this Rails application. And I think I've covered the bases.
I added a meaty comment to explain:
Connects to #199, #293.