Skip to content

Commit

Permalink
Allow tasks to be loaded from nested task directories (#809)
Browse files Browse the repository at this point in the history
Co-authored-by: Masa (Aileron inc) <[email protected]>
  • Loading branch information
styrmis and aileron authored Apr 19, 2023
1 parent 0e56981 commit 1dfa042
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 1 deletion.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,26 @@ MaintenanceTasks.tasks_module = "TaskModule"

If no value is specified, it will default to `Maintenance`.

#### Organizing tasks using namespaces

Tasks may be nested arbitrarily deeply under `app/tasks/maintenance`, for example given a
task file `app/tasks/maintenance/team_name/service_name/update_posts_task.rb` we
can define the task as:

```ruby
module Maintenance
module TeamName
module ServiceName
class UpdatePostsTask < MaintenanceTasks::Task
def process(rows)
# ...
end
end
end
end
end
```

#### Customizing the underlying job class

`MaintenanceTasks.job` can be configured to define a Job class for your tasks to
Expand Down
8 changes: 7 additions & 1 deletion app/models/maintenance_tasks/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,13 @@ def load_constants
namespace = MaintenanceTasks.tasks_module.safe_constantize
return unless namespace

namespace.constants.map { |constant| namespace.const_get(constant) }
load_const = lambda do |root|
root.constants.each do |name|
object = root.const_get(name)
load_const.call(object) if object.instance_of?(Module)
end
end
load_const.call(namespace)
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Maintenance
module Nested
module NestedMore
class NestedMoreTask < MaintenanceTasks::Task
no_collection

def process
# Task only exists to verify correct loading of tasks within subfolders
end
end
end
end
end
13 changes: 13 additions & 0 deletions test/dummy/app/tasks/maintenance/nested/nested_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Maintenance
module Nested
class NestedTask < MaintenanceTasks::Task
no_collection

def process
# Task only exists to verify correct loading of tasks within subfolders
end
end
end
end
2 changes: 2 additions & 0 deletions test/models/maintenance_tasks/task_data_index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class TaskDataIndexTest < ActiveSupport::TestCase
"Maintenance::EnqueueErrorTask",
"Maintenance::ErrorTask",
"Maintenance::ImportPostsTask",
"Maintenance::Nested::NestedMore::NestedMoreTask",
"Maintenance::Nested::NestedTask",
"Maintenance::NoCollectionTask",
# duplicate due to fixtures containing two active runs of this task
"Maintenance::NoCollectionTask",
Expand Down
2 changes: 2 additions & 0 deletions test/models/maintenance_tasks/task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class TaskTest < ActiveSupport::TestCase
"Maintenance::EnqueueErrorTask",
"Maintenance::ErrorTask",
"Maintenance::ImportPostsTask",
"Maintenance::Nested::NestedMore::NestedMoreTask",
"Maintenance::Nested::NestedTask",
"Maintenance::NoCollectionTask",
"Maintenance::ParamsTask",
"Maintenance::TestTask",
Expand Down
2 changes: 2 additions & 0 deletions test/system/maintenance_tasks/tasks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class TasksTest < ApplicationSystemTestCase
"Maintenance::CancelledEnqueueTask\nNew",
"Maintenance::EnqueueErrorTask\nNew",
"Maintenance::ErrorTask\nNew",
"Maintenance::Nested::NestedMore::NestedMoreTask\nNew",
"Maintenance::Nested::NestedTask\nNew",
"Maintenance::ParamsTask\nNew",
"Maintenance::TestTask\nNew",
"Maintenance::UpdatePostsInBatchesTask\nNew",
Expand Down

0 comments on commit 1dfa042

Please sign in to comment.