Skip to content

Commit

Permalink
Minor Rails 6.1 fix, plus autoload Delayed::Job constant (#2)
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
smudge authored Sep 2, 2021
1 parent 941f02d commit 484d68a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 72 deletions.
75 changes: 10 additions & 65 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -17,30 +17,13 @@ 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
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:
Expand All @@ -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:
Expand All @@ -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'
Expand All @@ -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.
Expand All @@ -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'
Expand All @@ -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:
Expand All @@ -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'
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,28 @@ and this project aims to adhere to [Semantic Versioning](http://semver.org/spec/
### Removed <!-- for now removed features. -->
### Fixed <!-- for any bug fixes. -->

## [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!
### Fixed
- 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
File renamed without changes.
2 changes: 1 addition & 1 deletion delayed.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
9 changes: 5 additions & 4 deletions lib/delayed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/delayed/railtie.rb → lib/delayed/engine.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Delayed
class Railtie < Rails::Railtie
class Engine < Rails::Engine
rake_tasks do
load 'delayed/tasks.rb'
end
Expand Down

0 comments on commit 484d68a

Please sign in to comment.