From 484d68a7a1eea573d52fe12a963002925b9171e9 Mon Sep 17 00:00:00 2001 From: Nathan Griffith Date: Thu, 2 Sep 2021 12:45:07 -0400 Subject: [PATCH] Minor Rails 6.1 fix, plus autoload Delayed::Job constant (#2) This fixes an issue I was seeing on a very slim Rails 6.1 app -- essentially, `ActiveSupport.on_load(:active_record)` was not triggering at all during `rake delayed:work`, resulting in a missing constant error. Previous versions of rails seem to load `ActiveRecord::Base` during boot, but 6.1 seems to have found a way to optimize that step away. This PR moves the `delayed/job.rb` class to `app/models` and converts the gem's `Railtie` to an `Engine`, allowing it to autoload files in `app/models` by convention. In cases where we are not in a Rails app (and `Rails::Engine` is not defined), we fall back on loading active_record manually and then running a `require_relative` of the specific `job.rb` file. I based this on other "rails-engine-optional" gems I've encountered, and confirmed that it works in an `irb` console. I also removed `require 'delayed/serialization/active_record'` because it would already be loaded inside of `require 'delayed/yaml_ext'`. It also only applies to `syck` which dates back to Ruby 1.9, and we should consider removing support for it entirely. (It can still be installed via the `syck` gem, but `psych` has been the default for many, many years.) --- .rubocop_todo.yml | 75 ++++----------------------- CHANGELOG.md | 11 +++- {lib => app/models}/delayed/job.rb | 0 delayed.gemspec | 2 +- lib/delayed.rb | 9 ++-- lib/delayed/{railtie.rb => engine.rb} | 2 +- 6 files changed, 27 insertions(+), 72 deletions(-) rename {lib => app/models}/delayed/job.rb (100%) rename lib/delayed/{railtie.rb => engine.rb} (71%) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 56db08be..c2dcf3ef 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,12 +1,12 @@ # This configuration was generated by # `rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 99999` -# on 2021-04-16 17:24:59 UTC using RuboCop version 1.12.1. +# on 2021-08-28 18:08:22 UTC using RuboCop version 1.20.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 12 +# Offense count: 13 Betterment/ActiveJobPerformable: Exclude: - 'lib/delayed/performable_mailer.rb' @@ -17,11 +17,6 @@ Betterment/ActiveJobPerformable: - 'spec/autoloaded/struct.rb' - 'spec/sample_jobs.rb' -# Offense count: 1 -Betterment/Timeout: - Exclude: - - 'spec/worker_spec.rb' - # Offense count: 4 # Configuration parameters: AllowedMethods. # AllowedMethods: enums @@ -29,18 +24,6 @@ Lint/ConstantDefinitionInBlock: Exclude: - 'spec/message_sending_spec.rb' -# Offense count: 1 -# Configuration parameters: AllowComments. -Lint/EmptyFile: - Exclude: - - 'spec/delayed/serialization/test.rb' - -# Offense count: 1 -# Configuration parameters: MaximumRangeSize. -Lint/MissingCopEnableDirective: - Exclude: - - 'lib/delayed/worker.rb' - # Offense count: 2 Lint/MissingSuper: Exclude: @@ -52,35 +35,31 @@ Lint/ShadowingOuterLocalVariable: Exclude: - 'spec/helper.rb' -# Offense count: 2 +# Offense count: 1 # Configuration parameters: AllowComments, AllowNil. Lint/SuppressedException: Exclude: - 'lib/delayed/backend/base.rb' - - 'lib/delayed/worker.rb' -# Offense count: 8 +# Offense count: 4 # Configuration parameters: IgnoredMethods, CountRepeatedAttributes, Max. Metrics/AbcSize: Exclude: - - 'lib/delayed/backend/job_preparer.rb' - - 'lib/delayed/command.rb' - 'lib/delayed/message_sending.rb' - 'lib/delayed/psych_ext.rb' - 'lib/delayed/worker.rb' -# Offense count: 2 +# Offense count: 1 RSpec/AnyInstance: Exclude: - 'spec/delayed/job_spec.rb' - - 'spec/delayed/command_spec.rb' # Offense count: 2 RSpec/ExpectInHook: Exclude: - 'spec/delayed/job_spec.rb' -# Offense count: 104 +# Offense count: 91 # Configuration parameters: AssignmentOnly. RSpec/InstanceVariable: Exclude: @@ -100,16 +79,10 @@ RSpec/StubbedMock: - 'spec/performable_method_spec.rb' - 'spec/worker_spec.rb' -# Offense count: 2 -RSpec/SubjectStub: - Exclude: - - 'spec/delayed/command_spec.rb' - -# Offense count: 12 +# Offense count: 6 # Configuration parameters: IgnoreNameless, IgnoreSymbolicNames. RSpec/VerifiedDoubles: Exclude: - - 'spec/delayed/command_spec.rb' - 'spec/lifecycle_spec.rb' - 'spec/performable_mailer_spec.rb' - 'spec/performable_method_spec.rb' @@ -121,20 +94,6 @@ Rails/ApplicationMailer: Exclude: - 'spec/performable_mailer_spec.rb' -# Offense count: 2 -# Configuration parameters: Include. -# Include: app/**/*.rb, config/**/*.rb, lib/**/*.rb -Rails/Exit: - Exclude: - - 'lib/delayed/command.rb' - -# Offense count: 1 -# Configuration parameters: Include. -# Include: app/**/*.rb, config/**/*.rb, db/**/*.rb, lib/**/*.rb -Rails/Output: - Exclude: - - 'lib/delayed/command.rb' - # Offense count: 2 # Cop supports --auto-correct. # Configuration parameters: Include. @@ -148,20 +107,19 @@ Rails/RakeEnvironment: # ForbiddenMethods: decrement!, decrement_counter, increment!, increment_counter, insert, insert!, insert_all, insert_all!, toggle!, touch, touch_all, update_all, update_attribute, update_column, update_columns, update_counters, upsert, upsert_all Rails/SkipsModelValidations: Exclude: - - 'lib/delayed/job.rb' + - 'app/models/delayed/job.rb' -# Offense count: 4 +# Offense count: 2 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. # SupportedStyles: strict, flexible Rails/TimeZone: Exclude: - - 'lib/delayed/tasks.rb' - 'lib/delayed/worker.rb' - - 'spec/message_sending_spec.rb' - 'spec/worker_spec.rb' # Offense count: 2 +# Cop supports --auto-correct. Rake/Desc: Exclude: - 'Rakefile' @@ -179,13 +137,6 @@ Security/YAMLLoad: - 'spec/helper.rb' - 'spec/yaml_ext_spec.rb' -# Offense count: 1 -# Configuration parameters: EnforcedStyle. -# SupportedStyles: if, case, both -Style/MissingElse: - Exclude: - - 'lib/delayed/job.rb' - # Offense count: 2 Style/MissingRespondToMissing: Exclude: @@ -198,9 +149,3 @@ Style/MissingRespondToMissing: Style/OptionalBooleanParameter: Exclude: - 'lib/delayed/performable_method.rb' - -# Offense count: 2 -# Cop supports --auto-correct. -Style/StderrPuts: - Exclude: - - 'lib/delayed/command.rb' diff --git a/CHANGELOG.md b/CHANGELOG.md index 26356aaf..bd9d30ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,14 @@ and this project aims to adhere to [Semantic Versioning](http://semver.org/spec/ ### Removed ### Fixed +## [0.2.0] - 2021-08-30 +### Fixed +- Fix the loading of `Delayed::Job` constant on newly-generated Rails 6.1 apps. (previously, the + constant would not be available until `ActiveRecord::Base` was referenced for the first time) +### Changed +- The `Delayed::Railtie` is now a `Delayed::Engine`, allowing it to autoload constants via Rails' + built-in autoloader. In a non-Rails context, `require 'delayed'` will eager-load its models. + ## [0.1.1] - 2021-08-19 ### Added - This CHANGELOG file! @@ -18,12 +26,13 @@ and this project aims to adhere to [Semantic Versioning](http://semver.org/spec/ - Fix the gemspec description, which had previously been written in rdoc format (causing it to appear garbled on rubygems.org). -## [0.1.0] - 2021-08-18 +## [0.1.0] - 2021-08-17 ### Added - Initial release! This repo is the result of some merging, squashing, and commit massaging, in preparation for a public release! The goal was to maintain historical commit authorship of the ancestor repos (`delayed_job` and `delayed_job_active_record`), plus the changes from Betterment's internal forks. +[0.2.0]: https://github.com/betterment/delayed/compare/v0.1.1...v0.2.0 [0.1.1]: https://github.com/betterment/delayed/compare/v0.1.0...v0.1.1 [0.1.0]: https://github.com/betterment/delayed/releases/tag/v0.1.0 diff --git a/lib/delayed/job.rb b/app/models/delayed/job.rb similarity index 100% rename from lib/delayed/job.rb rename to app/models/delayed/job.rb diff --git a/delayed.gemspec b/delayed.gemspec index ef781156..ac75ed59 100644 --- a/delayed.gemspec +++ b/delayed.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |spec| spec.require_paths = ['lib'] spec.summary = 'a multi-threaded, SQL-driven ActiveJob backend used at Betterment to process millions of background jobs per day' - spec.version = '0.1.1' + spec.version = '0.2.0' spec.metadata = { 'changelog_uri' => 'https://github.com/betterment/delayed/blob/main/CHANGELOG.md', 'bug_tracker_uri' => 'https://github.com/betterment/delayed/issues', diff --git a/lib/delayed.rb b/lib/delayed.rb index 4ae1b711..eac34631 100644 --- a/lib/delayed.rb +++ b/lib/delayed.rb @@ -14,11 +14,12 @@ require 'delayed/backend/base' require 'delayed/backend/job_preparer' require 'delayed/worker' -require 'delayed/railtie' if defined?(Rails::Railtie) -ActiveSupport.on_load(:active_record) do - require 'delayed/serialization/active_record' - require 'delayed/job' +if defined?(Rails::Engine) + require 'delayed/engine' +else + require 'active_record' + require_relative '../app/models/delayed/job' end ActiveSupport.on_load(:active_job) do diff --git a/lib/delayed/railtie.rb b/lib/delayed/engine.rb similarity index 71% rename from lib/delayed/railtie.rb rename to lib/delayed/engine.rb index a44a7e70..641f408f 100644 --- a/lib/delayed/railtie.rb +++ b/lib/delayed/engine.rb @@ -1,5 +1,5 @@ module Delayed - class Railtie < Rails::Railtie + class Engine < Rails::Engine rake_tasks do load 'delayed/tasks.rb' end