maintenance_tasks 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 04c33ddef00ee39487f42a7275fd3b11367bc16f32007a607e2d6fd7a9b3b55d
4
- data.tar.gz: 841a336d86ce65e52d2ddee9cef746f9a448664328213056ea8c7a9f0d861b76
3
+ metadata.gz: a14c442d683123d8aabb30db8c73c15a31ddd24852356aa64b0255b1cb2312e0
4
+ data.tar.gz: d5df5cbfa0cf9feab143ae2ea15cb5a0013978b552a068d9a588fed4e04178bd
5
5
  SHA512:
6
- metadata.gz: cefc3a619d3d3aec10c6104cf227c33558e1d30146f0d9338706eb30c4e21f58bda704905c85a8faa87bc4735d5255b714c8690b6b733f9dcde42216231abc3d
7
- data.tar.gz: 4f23532262622d91da4065926f19e96bb22788877acc10e1e7a0ce327e7136a2c24905e9a29aacbbdbe380c39dc58e593a0254e6bfec2b1c6d929f8e5fe13304
6
+ metadata.gz: 528669b01c611fe0e502683665182bddb2a98f2a74911c5d957f3868fca2ff57323a07eb2093acdfedc39e44057f8346702f70fc67cf027f59a32b985a6f7cea
7
+ data.tar.gz: bb00d7ef688b49ad9ae295650eea0b57718ea95aea6b341780011d882ae2b0f9ac830241cd9663d3bd7c9b9e08b3ee9bf6daa91a4ef2e4c53b69a15350c2da06
data/README.md CHANGED
@@ -832,6 +832,19 @@ controller class which **must inherit** from `ActionController::Base`.
832
832
 
833
833
  If no value is specified, it will default to `"ActionController::Base"`.
834
834
 
835
+ ### Metadata
836
+
837
+ `MaintenanceTasks.metadata` can be configured to specify a proc from which to get extra information about the run.
838
+ Since this proc will be ran in the context of the `MaintenanceTasks.parent_controller`, it can be used to keep the id
839
+ or email of the user who performed the maintenance task.
840
+
841
+ ```ruby
842
+ # config/initializers/maintenance_tasks.rb
843
+ MaintenanceTasks.metadata = -> do
844
+ { user_email: current_user.email }
845
+ end
846
+ ```
847
+
835
848
  ## Upgrading
836
849
 
837
850
  Use bundler to check for and upgrade to newer versions. After installing a new
@@ -14,6 +14,7 @@ module MaintenanceTasks
14
14
  name: params.fetch(:task_id),
15
15
  csv_file: params[:csv_file],
16
16
  arguments: params.fetch(:task_arguments, {}).permit!.to_h,
17
+ metadata: MaintenanceTasks.metadata&.call,
17
18
  &block
18
19
  )
19
20
  redirect_to(task_path(task))
@@ -36,9 +36,7 @@ module MaintenanceTasks
36
36
 
37
37
  enum status: STATUSES.to_h { |status| [status, status.to_s] }
38
38
 
39
- validates :task_name, on: :create, inclusion: {
40
- in: ->(_) { Task.available_tasks.map(&:to_s) },
41
- }
39
+ validate :task_name_belongs_to_a_valid_task, on: :create
42
40
  validate :csv_attachment_presence, on: :create
43
41
  validate :csv_content_type, on: :create
44
42
  validate :validate_task_arguments, on: :create
@@ -48,9 +46,11 @@ module MaintenanceTasks
48
46
  if Rails.gem_version >= Gem::Version.new("7.1.alpha")
49
47
  serialize :backtrace, coder: YAML
50
48
  serialize :arguments, coder: JSON
49
+ serialize :metadata, coder: JSON
51
50
  else
52
51
  serialize :backtrace
53
52
  serialize :arguments, JSON
53
+ serialize :metadata, JSON
54
54
  end
55
55
 
