From b0a91eed6822f9c97b4b6ef98c600e19f4bf180a Mon Sep 17 00:00:00 2001 From: DmitryTsepelev Date: Mon, 1 Feb 2021 13:57:27 +0300 Subject: [PATCH] Delay initialization until Rails is fully initialized --- README.md | 34 +++++++++++++++++++-------------- lib/sidekiq/grouping.rb | 2 +- lib/sidekiq/grouping/railtie.rb | 11 +++++++++++ 3 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 lib/sidekiq/grouping/railtie.rb diff --git a/README.md b/README.md index b89cf10..3534d49 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,26 @@ Useful for: *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 + +Add this line to your application's Gemfile: + + gem 'sidekiq-grouping' + +And then execute: + + $ bundle + +Or install it yourself as: + + $ gem install sidekiq-grouping + +If you're using the gem outside of Rails, run Grouping when application is initialized: + +```ruby +Sidekiq::Grouping.start! if Sidekiq.server? +``` + ## Usage Create a worker: @@ -195,20 +215,6 @@ end ``` -## Installation - -Add this line to your application's Gemfile: - - gem 'sidekiq-grouping' - -And then execute: - - $ bundle - -Or install it yourself as: - - $ gem install sidekiq-grouping - ## Contributing 1. Fork it ( http://github.com/gzigzigzeo/sidekiq-grouping/fork ) diff --git a/lib/sidekiq/grouping.rb b/lib/sidekiq/grouping.rb index 1463adb..e9c464b 100644 --- a/lib/sidekiq/grouping.rb +++ b/lib/sidekiq/grouping.rb @@ -3,6 +3,7 @@ require "active_support/core_ext/numeric/time" require "sidekiq/grouping/version" require "concurrent" +require "sidekiq/grouping/railtie" if defined?(Rails) module Sidekiq::Grouping autoload :Config, "sidekiq/grouping/config" @@ -50,4 +51,3 @@ def start! end end -Sidekiq::Grouping.start! if Sidekiq.server? diff --git a/lib/sidekiq/grouping/railtie.rb b/lib/sidekiq/grouping/railtie.rb new file mode 100644 index 0000000..f910e3c --- /dev/null +++ b/lib/sidekiq/grouping/railtie.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Sidekiq + module Grouping + class Railtie < ::Rails::Railtie + config.after_initialize do + Sidekiq::Grouping.start! if Sidekiq.server? + end + end + end +end