-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the task preloading process configurable
In ordered to be displayed in the UI, tasks have to be loaded by the time `MaintenanceTasks::Task.descendants` is called. To achieve that, the `Task.load_constants` method was previously implemented, and called from `Task.available_tasks`. A few inconvenients of that method are: - It could easily be abused and used in lower levels of the gem, where ideally it should ne be necessary to load all tasks to achieve the gem's goal. See for example my previous PR [Avoid loading all tasks to validate a `Run#task_name`](#866). - It can introduce unnecessary overhead. For example, displaying the CLI's basic help message with `maintenance_tasks help` will necessitate all tasks to be loaded (even though the help message does not display anything related to the available tasks), and this loading time will delay the appearance of that help message. Similarly, a single test case that want to test a single task's behaviour, while never displaying the whole list of available tasks, would be negatively impacted by a misuse of `Task.available_tasks`. - It does not allow an application to define an alternative way to load all constants. As a matter of fact, the Shopify Core monolith monkey-patched that method to allow multiple "task modules" instead of a single one. With a configurable `task_loader`, monkey-patching would not be necessary. (Which is good. 🙈) This PR introduces `MaintenanceTasks.task_loader`, a configuration that allows applications to change the logic applied to ensure all task are loaded before being displayed. As a bonus: - it changes how the CLI generates the long description containing all task names, so that tasks get loaded at the last minute, only when necessary - it gets rid of `Task.available_tasks` (its only use was for UI purposes, so I changed it to a direct call to `Task.descendants`) - it introduces some tests of the loading behaviour
- Loading branch information
1 parent
292ebc3
commit 6f45986
Showing
11 changed files
with
190 additions
and
56 deletions.
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,40 @@ | ||
|
||
# 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