Skip to content

Delay initialization until Rails is fully initialized - #41

Open
DmitryTsepelev wants to merge 1 commit into
gzigzigzeo:masterfrom
DmitryTsepelev:railtie-after-config
Open

Delay initialization until Rails is fully initialized#41
DmitryTsepelev wants to merge 1 commit into
gzigzigzeo:masterfrom
DmitryTsepelev:railtie-after-config

Conversation

@DmitryTsepelev

Copy link
Copy Markdown

Related to the Zeitwerk issue about occasional NameError inside Sidekiq

In some cases sidekiq-grouping starts before Rails is fully initialized and Zeitwerk initial loading is completed, which can cause situations when some classes are loaded inside the concurrent thread rather than in Zeitwerk. This PR moves gem initialization to the Railtie.

Comment thread README.md
*NOTE:* As of 1.0.6 works with Sidekiq 4.
*NOTE:* As of 1.0.8 Locking is atomic (set nx/ex) and will no longer lead to batches that are permalocked and stuck

## Installation

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Moved this section to the top because non-Rails users will have to perform an extra setup

Comment thread lib/sidekiq/grouping/railtie.rb
Comment thread lib/sidekiq/grouping.rb
end
end

Sidekiq::Grouping.start! if Sidekiq.server?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This is a breaking change, so I guess we'll need a major release

elcuervo added a commit to unsplash/sidekiq-grouping that referenced this pull request Jul 10, 2023
Directly taken from gzigzigzeo#41
There's a potential issue with the initialization that prevents the
config to be correctly loaded when in Rails.
This enforces the right timing
@MrPhantomT

Copy link
Copy Markdown

Hi there are there any plans to merge this pull-request?

@jgoerz-ehub

Copy link
Copy Markdown

We had a similar problem. In our case, the sidekiq-grouping initialization would clobber our redis URL initialization forcing the use of the default redis URL (localhost) which didn't work for our production setup. The workaround is to properly sequence the initialization. In rails, this can be done by:

# config/initializers/sidekiq.rb

# The gem runs `Sidekiq::Grouping.start!` at require time (whenever Sidekiq.server? is true),
# spawning a background poller. Requiring it here, inside after_initialize, i.e. after all
# initializers, eager loading and Zeitwerk have finished,  mirrors upstream PR #41
# (https://github.com/gzigzigzeo/sidekiq-grouping/pull/41), which is neither merged nor
# released. Deferring the poller this late guarantees Sidekiq's Redis URL is already set,
# so it builds the internal connection pool against the real Redis instead of
# memoizing the localhost fallback, and it avoids the poller autoloading app classes off
# the main thread before eager load.
Rails.application.config.after_initialize do
  require 'sidekiq/grouping'
end

And

# Gemfile
gem 'sidekiq-grouping', require: false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants