maintenance_tasks 1.4.0 → 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +229 -41
- data/app/controllers/maintenance_tasks/tasks_controller.rb +2 -1
- data/app/helpers/maintenance_tasks/application_helper.rb +1 -0
- data/app/helpers/maintenance_tasks/tasks_helper.rb +19 -0
- data/app/jobs/concerns/maintenance_tasks/task_job_concern.rb +28 -21
- data/app/models/maintenance_tasks/application_record.rb +1 -0
- data/app/models/maintenance_tasks/csv_collection_builder.rb +38 -0
- data/app/models/maintenance_tasks/no_collection_builder.rb +29 -0
- data/app/models/maintenance_tasks/null_collection_builder.rb +38 -0
- data/app/models/maintenance_tasks/progress.rb +8 -3
- data/app/models/maintenance_tasks/run.rb +157 -13
- data/app/models/maintenance_tasks/runner.rb +22 -9
- data/app/models/maintenance_tasks/runs_page.rb +1 -0
- data/app/models/maintenance_tasks/task.rb +236 -0
- data/app/models/maintenance_tasks/task_data.rb +15 -3
- data/app/validators/maintenance_tasks/run_status_validator.rb +2 -2
- data/app/views/maintenance_tasks/runs/_arguments.html.erb +22 -0
- data/app/views/maintenance_tasks/runs/_csv.html.erb +5 -0
- data/app/views/maintenance_tasks/runs/_run.html.erb +18 -1
- data/app/views/maintenance_tasks/runs/info/_custom.html.erb +0 -0
- data/app/views/maintenance_tasks/runs/info/_errored.html.erb +0 -2
- data/app/views/maintenance_tasks/runs/info/_running.html.erb +3 -5
- data/app/views/maintenance_tasks/tasks/_custom.html.erb +0 -0
- data/app/views/maintenance_tasks/tasks/_task.html.erb +19 -1
- data/app/views/maintenance_tasks/tasks/show.html.erb +32 -7
- data/config/routes.rb +1 -0
- data/db/migrate/20201211151756_create_maintenance_tasks_runs.rb +1 -0
- data/db/migrate/20210225152418_remove_index_on_task_name.rb +1 -0
- data/db/migrate/20210517131953_add_arguments_to_maintenance_tasks_runs.rb +1 -0
- data/db/migrate/20211210152329_add_lock_version_to_maintenance_tasks_runs.rb +8 -0
- data/lib/generators/maintenance_tasks/install_generator.rb +1 -0
- data/lib/generators/maintenance_tasks/task_generator.rb +13 -0
- data/lib/generators/maintenance_tasks/templates/no_collection_task.rb.tt +13 -0
- data/lib/generators/maintenance_tasks/templates/no_collection_task_test.rb.tt +12 -0
- data/lib/generators/maintenance_tasks/templates/task.rb.tt +3 -1
- data/lib/generators/maintenance_tasks/templates/task_test.rb.tt +4 -0
- data/lib/maintenance_tasks/cli.rb +6 -5
- data/lib/maintenance_tasks/engine.rb +15 -1
- data/lib/maintenance_tasks.rb +12 -1
- metadata +15 -7
- data/app/models/maintenance_tasks/csv_collection.rb +0 -33
- data/app/tasks/maintenance_tasks/task.rb +0 -137
- data/app/views/maintenance_tasks/runs/_info.html.erb +0 -16
@@ -1,137 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
module MaintenanceTasks
|
3
|
-
# Base class that is inherited by the host application's task classes.
|
4
|
-
class Task
|
5
|
-
extend ActiveSupport::DescendantsTracker
|
6
|
-
include ActiveModel::Attributes
|
7
|
-
include ActiveModel::AttributeAssignment
|
8
|
-
include ActiveModel::Validations
|
9
|
-
|
10
|
-
class NotFoundError < NameError; end
|
11
|
-
|
12
|
-
# The throttle conditions for a given Task. This is provided as an array of
|
13
|
-
# hashes, with each hash specifying two keys: throttle_condition and
|
14
|
-
# backoff. Note that Tasks inherit conditions from their superclasses.
|
15
|
-
#
|
16
|
-
# @api private
|
17
|
-
class_attribute :throttle_conditions, default: []
|
18
|
-
|
19
|
-
class << self
|
20
|
-
# Finds a Task with the given name.
|
21
|
-
#
|
22
|
-
# @param name [String] the name of the Task to be found.
|
23
|
-
#
|
24
|
-
# @return [Task] the Task with the given name.
|
25
|
-
#
|
26
|
-
# @raise [NotFoundError] if a Task with the given name does not exist.
|
27
|
-
def named(name)
|
28
|
-
task = name.safe_constantize
|
29
|
-
raise NotFoundError.new("Task #{name} not found.", name) unless task
|
30
|
-
unless task.is_a?(Class) && task < Task
|
31
|
-
raise NotFoundError.new("#{name} is not a Task.", name)
|
32
|
-
end
|
33
|
-
task
|
34
|
-
end
|
35
|
-
|
36
|
-
# Returns a list of concrete classes that inherit from the Task
|
37
|
-
# superclass.
|
38
|
-
#
|
39
|
-
# @return [Array<Class>] the list of classes.
|
40
|
-
def available_tasks
|
41
|
-
load_constants
|
42
|
-
descendants
|
43
|
-
end
|
44
|
-
|
45
|
-
# Make this Task a task that handles CSV.
|
46
|
-
#
|
47
|
-
# An input to upload a CSV will be added in the form to start a Run. The
|
48
|
-
# collection and count method are implemented.
|
49
|
-
def csv_collection
|
50
|
-
if !defined?(ActiveStorage) || !ActiveStorage::Attachment.table_exists?
|
51
|
-
raise NotImplementedError, "Active Storage needs to be installed\n"\
|
52
|
-
"To resolve this issue run: bin/rails active_storage:install"
|
53
|
-
end
|
54
|
-
include(CsvCollection)
|
55
|
-
end
|
56
|
-
|
57
|
-
# Processes one item.
|
58
|
-
#
|
59
|
-
# Especially useful for tests.
|
60
|
-
#
|
61
|
-
# @param item the item to process.
|
62
|
-
def process(item)
|
63
|
-
new.process(item)
|
64
|
-
end
|
65
|
-
|
66
|
-
# Returns the collection for this Task.
|
67
|
-
#
|
68
|
-
# Especially useful for tests.
|
69
|
-
#
|
70
|
-
# @return the collection.
|
71
|
-
def collection
|
72
|
-
new.collection
|
73
|
-
end
|
74
|
-
|
75
|
-
# Returns the count of items for this Task.
|
76
|
-
#
|
77
|
-
# Especially useful for tests.
|
78
|
-
#
|
79
|
-
# @return the count of items.
|
80
|
-
def count
|
81
|
-
new.count
|
82
|
-
end
|
83
|
-
|
84
|
-
# Add a condition under which this Task will be throttled.
|
85
|
-
#
|
86
|
-
# @param backoff [ActiveSupport::Duration] optionally, a custom backoff
|
87
|
-
# can be specified. This is the time to wait before retrying the Task.
|
88
|
-
# If no value is specified, it defaults to 30 seconds.
|
89
|
-
# @yieldreturn [Boolean] where the throttle condition is being met,
|
90
|
-
# indicating that the Task should throttle.
|
91
|
-
def throttle_on(backoff: 30.seconds, &condition)
|
92
|
-
self.throttle_conditions += [
|
93
|
-
{ throttle_on: condition, backoff: backoff },
|
94
|
-
]
|
95
|
-
end
|
96
|
-
|
97
|
-
private
|
98
|
-
|
99
|
-
def load_constants
|
100
|
-
namespace = MaintenanceTasks.tasks_module.safe_constantize
|
101
|
-
return unless namespace
|
102
|
-
namespace.constants.map { |constant| namespace.const_get(constant) }
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
# Placeholder method to raise in case a subclass fails to implement the
|
107
|
-
# expected instance method.
|
108
|
-
#
|
109
|
-
# @raise [NotImplementedError] with a message advising subclasses to
|
110
|
-
# implement an override for this method.
|
111
|
-
def collection
|
112
|
-
raise NoMethodError, "#{self.class.name} must implement `collection`."
|
113
|
-
end
|
114
|
-
|
115
|
-
# Placeholder method to raise in case a subclass fails to implement the
|
116
|
-
# expected instance method.
|
117
|
-
#
|
118
|
-
# @param _item [Object] the current item from the enumerator being iterated.
|
119
|
-
#
|
120
|
-
# @raise [NotImplementedError] with a message advising subclasses to
|
121
|
-
# implement an override for this method.
|
122
|
-
def process(_item)
|
123
|
-
raise NoMethodError, "#{self.class.name} must implement `process`."
|
124
|
-
end
|
125
|
-
|
126
|
-
# Total count of iterations to be performed.
|
127
|
-
#
|
128
|
-
# Tasks override this method to define the total amount of iterations
|
129
|
-
# expected at the start of the run. Return +nil+ if the amount is
|
130
|
-
# undefined, or counting would be prohibitive for your database.
|
131
|
-
#
|
132
|
-
# @return [Integer, nil]
|
133
|
-
def count
|
134
|
-
:no_count
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
<h5 class="title is-5">
|
2
|
-
<%= time_tag run.created_at, title: run.created_at %>
|
3
|
-
<%= status_tag run.status if with_status %>
|
4
|
-
</h5>
|
5
|
-
|
6
|
-
<%= progress run %>
|
7
|
-
|
8
|
-
<div class="content">
|
9
|
-
<%= render "maintenance_tasks/runs/info/#{run.status}", run: run %>
|
10
|
-
</div>
|
11
|
-
|
12
|
-
<% if run.csv_file.present? %>
|
13
|
-
<div class="block">
|
14
|
-
<%= link_to('Download CSV', csv_file_download_path(run)) %>
|
15
|
-
</div>
|
16
|
-
<% end %>
|