Class: MaintenanceTasks::Task

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_tasksArray<Class>

Returns a list of concrete classes that inherit from the Task superclass.

Returns:

  • the list of classes.



49
50
51
52
# File 'app/models/maintenance_tasks/task.rb', line 49

def available_tasks
  load_constants
  descendants
end

.collectionObject

Returns the collection for this Task.

Especially useful for tests.

Returns:

  • the collection.



99
100
101
# File 'app/models/maintenance_tasks/task.rb', line 99

def collection
  new.collection
end

.countObject

Returns the count of items for this Task.

Especially useful for tests.

Returns:

  • the count of items.



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.

Parameters:

  • (defaults to: nil)

    optionally, supply a batch size if the CSV



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.

Parameters:

  • the name of the Task to be found.

Returns:

  • the Task with the given name.

Raises:

  • if a Task with the given name does not exist.



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_collectionObject

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.

Parameters:

  • the item to process



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.

Parameters:

  • (defaults to: 30.seconds)

    a custom backoff can be specified. This is the time to wait before retrying the Task, defaulting to 30 seconds. If provided as a Duration, the backoff is wrapped in a proc. Alternatively,an object responding to call can be used. It must return an ActiveSupport::Duration.

Yield Returns:

  • (Boolean)

    where the throttle condition is being met, indicating that the Task should throttle.



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

#collectionObject

The collection to be processed, delegated to the strategy.

Returns:

  • the collection.



223
224
225
# File 'app/models/maintenance_tasks/task.rb', line 223

def collection
  self.class.collection_builder_strategy.collection(self)
end

#countInteger?

Total count of iterations to be performed, delegated to the strategy.

Returns:



241
242
243
# File 'app/models/maintenance_tasks/task.rb', line 241

def count
  self.class.collection_builder_strategy.count(self)
end

#csv_contentString

The contents of a CSV file to be processed by a Task.

Returns:

  • the content of the CSV file to process.

Raises:



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.

Parameters:

  • the content of the CSV file to process.

Raises:



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.

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.

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.

Parameters:

  • the current item from the enumerator being iterated.

Raises:

  • with a message advising subclasses to implement an override for this 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