Class: MaintenanceTasks::Task
- Inherits:
-
Object
- Object
- MaintenanceTasks::Task
- Extended by:
- ActiveSupport::DescendantsTracker
- Includes:
- ActiveModel::AttributeAssignment, ActiveModel::Attributes, ActiveModel::Validations, ActiveSupport::Callbacks
- Defined in:
- app/models/maintenance_tasks/task.rb
Overview
Base class that is inherited by the host application’s task classes.
Defined Under Namespace
Classes: NotFoundError
Class Method Summary collapse
-
.after_cancel(*filter_list, &block) ⇒ Object
Initialize a callback to run after the task is cancelled.
-
.after_complete(*filter_list, &block) ⇒ Object
Initialize a callback to run after the task completes.
-
.after_error(*filter_list, &block) ⇒ Object
Initialize a callback to run after the task produces an error.
-
.after_interrupt(*filter_list, &block) ⇒ Object
Initialize a callback to run after the task is interrupted.
-
.after_pause(*filter_list, &block) ⇒ Object
Initialize a callback to run after the task pauses.
-
.after_start(*filter_list, &block) ⇒ Object
Initialize a callback to run after the task starts.
-
.available_tasks ⇒ Array<Class>
Returns a list of concrete classes that inherit from the Task superclass.
-
.collection ⇒ Object
Returns the collection for this Task.
-
.count ⇒ Object
Returns the count of items for this Task.
-
.csv_collection(in_batches: nil) ⇒ Object
Make this Task a task that handles CSV.
-
.named(name) ⇒ Task
Finds a Task with the given name.
-
.no_collection ⇒ Object
Make this a Task that calls #process once, instead of iterating over a collection.
-
.process(*args) ⇒ Object
Processes one item.
-
.throttle_on(backoff: 30.seconds, &condition) ⇒ Object
Add a condition under which this Task will be throttled.
Instance Method Summary collapse
-
#collection ⇒ Object
The collection to be processed, delegated to the strategy.
-
#count ⇒ Integer?
Total count of iterations to be performed, delegated to the strategy.
-
#csv_content ⇒ String
The contents of a CSV file to be processed by a Task.
-
#csv_content=(csv_content) ⇒ Object
Set the contents of a CSV file to be processed by a Task.
-
#has_csv_content? ⇒ Boolean
Returns whether the Task handles CSV.
-
#no_collection? ⇒ Boolean
Returns whether the Task is collection-less.
-
#process(_item) ⇒ Object
Placeholder method to raise in case a subclass fails to implement the expected instance method.
Class Method Details
.after_cancel(*filter_list, &block) ⇒ Object
Initialize a callback to run after the task is cancelled.
166 167 168 |
# File 'app/models/maintenance_tasks/task.rb', line 166 def after_cancel(*filter_list, &block) set_callback(:cancel, :after, *filter_list, &block) end |
.after_complete(*filter_list, &block) ⇒ Object
Initialize a callback to run after the task completes.
142 143 144 |
# File 'app/models/maintenance_tasks/task.rb', line 142 def after_complete(*filter_list, &block) set_callback(:complete, :after, *filter_list, &block) end |
.after_error(*filter_list, &block) ⇒ Object
Initialize a callback to run after the task produces an error.
174 175 176 |
# File 'app/models/maintenance_tasks/task.rb', line 174 def after_error(*filter_list, &block) set_callback(:error, :after, *filter_list, &block) end |
.after_interrupt(*filter_list, &block) ⇒ Object
Initialize a callback to run after the task is interrupted.
158 159 160 |
# File 'app/models/maintenance_tasks/task.rb', line 158 def after_interrupt(*filter_list, &block) set_callback(:interrupt, :after, *filter_list, &block) end |
.after_pause(*filter_list, &block) ⇒ Object
Initialize a callback to run after the task pauses.
150 151 152 |
# File 'app/models/maintenance_tasks/task.rb', line 150 def after_pause(*filter_list, &block) set_callback(:pause, :after, *filter_list, &block) end |
.after_start(*filter_list, &block) ⇒ Object
Initialize a callback to run after the task starts.
134 135 136 |
# File 'app/models/maintenance_tasks/task.rb', line 134 def after_start(*filter_list, &block) set_callback(:start, :after, *filter_list, &block) end |
.available_tasks ⇒ Array<Class>
Returns a list of concrete classes that inherit from the Task superclass.
49 50 51 52 |
# File 'app/models/maintenance_tasks/task.rb', line 49 def available_tasks load_constants descendants end |
.collection ⇒ Object
Returns the collection for this Task.
Especially useful for tests.
99 100 101 |
# File 'app/models/maintenance_tasks/task.rb', line 99 def collection new.collection end |
.count ⇒ Object
Returns the count of items for this Task.
Especially useful for tests.
108 109 110 |
# File 'app/models/maintenance_tasks/task.rb', line 108 def count new.count end |
.csv_collection(in_batches: nil) ⇒ Object
Make this Task a task that handles CSV.
should be processed in batches.
An input to upload a CSV will be added in the form to start a Run. The collection and count method are implemented.
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/models/maintenance_tasks/task.rb', line 61 def csv_collection(in_batches: nil) unless defined?(ActiveStorage) raise NotImplementedError, "Active Storage needs to be installed\n"\ "To resolve this issue run: bin/rails active_storage:install" end if in_batches self.collection_builder_strategy = BatchCsvCollectionBuilder.new(in_batches) else self.collection_builder_strategy = CsvCollectionBuilder.new end end |
.named(name) ⇒ Task
Finds a Task with the given name.
35 36 37 38 39 40 41 42 43 |
# File 'app/models/maintenance_tasks/task.rb', line 35 def named(name) task = name.safe_constantize raise NotFoundError.new("Task #{name} not found.", name) unless task unless task.is_a?(Class) && task < Task raise NotFoundError.new("#{name} is not a Task.", name) end task end |
.no_collection ⇒ Object
Make this a Task that calls #process once, instead of iterating over a collection.
77 78 79 80 |
# File 'app/models/maintenance_tasks/task.rb', line 77 def no_collection self.collection_builder_strategy = MaintenanceTasks::NoCollectionBuilder.new end |
.process(*args) ⇒ Object
Processes one item.
Especially useful for tests.
90 91 92 |
# File 'app/models/maintenance_tasks/task.rb', line 90 def process(*args) new.process(*args) end |
.throttle_on(backoff: 30.seconds, &condition) ⇒ Object
Add a condition under which this Task will be throttled.
121 122 123 124 125 126 127 128 |
# File 'app/models/maintenance_tasks/task.rb', line 121 def throttle_on(backoff: 30.seconds, &condition) backoff_as_proc = backoff backoff_as_proc = -> { backoff } unless backoff.respond_to?(:call) self.throttle_conditions += [ { throttle_on: condition, backoff: backoff_as_proc }, ] end |
Instance Method Details
#collection ⇒ Object
The collection to be processed, delegated to the strategy.
223 224 225 |
# File 'app/models/maintenance_tasks/task.rb', line 223 def collection self.class.collection_builder_strategy.collection(self) end |
#count ⇒ Integer?
Total count of iterations to be performed, delegated to the strategy.
241 242 243 |
# File 'app/models/maintenance_tasks/task.rb', line 241 def count self.class.collection_builder_strategy.count(self) end |
#csv_content ⇒ String
The contents of a CSV file to be processed by a Task.
191 192 193 194 195 |
# File 'app/models/maintenance_tasks/task.rb', line 191 def csv_content raise NoMethodError unless has_csv_content? @csv_content end |
#csv_content=(csv_content) ⇒ Object
Set the contents of a CSV file to be processed by a Task.
200 201 202 203 204 |
# File 'app/models/maintenance_tasks/task.rb', line 200 def csv_content=(csv_content) raise NoMethodError unless has_csv_content? @csv_content = csv_content end |
#has_csv_content? ⇒ Boolean
Returns whether the Task handles CSV.
209 210 211 |
# File 'app/models/maintenance_tasks/task.rb', line 209 def has_csv_content? self.class.has_csv_content? end |
#no_collection? ⇒ Boolean
Returns whether the Task is collection-less.
216 217 218 |
# File 'app/models/maintenance_tasks/task.rb', line 216 def no_collection? self.class.no_collection? end |
#process(_item) ⇒ Object
Placeholder method to raise in case a subclass fails to implement the expected instance method.
234 235 236 |
# File 'app/models/maintenance_tasks/task.rb', line 234 def process(_item) raise NoMethodError, "#{self.class.name} must implement `process`." end |