56
56
  scope :active, -> { where(status: ACTIVE_STATUSES) }
@@ -337,6 +337,15 @@ module MaintenanceTasks
337
337
  cancelling? && updated_at <= STUCK_TASK_TIMEOUT.ago
338
338
  end
339
339
 
340
+ # Performs validation on the task_name attribute.
341
+ # A Run must be associated with a valid Task to be valid.
342
+ # In order to confirm that, the Task is looked up by name.
343
+ def task_name_belongs_to_a_valid_task
344
+ Task.named(task_name)
345
+ rescue Task::NotFoundError
346
+ errors.add(:task_name, "must be the name of an existing Task.")
347
+ end
348
+
340
349
  # Performs validation on the presence of a :csv_file attachment.
341
350
  # A Run for a Task that uses CsvCollection must have an attached :csv_file
342
351
  # to be valid. Conversely, a Run for a Task that doesn't use CsvCollection
@@ -39,8 +39,8 @@ module MaintenanceTasks
39
39
  # creating the Run.
40
40
  # @raise [ActiveRecord::ValueTooLong] if the creation of the Run fails due
41
41
  # to a value being too long for the column type.
42
- def run(name:, csv_file: nil, arguments: {}, run_model: Run)
43
- run = run_model.new(task_name: name, arguments: arguments)
42
+ def run(name:, csv_file: nil, arguments: {}, run_model: Run, metadata: nil)
43
+ run = run_model.new(task_name: name, arguments: arguments, metadata: metadata)
44
44
  if csv_file
45
45
  run.csv_file.attach(csv_file)
46
46
  run.csv_file.filename = filename(name)
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddMetadataToRuns < ActiveRecord::Migration[6.0]
4
+ def change
5
+ add_column(:maintenance_tasks_runs, :metadata, :text)
6
+ end
7
+ end
@@ -82,4 +82,11 @@ module MaintenanceTasks
82
82
  #
83
83
  # @return [String] the name of the parent controller for web UI.
84
84
  mattr_accessor :parent_controller, default: "ActionController::Base"
85
+
86
+ # @!attribute metadata
87
+ # @scope class
88
+ # The Proc to call from the controller to generate metadata that will be persisted on the Run.
89
+ #
90
+ # @return [Proc] generates a hash containing the metadata to be stored on the Run
91
+ mattr_accessor :metadata, default: nil
85
92
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maintenance_tasks
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify Engineering
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-02 00:00:00.000000000 Z
11
+ date: 2023-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -137,6 +137,7 @@ files:
137
137
  - db/migrate/20211210152329_add_lock_version_to_maintenance_tasks_runs.rb
138
138
  - db/migrate/20220706101937_change_runs_tick_columns_to_bigints.rb
139
139
  - db/migrate/20220713131925_add_index_on_task_name_and_status_to_runs.rb
140
+ - db/migrate/20230622035229_add_metadata_to_runs.rb
140
141
  - exe/maintenance_tasks
141
142
  - lib/generators/maintenance_tasks/install_generator.rb
142
143
  - lib/generators/maintenance_tasks/task_generator.rb
@@ -155,7 +156,7 @@ homepage: https://github.com/Shopify/maintenance_tasks
155
156
  licenses:
156
157
  - MIT
157
158
  metadata:
158
- source_code_uri: https://github.com/Shopify/maintenance_tasks/tree/v2.2.0
159
+ source_code_uri: https://github.com/Shopify/maintenance_tasks/tree/v2.3.0
159
160
  allowed_push_host: https://rubygems.org
160
161
  post_install_message:
161
162
  rdoc_options: []
@@ -172,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
172
173
  - !ruby/object:Gem::Version
173
174
  version: '0'
174
175
  requirements: []
175
- rubygems_version: 3.4.17
176
+ rubygems_version: 3.4.18
176
177
  signing_key:
177
178
  specification_version: 4
178
179
  summary: A Rails engine for queuing and managing maintenance tasks