-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make the task preloading process configurable #867
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module MaintenanceTasks | ||
# This class defines how the gem will find and load all tasks defined by the application. | ||
# It can be overridden by the application to change the way tasks are loaded. | ||
class DefaultTaskLoader | ||
class << self | ||
# Recursively browse the MaintenanceTasks.tasks_module namespace to load all defined tasks. | ||
# | ||
# @return [void] | ||
def load_all | ||
load_constants(MaintenanceTasks.tasks_module.safe_constantize) | ||
end | ||
|
||
private | ||
|
||
def load_constants(namespace) | ||
namespace.constants.each do |name| | ||
object = namespace.const_get(name) | ||
load_constants(object) if object.instance_of?(Module) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class NotAutoloadedTask < MaintenanceTasks::Task | ||
def collection | ||
[1, 2] | ||
end | ||
|
||
def process(_); end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
module MaintenanceTasks | ||
class DefaultTaskLoaderTest < ActiveSupport::TestCase | ||
include ActiveSupport::Testing::Isolation | ||
|
||
test ".load_all loads all autoloadable Task subclasses within the Maintenance namespace" do | ||
[ | ||
Maintenance::BatchImportPostsTask, | ||
Maintenance::CallbackTestTask, | ||
Maintenance::CancelledEnqueueTask, | ||
Maintenance::EnqueueErrorTask, | ||
Maintenance::ErrorTask, | ||
Maintenance::ImportPostsTask, | ||
Maintenance::Nested, | ||
Maintenance::Nested::NestedMore, | ||
Maintenance::Nested::NestedMore::NestedMoreTask, | ||
Maintenance::Nested::NestedTask, | ||
Maintenance::NoCollectionTask, | ||
Maintenance::ParamsTask, | ||
Maintenance::TestTask, | ||
Maintenance::UpdatePostsInBatchesTask, | ||
Maintenance::UpdatePostsModulePrependedTask, | ||
Maintenance::UpdatePostsTask, | ||
Maintenance::UpdatePostsThrottledTask, | ||
].each do |constant| | ||
constant | ||
.module_parent | ||
.expects(:const_get) | ||
.with(constant.name.demodulize.to_sym) | ||
.returns(constant) | ||
end | ||
|
||
DefaultTaskLoader.load_all | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new test fails in Rails 6.
Has it possibly identified an issue with the gem in older Rails versions? I haven't altered the recursive loading algorithm, just moved it from
Task.load_constants
toDefaultTaskLoader.load_constants
. 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wonder if it's just because of a different order... 🤔
Update: nothing's broken, it's just that the order in which I define the call
expects
matters, and different versions of Rails produce different orders.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like String vs Symbol, "Nested" vs :Nested, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the order returned by
.constants
seems to differ depending on the version of Rails.This would work in Ruby 6.1 for example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is blowing up before we even attempt to load the tasks:
I think the issue is that, in setting up the expectations for the nested task constants (
Maintenance::Nested::NestedMore
,Maintenance::Nested::NestedMore::NestedMoreTask
, etc.), we end up performing aconst_get
onMaintenance::Nested
when we doconstant.module_parent
, which triggers the expectation onMaintenance::Nested
that's just been set.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. Note it only blows up on Rails 6.1. :)
I managed to fix it (for Rails 6.1) by changing the order, but obviously it's gonna start failing in Rails 7.
Ideally I'd want to write expectations that do not care about the order in which
const_get
calls happen, but I haven't figured it out...I think what I really need is something Mocha doesn't provide: spies.
I want to spy on
const_get
calls without changing their original behaviours.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, right -- sorry I thought the issue was more complex than it was, but it really is just whether the
Maintenance::Nested
expectation is set before or after the otherNested
constant expectations...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually @davidstosik if I change the order to what you proposed in #867 (comment), it passes against all the Rails versions for me? Were you seeing failures locally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adrianna Huh, wow, this is weird, you seem to be right. Although both orders work with Rails 7, it seems only a specific order works with Rails 6.1?
For fun (and because I hadn't imagined the right order would work for all supported Rails versions), I've also implemented a spy. Here it is, just to show what it could look like:
(I saw your more general comment about the PR, will reply next. 🙇🏻♂️ )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is neat, thanks for sharing!! ❤️