deploy_pin 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ee7d5ecb0f1089412e2355727340b0c3ea1770b14dd27a09113a50ecfdadbbe4
4
- data.tar.gz: 8f3274681a343638fa28b2b10e8e36f2b5c053a678d8cd1b33ed46157bb1a618
3
+ metadata.gz: 99ce49aff4122914c4320e785d27b64aa8ad2bc5ead7f9360b62e1fffd691535
4
+ data.tar.gz: 5750109d713dc3b05a561ae8907235bb22178d60f39e1e87ec1c28a99232ef5d
5
5
  SHA512:
6
- metadata.gz: 99540af5bdc9a3494916e63c8173865b108f3f2083ab1e79748d3b8148dd5d7fa77f701fc50ae29829e4c0981dc33e36fe68e752cb79be190a8f7d956546b9bc
7
- data.tar.gz: c65f5bd5ade9efdbe7f0ebf4f88c3d8d6a6459ce359c87e218d322ba0346f6890ac01a43bd814a1cbcb58dd0493cb3c2733a4485b346b2e6d1cad58f5b0a50c1
6
+ metadata.gz: 2749cdd1b3f047393ff66986873bef3d70604b65d37bb931477e6a56ec809b8b3e71cd551b9a655b8eb446ea0dbe447671db78f900da1d4c79d5b7170623a0fe
7
+ data.tar.gz: 67546b5eaa86e6bc32b9c1403e7d09601ce386d16432183b790c3815e5a78c9e3adadea08f755d20078fa879cc21b7019ce8bcc787a116a730bc1f20333f8de6
data/README.md CHANGED
@@ -37,6 +37,7 @@ Migrations are for schema changes. Everything else you need to run during a rele
37
37
  - ⚡ **Parallel:** a [Parallel](https://github.com/grosser/parallel) wrapper that applies the timeout in every worker.
38
38
  - 🔁 **Resumable:** save progress so a long task continues where it stopped.
39
39
  - ♻️ **Recurring tasks:** tasks that run on every deploy.
40
+ - 🧹 **Cleanup:** remove the files of old one-off tasks, so the tasks directory doesn't keep growing.
40
41
  - 🚦 **Deployment state:** `DeployPin.ongoing_deployment?` lets your app behave differently while a deploy is in progress.
41
42
  - 🍃 **ActiveRecord or Mongoid** storage, or a record class of your own.
42
43
  - 🧩 **Rails or plain Ruby:** a Rails engine and `rails g` generators, or the `deploy_pin` command and `DeployPin.load_tasks` in any other Ruby app (Sinatra, Hanami, Roda, scripts…).
@@ -194,7 +195,15 @@ The generator gives each one-off task an unused identifier: two tasks generated
194
195
 
195
196
  Tasks are sorted by **group**, then by **identifier**. Negative identifiers run after the other tasks of their group.
196
197
 
197
- A non-recurring task runs only once. When it finishes, it is marked done in the storage, and later runs skip it. If two tasks have the same code, only the first one runs. Lines that hold only a comment or nothing don't count, unless they are inside a heredoc or another string.
198
+ A non-recurring task runs only once. When it finishes, it is marked done in the storage, and later runs skip it.
199
+
200
+ ### Why DeployPin deduplicates tasks
201
+
202
+ You might run a backfill in development, fix the job it calls, then add a new DeployPin task to run it again. The first task is already complete in development, so the next deploy runs the new task. Both task files can still contain the same call, such as `BackfillUserNames.call`.
203
+
204
+ If both tasks are pending and selected together on the first production deploy, DeployPin runs the first task and skips the second task's body, marking that one-off task done. Both files call the job implementation in the deployed release.
205
+
206
+ DeployPin compares task bodies with different identifiers within a single `deploy_pin:run` invocation. It ignores standalone comments and blank lines outside strings. If the fix changes the task body itself, both versions can run.
198
207
 
199
208
  ### Running one executor at a time
200
209
 
@@ -236,6 +245,7 @@ rake 'deploy_pin:run[I, !20190401135040]' # group I except one task
236
245
  | `rake deploy_pin:short_list[...]` | List pending tasks by group, without the code |
237
246
  | `rake deploy_pin:run[...]` | Run pending tasks |
238
247
  | `rake deploy_pin:mark_done[...]` | Mark tasks as done without running them. For example, in `db/seeds.rb` for a fresh database, or for a new environment |
248
+ | `rake deploy_pin:cleanup` | Remove the files and tests of one-off tasks created more than [`cleanup_safe_time_window`](#cleaning-up-old-tasks) ago. `deploy_pin:cleanup[dry_run]` (or `DRY_RUN=1`) only lists them |
239
249
 
240
250
  ## Hooking into your deploy
241
251
 
@@ -434,6 +444,30 @@ rails g deploy_pin:task warm_caches -g III --recurring --identifier 5
434
444
  > [!NOTE]
435
445
  > Identifiers `0` and `-10` are used by the built-in [deployment state](#deployment-state) tasks. Don't use them.
436
446
 
447
+ ### Cleaning up old tasks
448
+
449
+ Once a one-off task has run in every environment, its file is no longer needed. `deploy_pin:cleanup` removes the files of one-off tasks created more than `cleanup_safe_time_window` ago, together with their [tests](#testing-tasks). A task's creation time is the timestamp in its identifier. Set the window to your deployment cycle, from generating a task to running it in every environment, plus a safety buffer:
450
+
451
+ ```ruby
452
+ DeployPin.setup do
453
+ cleanup_safe_time_window 6.months
454
+ end
455
+ ```
456
+
457
+ Run it in your repository and commit the result:
458
+
459
+ ```bash
460
+ bundle exec rake 'deploy_pin:cleanup[dry_run]' # lists the files it would remove, one per line
461
+ bundle exec rake deploy_pin:cleanup # removes them
462
+ git commit -am 'Remove old DeployPin tasks'
463
+ ```
464
+
465
+ - Without `cleanup_safe_time_window`, cleanup removes nothing and fails with a message saying how to set it.
466
+ - Only source files are removed. Cleanup doesn't read or change the stored records, so it doesn't need access to your production database. If a removed file comes back, environments where it already ran skip it, because its record is still there.
467
+ - Recurring tasks, the built-in deployment state tasks and tasks whose identifier isn't a 14-digit timestamp are never removed. Tasks of groups no longer in `groups` are removed like the others.
468
+ - A task's tests are removed before its file. If a file can't be removed, cleanup removes the rest, then fails and names it. The task's file is kept, so the next cleanup tries again.
469
+ - Cleanup goes by age only: it doesn't know whether a task has run anywhere. Once removed, a task that is still pending somewhere (an environment that hasn't deployed within the window, a failed deploy, a long-lived release branch) never runs there. Pick a window longer than your slowest path from generating a task to running it in every environment, and check the dry run before you commit.
470
+
437
471
  ### Deployment state
438
472
 
439
473
  DeployPin can store in Redis whether a deploy is in progress, so your code can check it:
@@ -563,6 +597,7 @@ All options are set in `DeployPin.setup`. See the [API docs](https://rubydoc.inf
563
597
  | `fallback_group` | — | Group for `deploy_pin:task` when `-g` is not given |
564
598
  | `statement_timeout` | — | Default database statement timeout |
565
599
  | `storage` | `:active_record` | `:active_record`, `:mongoid`, or a record class |
600
+ | `cleanup_safe_time_window` | not set | How long after its creation `deploy_pin:cleanup` may remove a one-off task's file. See [Cleaning up old tasks](#cleaning-up-old-tasks) |
566
601
  | `continue_on_error` | `false` | Log failed tasks and continue instead of stopping |
567
602
  | `test_framework` | detected (`:rspec` if `RSpec` is loaded) | Test framework for generated task tests; `false` to skip them. See [Testing tasks](#testing-tasks) |
568
603
  | `tests_path` | `'spec/deploy_pin'` | Directory for generated task tests |
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeployPin
4
+ # Raised by `rake deploy_pin:cleanup` when {DeployPin.cleanup_safe_time_window}
5
+ # is not set: cleanup removes nothing until a window is chosen.
6
+ class CleanupWindowNotSetError < StandardError
7
+ # @return [String]
8
+ def message
9
+ 'Set DeployPin.cleanup_safe_time_window to choose how long after its creation a task can be ' \
10
+ 'removed, e.g. `cleanup_safe_time_window 6.months` in DeployPin.setup.'
11
+ end
12
+ end
13
+
14
+ # Raised by `rake deploy_pin:cleanup` when some files could not be removed.
15
+ # A task's file is only removed after its tests, so a task whose test could
16
+ # not be removed is kept, and the next cleanup tries again.
17
+ class CleanupError < StandardError
18
+ # @return [Array<String>] the files that were removed
19
+ attr_reader :removed
20
+ # @return [Hash{String => SystemCallError}] the files that could not be removed, and why
21
+ attr_reader :failures
22
+
23
+ # @param removed [Array<String>]
24
+ # @param failures [Hash{String => SystemCallError}]
25
+ def initialize(removed:, failures:)
26
+ @removed = removed
27
+ @failures = failures
28
+ super("Could not remove #{failures.map { |path, error| "#{path} (#{error.message})" }.join(', ')}")
29
+ end
30
+ end
31
+
32
+ # Removes the source files and the tests of the one-off tasks created (see
33
+ # {Task#created_at}) more than {DeployPin.cleanup_safe_time_window} ago.
34
+ # Recurring and built-in tasks are kept. Only files are removed: the stored
35
+ # records are neither read nor changed, so a removed file that comes back
36
+ # is not run again where it ran.
37
+ #
38
+ # Each task's tests are removed before its file, so a task whose tests
39
+ # could not be removed stays, and the next cleanup finds them again. Tasks
40
+ # are taken in file name order, whatever their group, so tasks of groups
41
+ # no longer in {DeployPin.groups} are removed too.
42
+ #
43
+ # Files are removed by the paths they were found at, never by a normalized
44
+ # copy: `File.expand_path` collapses `link/..` without following the link,
45
+ # which can name another file. A test file that is also a task file (when
46
+ # {DeployPin.tests_path} and {DeployPin.tasks_path} overlap) is not removed
47
+ # as a test.
48
+ class Cleanup
49
+ # @param tasks [Array<Task>] parsed tasks
50
+ def initialize(tasks)
51
+ @tasks = tasks
52
+ @task_sources = nil
53
+ end
54
+
55
+ # @param dry_run [Boolean] only return the files, without removing them
56
+ # @return [Array<String>] the removed (or, on a dry run, removable) files,
57
+ # without the current directory's prefix when they start with it
58
+ # @raise [CleanupWindowNotSetError] if {DeployPin.cleanup_safe_time_window} is not set
59
+ # @raise [CleanupError] if some files could not be removed; the others are
60
+ # removed, and listed in {CleanupError#removed}
61
+ # :reek:BooleanParameter and :reek:ControlParameter
62
+ def call(dry_run: false)
63
+ files = files_by_task
64
+ return files.flatten.map { |path| display_path(path) } if dry_run
65
+
66
+ remove(files)
67
+ end
68
+
69
+ private
70
+
71
+ # The files of each removable task: its tests, then the task file.
72
+ # :reek:TooManyStatements and :reek:NestedIterators
73
+ def files_by_task
74
+ window = DeployPin.cleanup_safe_time_window
75
+ raise CleanupWindowNotSetError unless window
76
+
77
+ before = Time.current - window
78
+ @tasks.select { |task| task.cleanable?(before:) }.sort_by(&:file).map do |task|
79
+ [*task.test_files.reject { |path| task_source?(path) }, task.file]
80
+ end
81
+ end
82
+
83
+ # Whether a file is a task file, with an identifier in its header,
84
+ # whatever path names it. Generated tests have no identifier, so they are
85
+ # removed even when they are in the tasks directory.
86
+ def task_source?(path)
87
+ @task_sources ||= @tasks.select { |task| task.identifier && !task.built_in? }
88
+ .filter_map { |task| real_path(task.file) }.to_set
89
+ @task_sources.include?(real_path(path))
90
+ end
91
+
92
+ # :reek:TooManyStatements
93
+ def remove(files)
94
+ removed = []
95
+ failures = {}
96
+ files.each { |paths| remove_task(paths, removed, failures) }
97
+ raise CleanupError.new(removed:, failures:) if failures.any?
98
+
99
+ removed
100
+ end
101
+
102
+ # Removes a task's files in order, stopping at the first failure so that
103
+ # the task file, which comes last, is kept.
104
+ # :reek:UtilityFunction and :reek:TooManyStatements
105
+ def remove_task(paths, removed, failures)
106
+ paths.each do |path|
107
+ File.delete(path)
108
+ removed << display_path(path)
109
+ rescue Errno::ENOENT
110
+ next # already gone
111
+ rescue SystemCallError => e
112
+ failures[display_path(path)] = e
113
+ break
114
+ end
115
+ end
116
+
117
+ # The path without the current directory's prefix (and the slashes after
118
+ # it), `.` components and repeated slashes, which name the same file from the current directory.
119
+ # `..` components are kept: after a symbolic link, they lead elsewhere
120
+ # than a lexical normalization says.
121
+ # :reek:UtilityFunction and :reek:TooManyStatements
122
+ def display_path(path)
123
+ cwd = Regexp.escape(Dir.pwd.chomp('/'))
124
+ head, *parts = path.sub(%r{\A#{cwd}/+}, '').split('/', -1)
125
+ parts = parts.reject { |part| part.empty? || part == '.' }
126
+ head == '.' ? parts.join('/') : [head, *parts].join('/')
127
+ end
128
+
129
+ # :reek:UtilityFunction
130
+ def real_path(path)
131
+ File.realpath(path)
132
+ rescue SystemCallError
133
+ nil
134
+ end
135
+ end
136
+ end
@@ -90,6 +90,20 @@ module DeployPin
90
90
  end
91
91
  # :reek:FeatureEnvy
92
92
 
93
+ # Removes the source files and the tests of the one-off tasks created
94
+ # more than {DeployPin.cleanup_safe_time_window} ago, whatever the
95
+ # selected identifiers. See {Cleanup}.
96
+ #
97
+ # @param dry_run [Boolean] only return the files, without removing them
98
+ # @return [Array<String>] the removed (or, on a dry run, removable) files
99
+ # @raise [CleanupWindowNotSetError] if {DeployPin.cleanup_safe_time_window} is not set
100
+ # @raise [DuplicateIdentifierError] if two one-off tasks have the same identifier
101
+ # @raise [CleanupError] if some files could not be removed
102
+ # :reek:BooleanParameter
103
+ def cleanup(dry_run: false)
104
+ DeployPin::Cleanup.new(all_tasks).call(dry_run:)
105
+ end
106
+
93
107
  # The selected tasks without duplicates (tasks whose script is the same as an earlier task's).
94
108
  #
95
109
  # @return [Array<Task>]
@@ -142,16 +156,20 @@ module DeployPin
142
156
  Dir["#{DeployPin.tasks_path}/*.rb"]
143
157
  end
144
158
 
145
- def init_tasks
159
+ # All the tasks, parsed, before selecting.
160
+ def all_tasks
146
161
  tasks = [*DeployPin.deployment_tasks_code, *files].map do |file|
147
162
  DeployPin::Task.new(file).tap(&:parse)
148
163
  end
149
164
  # One-off tasks that share an identifier share their completion record. Checked
150
165
  # before selecting: a collision must not hide behind an unselected group.
151
166
  DuplicateIdentifierError.check!(tasks)
167
+ tasks
168
+ end
152
169
 
170
+ def init_tasks
153
171
  # check if task is suitable
154
- tasks.select { |task| task_criteria.suitable?(task) }.sort # sort by group position in config
172
+ all_tasks.select { |task| task_criteria.suitable?(task) }.sort # sort by group position in config
155
173
  end
156
174
 
157
175
  def task_criteria
@@ -14,17 +14,13 @@ module DeployPin
14
14
  # deploy_pin task backfill_users -g I --skip-test
15
15
  # deploy_pin task backfill_users -g I --test-framework=rspec
16
16
  class Task < Base # rubocop:disable Metrics/ClassLength
17
- # Supported test frameworks: the default directory for the tests (used
18
- # when {DeployPin.tests_path} is not set) and the test file name, where
19
- # `%<task>s` is the task file name without `.rb`.
20
- TEST_FRAMEWORKS = {
21
- rspec: { path: 'spec/deploy_pin', file: '%<task>s_spec.rb' }
22
- }.freeze
17
+ # Supported test frameworks; see {DeployPin::TestFiles::FRAMEWORKS}.
18
+ TEST_FRAMEWORKS = DeployPin::TestFiles::FRAMEWORKS
23
19
 
24
20
  # The identifiers a task header accepts.
25
21
  IDENTIFIER_FORMAT = /\A-?\d+\z/
26
22
  # Format of one-off task identifiers: a timestamp.
27
- TIMESTAMP_FORMAT = '%Y%m%d%H%M%S'
23
+ TIMESTAMP_FORMAT = DeployPin::Task::TIMESTAMP_FORMAT
28
24
 
29
25
  # Printed when no test framework is configured and none is detected.
30
26
  UNDETECTED_TEST_FRAMEWORK = 'test for the task: no test framework detected. ' \
@@ -121,10 +117,7 @@ module DeployPin
121
117
 
122
118
  # The test file in {DeployPin.tests_path}, or in the framework's default directory.
123
119
  def test_file_path(framework)
124
- settings = TEST_FRAMEWORKS.fetch(framework)
125
- directory = DeployPin.tests_path.presence || settings[:path]
126
-
127
- File.join(directory.to_s, format(settings[:file], task: task_name))
120
+ DeployPin::TestFiles.path(framework, task_name)
128
121
  end
129
122
 
130
123
  # The --test-framework flag or DeployPin.test_framework, else the detected one.
@@ -8,6 +8,9 @@ DeployPin.setup do
8
8
  groups %w[pre I II III]
9
9
  fallback_group 'III'
10
10
  statement_timeout 10.minutes
11
+ # How long after its creation `rake deploy_pin:cleanup` may remove a one-off task's file:
12
+ # your deployment cycle, from generating a task to running it everywhere, plus a buffer.
13
+ # cleanup_safe_time_window 6.months
11
14
  # Tests generated for each task: RSpec is used when it is loaded, or set it here.
12
15
  # test_framework :rspec # false to not generate tests
13
16
  # tests_path 'spec/deploy_pin'
@@ -43,6 +43,39 @@ module DeployPin
43
43
  DeployPin::Collector.new(identifiers:).mark_done
44
44
  end
45
45
 
46
+ # Removes the source files and tests of the one-off tasks created more than
47
+ # {DeployPin.cleanup_safe_time_window} ago (see {Collector#cleanup}), and
48
+ # prints each one and how many were removed. A dry run prints only the
49
+ # paths, one per line, so the output can be passed to `git rm`.
50
+ #
51
+ # When some files cannot be removed, the removed ones are still printed
52
+ # before the error is raised.
53
+ #
54
+ # @param dry_run [Boolean] only print the files, without removing them
55
+ # @return [Array<String>] the removed (or, on a dry run, removable) files
56
+ # @raise [CleanupWindowNotSetError] if {DeployPin.cleanup_safe_time_window} is not set
57
+ # @raise [CleanupError] if some files could not be removed
58
+ # :reek:BooleanParameter and :reek:TooManyStatements
59
+ def self.cleanup(dry_run: false)
60
+ paths = DeployPin::Collector.new(identifiers: DeployPin.groups).cleanup(dry_run:)
61
+ return paths.each { |path| self.print(path) } if dry_run
62
+
63
+ print_removed(paths)
64
+ rescue DeployPin::CleanupError => e
65
+ print_removed(e.removed)
66
+ raise
67
+ end
68
+
69
+ # Prints each removed file and how many there are.
70
+ #
71
+ # @param paths [Array<String>]
72
+ # @return [Array<String>] the paths
73
+ def self.print_removed(paths)
74
+ paths.each { |path| self.print("Removed #{path}") }
75
+ self.print("Removed #{paths.count} #{paths.one? ? 'file' : 'files'}")
76
+ paths
77
+ end
78
+
46
79
  # Prints how many tasks are selected.
47
80
  #
48
81
  # @param identifiers [Array<String, Integer>] groups and task identifiers to select;
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'ripper'
4
+ require 'time'
4
5
 
5
6
  module DeployPin
6
7
  # A task loaded from a file in {DeployPin.tasks_path}.
@@ -130,6 +131,50 @@ module DeployPin
130
131
  record.completed_at.present?
131
132
  end
132
133
 
134
+ # Format of one-off task identifiers: the time the task was created.
135
+ TIMESTAMP_FORMAT = '%Y%m%d%H%M%S'
136
+
137
+ # When the task was created: its identifier read as a {TIMESTAMP_FORMAT}
138
+ # timestamp, in the local time zone like the generator writes it. The
139
+ # identifier must be exactly 14 digits and a real time: `Time.strptime`
140
+ # alone ignores extra digits and turns February 30 into March 2.
141
+ #
142
+ # @return [Time, nil] nil for recurring tasks and identifiers that are not timestamps
143
+ def created_at
144
+ digits = identifier.to_s
145
+ return if recurring || !digits.match?(/\A\d{14}\z/)
146
+
147
+ time = Time.strptime(digits, TIMESTAMP_FORMAT)
148
+ time if time.strftime(TIMESTAMP_FORMAT) == digits
149
+ rescue ArgumentError
150
+ nil
151
+ end
152
+
153
+ # Whether `rake deploy_pin:cleanup` may remove the task's source: a
154
+ # one-off task file created before `before`. Recurring and built-in tasks,
155
+ # and tasks whose identifier is not a timestamp, are never removed. The
156
+ # stored records are not looked at.
157
+ #
158
+ # @param before [Time] tasks created before this time can be removed
159
+ # @return [Boolean]
160
+ def cleanable?(before:)
161
+ return false if built_in?
162
+
163
+ created = created_at
164
+ created.present? && created < before
165
+ end
166
+
167
+ # @return [Array<String>] the test files of a task file that exist (see {TestFiles.existing})
168
+ def test_files
169
+ DeployPin::TestFiles.existing(File.basename(file, '.rb'))
170
+ end
171
+
172
+ # @return [Boolean] whether the task is a built-in one, defined in code
173
+ # (starting with `# no_file_task`) rather than in a file
174
+ def built_in?
175
+ file.starts_with?('# no_file_task')
176
+ end
177
+
133
178
  # @return [Boolean] whether the task should run under {DeployPin.statement_timeout}
134
179
  def under_timeout?
135
180
  !explicit_timeout? && !parallel?
@@ -165,7 +210,7 @@ module DeployPin
165
210
  def each_line(&block)
166
211
  return enum_for(:each_line) unless block
167
212
 
168
- if file.starts_with?('# no_file_task')
213
+ if built_in?
169
214
  file.each_line(&block)
170
215
  else
171
216
  File.foreach(file, &block)
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeployPin
4
+ # Where the tests of tasks are: the task generator writes them there, and
5
+ # `rake deploy_pin:cleanup` removes them with their tasks.
6
+ module TestFiles
7
+ # Supported test frameworks: the default directory for the tests (used
8
+ # when {DeployPin.tests_path} is not set) and the test file name, where
9
+ # `%<task>s` is the task file name without `.rb`.
10
+ FRAMEWORKS = {
11
+ rspec: { path: 'spec/deploy_pin', file: '%<task>s_spec.rb' }
12
+ }.freeze
13
+
14
+ # The test file of a task, in {DeployPin.tests_path} or in the framework's
15
+ # default directory.
16
+ #
17
+ # @param framework [Symbol] one of {FRAMEWORKS}
18
+ # @param task_name [String] the task file name without `.rb`, e.g. `20190401135040_backfill_users`
19
+ # @return [String]
20
+ def self.path(framework, task_name)
21
+ settings = FRAMEWORKS.fetch(framework)
22
+ directory = DeployPin.tests_path.presence || settings[:path]
23
+
24
+ File.join(directory.to_s, format(settings[:file], task: task_name))
25
+ end
26
+
27
+ # The test files of a task that exist, for every framework, in
28
+ # {DeployPin.tests_path} and in the frameworks' default directories: the
29
+ # framework may have been detected rather than configured, and
30
+ # {DeployPin.tests_path} set after the test was generated.
31
+ #
32
+ # @param task_name [String] the task file name without `.rb`
33
+ # @return [Array<String>]
34
+ def self.existing(task_name)
35
+ paths = FRAMEWORKS.values.product([DeployPin.tests_path.presence, nil]).map do |settings, directory|
36
+ File.join((directory || settings[:path]).to_s, format(settings[:file], task: task_name))
37
+ end
38
+ paths.uniq.select { |path| File.file?(path) }
39
+ end
40
+ end
41
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module DeployPin
4
4
  # The gem version.
5
- VERSION = '2.0.1'
5
+ VERSION = '2.1.0'
6
6
  end
data/lib/deploy_pin.rb CHANGED
@@ -6,9 +6,11 @@ require 'deploy_pin/version'
6
6
  require 'deploy_pin/deployment_state'
7
7
  require 'deploy_pin/runner'
8
8
  require 'deploy_pin/collector'
9
+ require 'deploy_pin/cleanup'
9
10
  require 'deploy_pin/parallel_wrapper'
10
11
  require 'deploy_pin/task'
11
12
  require 'deploy_pin/task_criteria'
13
+ require 'deploy_pin/test_files'
12
14
  require 'deploy_pin/database'
13
15
  require 'deploy_pin/database_engine'
14
16
  require 'deploy_pin/storage'
@@ -37,6 +39,18 @@ require 'deploy_pin/engine' if defined?(Rails::Engine)
37
39
  # statement_timeout 10.minutes
38
40
  # end
39
41
  #
42
+ # @!method self.cleanup_safe_time_window(val = nil)
43
+ # How long after a one-off task was created (the timestamp in its
44
+ # identifier) `rake deploy_pin:cleanup` may remove its file and its test:
45
+ # your deployment cycle, from generating a task to running it in every
46
+ # environment, plus a safety buffer. Not set by default: cleanup then
47
+ # removes nothing and raises {CleanupWindowNotSetError}.
48
+ # @example
49
+ # cleanup_safe_time_window 6.months
50
+ # @param val [ActiveSupport::Duration, Numeric, nil] new value (seconds for a
51
+ # Numeric); omit to read the current one
52
+ # @return [ActiveSupport::Duration, Numeric, nil]
53
+ #
40
54
  # @!method self.continue_on_error(val = nil)
41
55
  # When true, a failing task is logged and the run moves on to the next task
42
56
  # instead of raising. Defaults to `false`.
@@ -111,7 +125,7 @@ require 'deploy_pin/engine' if defined?(Rails::Engine)
111
125
  # next to each task; `false` turns test generation off. When not set, RSpec
112
126
  # is used if the `RSpec` constant is loaded (e.g. rspec-rails in the
113
127
  # `:development` group); otherwise no test is generated.
114
- # @see TaskGenerator::TEST_FRAMEWORKS
128
+ # @see TestFiles::FRAMEWORKS
115
129
  # @param val [Symbol, false, nil] new value; omit to read the current one
116
130
  # @return [Symbol, false, nil]
117
131
  #
@@ -133,6 +147,7 @@ module DeployPin
133
147
 
134
148
  # Names of the configuration options. Each one is a reader/writer method on {DeployPin}.
135
149
  OPTIONS = %i[
150
+ cleanup_safe_time_window
136
151
  continue_on_error
137
152
  deployment_state_transition
138
153
  fallback_group
@@ -55,4 +55,16 @@ namespace :deploy_pin do
55
55
  DeployPin::Runner.mark_done(identifiers:)
56
56
  DeployPin::Runner.summary(identifiers:)
57
57
  end
58
+
59
+ # `deploy_pin:cleanup[dry_run]`, or DRY_RUN=1, only prints the files.
60
+ desc 'remove the files of one-off tasks created more than cleanup_safe_time_window ago ([dry_run] lists them)'
61
+ task :cleanup, [:mode] => 'deploy_pin:environment' do |_t, args|
62
+ mode = args.mode.to_s.strip
63
+ unless ['', 'dry_run'].include?(mode)
64
+ abort "Unknown deploy_pin:cleanup mode '#{mode}'. Use deploy_pin:cleanup[dry_run]"
65
+ end
66
+
67
+ dry_run = mode == 'dry_run' || %w[1 true yes].include?(ENV['DRY_RUN'].to_s.downcase)
68
+ DeployPin::Runner.cleanup(dry_run:)
69
+ end
58
70
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deploy_pin
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Viktor Sych
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-11-24 00:00:00.000000000 Z
11
+ date: 2026-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -402,6 +402,7 @@ files:
402
402
  - Rakefile
403
403
  - exe/deploy_pin
404
404
  - lib/deploy_pin.rb
405
+ - lib/deploy_pin/cleanup.rb
405
406
  - lib/deploy_pin/cli.rb
406
407
  - lib/deploy_pin/collector.rb
407
408
  - lib/deploy_pin/database.rb
@@ -428,6 +429,7 @@ files:
428
429
  - lib/deploy_pin/storage/mongoid_record.rb
429
430
  - lib/deploy_pin/task.rb
430
431
  - lib/deploy_pin/task_criteria.rb
432
+ - lib/deploy_pin/test_files.rb
431
433
  - lib/deploy_pin/version.rb
432
434
  - lib/generators/deploy_pin/install/USAGE
433
435
  - lib/generators/deploy_pin/install/install_generator.rb