deploy_pin 2.0.0 → 2.0.1
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 +4 -4
- data/README.md +26 -5
- data/lib/deploy_pin/cli.rb +1 -1
- data/lib/deploy_pin/collector.rb +34 -6
- data/lib/deploy_pin/database.rb +88 -23
- data/lib/deploy_pin/database_engine.rb +69 -3
- data/lib/deploy_pin/generators/task.rb +45 -4
- data/lib/deploy_pin/generators/templates/upgrade/upgrade_deploy_pins.rb +16 -5
- data/lib/deploy_pin/generators/upgrade.rb +4 -3
- data/lib/deploy_pin/parallel_wrapper.rb +22 -18
- data/lib/deploy_pin/task.rb +147 -21
- data/lib/deploy_pin/version.rb +1 -1
- data/lib/deploy_pin.rb +20 -1
- data/lib/generators/deploy_pin/upgrade/USAGE +6 -1
- data/lib/generators/deploy_pin/upgrade/upgrade_generator.rb +3 -2
- data/lib/tasks/deploy_pin_tasks.rake +4 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ee7d5ecb0f1089412e2355727340b0c3ea1770b14dd27a09113a50ecfdadbbe4
|
|
4
|
+
data.tar.gz: 8f3274681a343638fa28b2b10e8e36f2b5c053a678d8cd1b33ed46157bb1a618
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 99540af5bdc9a3494916e63c8173865b108f3f2083ab1e79748d3b8148dd5d7fa77f701fc50ae29829e4c0981dc33e36fe68e752cb79be190a8f7d956546b9bc
|
|
7
|
+
data.tar.gz: c65f5bd5ade9efdbe7f0ebf4f88c3d8d6a6459ce359c87e218d322ba0346f6890ac01a43bd814a1cbcb58dd0493cb3c2733a4485b346b2e6d1cad58f5b0a50c1
|
data/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
[](#requirements)
|
|
9
9
|
[](MIT-LICENSE)
|
|
10
10
|
|
|
11
|
-
**Run one-off and recurring tasks around your deployments — data backfills, cache warm-ups, cleanups — in a defined order,
|
|
11
|
+
**Run one-off and recurring tasks around your deployments — data backfills, cache warm-ups, cleanups — in a defined order, once per task, with database timeouts and resumable progress.**
|
|
12
12
|
|
|
13
13
|
Migrations are for schema changes. Everything else you need to run during a release (backfill a column, re-index records, call an external API, warm a cache) usually ends up in an ad-hoc rake task that someone has to remember to run. DeployPin keeps these tasks in your repository as plain Ruby files, runs each one once, in the order you define, and remembers what has already run.
|
|
14
14
|
|
|
@@ -178,6 +178,10 @@ User.where(name: nil).find_each { |user| user.update!(name: user.email) }
|
|
|
178
178
|
| `# task_title:` | Shown by `list` and `run` |
|
|
179
179
|
| `# affected_areas:` | Free text for reviewers: what the task touches |
|
|
180
180
|
|
|
181
|
+
The header is the comment block at the top of the file, up to `# === task code goes down here ===` (or up to the first line of code in files without that line). DeployPin reads the header lines from there only. Everything after it is the task's code, run exactly as written, so comments and lines such as `# 456:II` inside strings or heredocs are kept and do not change the task.
|
|
182
|
+
|
|
183
|
+
The generator gives each one-off task an unused identifier: two tasks generated in the same second get consecutive timestamps. One-off tasks must not share an identifier, because they share its completion record. `deploy_pin:run`, `list`, `short_list` and `mark_done` stop with an error naming both files if two do, whatever the selected groups. Give the task that has not run yet a new identifier.
|
|
184
|
+
|
|
181
185
|
### Groups and ordering
|
|
182
186
|
|
|
183
187
|
`groups` defines the steps of your deploy, in order. Group names are up to you, and so is the moment each group runs: you choose it by calling `rake 'deploy_pin:run[<group>]'` at that point of your deploy. You can have as many stages as you need. For example:
|
|
@@ -190,7 +194,15 @@ User.where(name: nil).find_each { |user| user.update!(name: user.email) }
|
|
|
190
194
|
|
|
191
195
|
Tasks are sorted by **group**, then by **identifier**. Negative identifiers run after the other tasks of their group.
|
|
192
196
|
|
|
193
|
-
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.
|
|
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
|
+
|
|
199
|
+
### Running one executor at a time
|
|
200
|
+
|
|
201
|
+
DeployPin does not coordinate overlapping runs. Serialize the invocations that change task state against the same storage: run one `deploy_pin:run` at a time, from a designated release job or host, and don't run `deploy_pin:mark_done` while a run is in progress. Two executors that overlap can both select the same pending task and run it.
|
|
202
|
+
|
|
203
|
+
A task is marked done after it finishes. If the process stops after the task changed something but before it was marked done, the next run runs the task again, so write tasks that can resume (see [progress](#resumable-tasks)) or are safe to run again.
|
|
204
|
+
|
|
205
|
+
This is about separate DeployPin invocations. Parallel workers inside a single task, with [`parallel_*`](#parallel), are supported.
|
|
194
206
|
|
|
195
207
|
### Selecting tasks
|
|
196
208
|
|
|
@@ -219,6 +231,7 @@ rake 'deploy_pin:run[I, !20190401135040]' # group I except one task
|
|
|
219
231
|
|---|---|
|
|
220
232
|
| `rails g deploy_pin:task <title> -g <group>` | Generate a task and its [test](#testing-tasks). Options: `--parallel`, `--recurring --identifier <n>`, `-a <author>`, `--skip-test`, `--test-framework=rspec` |
|
|
221
233
|
| `deploy_pin install`, `deploy_pin task <title> -g <group>`, `deploy_pin upgrade` | The generators, [without Rails](#without-rails). `--require <file>` loads files first |
|
|
234
|
+
| `rails g deploy_pin:upgrade`, `deploy_pin upgrade` | Only for a `deploy_pins` table created before DeployPin 1.7: a migration that adds the columns for resumable tasks and marks the existing records done. It changes nothing on newer tables, and cannot be rolled back: it raises `ActiveRecord::IrreversibleMigration` instead of removing the columns |
|
|
222
235
|
| `rake deploy_pin:list[...]` | List pending tasks, including their code |
|
|
223
236
|
| `rake deploy_pin:short_list[...]` | List pending tasks by group, without the code |
|
|
224
237
|
| `rake deploy_pin:run[...]` | Run pending tasks |
|
|
@@ -376,7 +389,15 @@ DeployPin::Database.execute_with_timeout 30.minutes do
|
|
|
376
389
|
end
|
|
377
390
|
```
|
|
378
391
|
|
|
379
|
-
It also accepts `connected_to:`, to run against a replica or
|
|
392
|
+
It also accepts `connected_to:` with `role:`, `shard:` and `prevent_writes:`, to run against a replica or a shard declared with `connects_to` in your `ApplicationRecord`:
|
|
393
|
+
|
|
394
|
+
```ruby
|
|
395
|
+
DeployPin::Database.execute_with_timeout 1.minute, connected_to: { role: :reading } do
|
|
396
|
+
Shipment.count
|
|
397
|
+
end
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
`connected_to: { database: ... }` is not supported by ActiveRecord 7.2 and later, and raises an `ArgumentError`, as do other options. The timeout is set on the connection the block uses, and that connection's previous timeout is restored afterwards, also when the block raises. See the [`DeployPin::Database` docs](https://rubydoc.info/gems/deploy_pin/DeployPin/Database).
|
|
380
401
|
|
|
381
402
|
### Parallel
|
|
382
403
|
|
|
@@ -388,7 +409,7 @@ parallel_each(User.ids.each_slice(1_000).to_a, in_processes: 4, timeout: 5.minut
|
|
|
388
409
|
end
|
|
389
410
|
```
|
|
390
411
|
|
|
391
|
-
See the [`DeployPin::ParallelWrapper` docs](https://rubydoc.info/gems/deploy_pin/DeployPin/ParallelWrapper) for passing timeout options.
|
|
412
|
+
`timeout:` is used by DeployPin, and the other options (`in_processes:`, `in_threads:`, `progress:`…) are passed to Parallel. See the [`DeployPin::ParallelWrapper` docs](https://rubydoc.info/gems/deploy_pin/DeployPin/ParallelWrapper) for passing timeout options.
|
|
392
413
|
|
|
393
414
|
### Resumable tasks
|
|
394
415
|
|
|
@@ -404,7 +425,7 @@ end
|
|
|
404
425
|
|
|
405
426
|
### Recurring tasks
|
|
406
427
|
|
|
407
|
-
Recurring tasks run on every deploy and are never marked done. Their identifier is
|
|
428
|
+
Recurring tasks run on every deploy and are never marked done. Their identifier is an integer you choose (negative ones run last in their group), which sets their order within the group. `--identifier` is required, and the generator writes nothing if it is missing or not an integer:
|
|
408
429
|
|
|
409
430
|
```bash
|
|
410
431
|
rails g deploy_pin:task warm_caches -g III --recurring --identifier 5
|
data/lib/deploy_pin/cli.rb
CHANGED
|
@@ -71,7 +71,7 @@ module DeployPin
|
|
|
71
71
|
register Generators::Task, 'task', 'task TITLE -g GROUP [--require config/environment.rb]',
|
|
72
72
|
'Create a task in DeployPin.tasks_path, and a test for it'
|
|
73
73
|
register Generators::Upgrade, 'upgrade', 'upgrade',
|
|
74
|
-
'Create the migration that upgrades
|
|
74
|
+
'Create the migration that upgrades a deploy_pins table created before DeployPin 1.7'
|
|
75
75
|
|
|
76
76
|
desc 'version', 'Print the DeployPin version'
|
|
77
77
|
# Prints the DeployPin version.
|
data/lib/deploy_pin/collector.rb
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module DeployPin
|
|
4
|
+
# Raised when two one-off task files have the same identifier: they would
|
|
5
|
+
# share one completion record.
|
|
6
|
+
class DuplicateIdentifierError < StandardError
|
|
7
|
+
# Raises if one-off tasks share an identifier, naming their files.
|
|
8
|
+
#
|
|
9
|
+
# @param tasks [Array<Task>] parsed tasks
|
|
10
|
+
# @return [void]
|
|
11
|
+
# @raise [DuplicateIdentifierError]
|
|
12
|
+
def self.check!(tasks)
|
|
13
|
+
duplicates = tasks.reject(&:recurring).group_by(&:identifier).select do |identifier, same|
|
|
14
|
+
identifier && same.size > 1
|
|
15
|
+
end
|
|
16
|
+
raise self, message(duplicates) if duplicates.any?
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# @param duplicates [Hash{Integer => Array<Task>}] tasks by shared identifier
|
|
20
|
+
# @return [String]
|
|
21
|
+
def self.message(duplicates)
|
|
22
|
+
messages = duplicates.map do |identifier, same|
|
|
23
|
+
"Tasks #{same.map(&:file).sort.join(' and ')} have the same identifier #{identifier}."
|
|
24
|
+
end
|
|
25
|
+
[*messages, 'Give all but the one that already ran a new identifier, e.g. by generating them again.'].join(' ')
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
4
29
|
# Loads the task files and the deployment state tasks, selects the ones
|
|
5
30
|
# matching the identifiers, sorts them by group and identifier, and runs or
|
|
6
31
|
# lists them.
|
|
@@ -21,6 +46,7 @@ module DeployPin
|
|
|
21
46
|
#
|
|
22
47
|
# @return [void]
|
|
23
48
|
# @raise [StandardError] a task's error, unless {DeployPin.continue_on_error} is set
|
|
49
|
+
# @raise [DuplicateIdentifierError] if two one-off tasks have the same identifier
|
|
24
50
|
# :reek:TooManyStatements
|
|
25
51
|
def run
|
|
26
52
|
tasks = init_tasks
|
|
@@ -117,13 +143,15 @@ module DeployPin
|
|
|
117
143
|
end
|
|
118
144
|
|
|
119
145
|
def init_tasks
|
|
120
|
-
[*DeployPin.deployment_tasks_code, *files].map do |file|
|
|
121
|
-
|
|
122
|
-
|
|
146
|
+
tasks = [*DeployPin.deployment_tasks_code, *files].map do |file|
|
|
147
|
+
DeployPin::Task.new(file).tap(&:parse)
|
|
148
|
+
end
|
|
149
|
+
# One-off tasks that share an identifier share their completion record. Checked
|
|
150
|
+
# before selecting: a collision must not hide behind an unselected group.
|
|
151
|
+
DuplicateIdentifierError.check!(tasks)
|
|
123
152
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
end.compact.sort # sort by group position in config
|
|
153
|
+
# check if task is suitable
|
|
154
|
+
tasks.select { |task| task_criteria.suitable?(task) }.sort # sort by group position in config
|
|
127
155
|
end
|
|
128
156
|
|
|
129
157
|
def task_criteria
|
data/lib/deploy_pin/database.rb
CHANGED
|
@@ -29,27 +29,34 @@ module DeployPin
|
|
|
29
29
|
# Shipment.all
|
|
30
30
|
# end
|
|
31
31
|
#
|
|
32
|
-
# @example Run against
|
|
33
|
-
# DeployPin::Database.execute_with_timeout 30.seconds, connected_to: {
|
|
34
|
-
#
|
|
32
|
+
# @example Run against a shard declared with `connects_to shards: { ... }`
|
|
33
|
+
# DeployPin::Database.execute_with_timeout 30.seconds, connected_to: { shard: :shard_two } do
|
|
34
|
+
# Shipment.where(state: 'stuck').delete_all
|
|
35
35
|
# end
|
|
36
36
|
#
|
|
37
|
+
# The timeout is set on the connection the block uses: the one of
|
|
38
|
+
# `ActiveRecord::Base`, or with `connected_to:`, the one of the app's
|
|
39
|
+
# `ApplicationRecord` (or `ActiveRecord::Base` without one) for that role
|
|
40
|
+
# or shard. The connection's previous timeout is restored when the block
|
|
41
|
+
# returns or raises, so nested calls and later queries are not affected.
|
|
42
|
+
#
|
|
37
43
|
# @param timeout [ActiveSupport::Duration, Numeric] timeout, in seconds when numeric
|
|
38
44
|
# @param params [Hash] options
|
|
39
|
-
# @option params [Hash] :connected_to
|
|
45
|
+
# @option params [Hash] :connected_to `role:`, `shard:` and `prevent_writes:`, passed to `connected_to`
|
|
40
46
|
# @yield the code to run under the timeout
|
|
41
47
|
# @return [Object] the result of the block
|
|
42
48
|
# @raise [NotImplementedError] if ActiveRecord is not loaded
|
|
43
|
-
# @raise [ArgumentError] if the timeout is not greater than zero
|
|
49
|
+
# @raise [ArgumentError] if the timeout is not greater than zero, or the options are not supported
|
|
44
50
|
def execute_with_timeout(timeout = DeployPin.statement_timeout, **params, &blk)
|
|
45
51
|
raise NotImplementedError, 'statement timeout requires ActiveRecord' unless available?
|
|
46
52
|
raise ArgumentError, 'timeout must be greater than zero' if timeout.to_f <= 0
|
|
47
53
|
|
|
48
|
-
|
|
54
|
+
connected_to = connected_to_options(params)
|
|
55
|
+
return call_block_under_timeout(timeout, ActiveRecord::Base, &blk) unless connected_to
|
|
49
56
|
|
|
50
|
-
klass =
|
|
51
|
-
klass.connected_to(**
|
|
52
|
-
call_block_under_timeout(timeout, &blk)
|
|
57
|
+
klass = application_record
|
|
58
|
+
klass.connected_to(**connected_to) do
|
|
59
|
+
call_block_under_timeout(timeout, klass, &blk)
|
|
53
60
|
end
|
|
54
61
|
end
|
|
55
62
|
|
|
@@ -62,22 +69,80 @@ module DeployPin
|
|
|
62
69
|
|
|
63
70
|
private
|
|
64
71
|
|
|
65
|
-
|
|
66
|
-
|
|
72
|
+
# Options of `connected_to` that select a connection of the app.
|
|
73
|
+
CONNECTED_TO_OPTIONS = %i[role shard prevent_writes].freeze
|
|
67
74
|
|
|
68
|
-
|
|
75
|
+
# @return [Hash, nil] the `connected_to` options, nil without them
|
|
76
|
+
def connected_to_options(params)
|
|
77
|
+
unknown = params.keys - [:connected_to]
|
|
78
|
+
raise ArgumentError, "Unknown option(s): #{unknown.join(', ')}. Supported: connected_to" if unknown.any?
|
|
79
|
+
|
|
80
|
+
options = params[:connected_to]
|
|
81
|
+
return if options.nil?
|
|
82
|
+
unless options.is_a?(Hash)
|
|
83
|
+
raise ArgumentError, 'connected_to must be a Hash, e.g. connected_to: { role: :reading }'
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
validate_connected_to(options.symbolize_keys)
|
|
69
87
|
end
|
|
70
88
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
89
|
+
# :reek:TooManyStatements
|
|
90
|
+
def validate_connected_to(options)
|
|
91
|
+
if options.key?(:database)
|
|
92
|
+
raise ArgumentError, 'connected_to: { database: ... } is not supported by ActiveRecord 7.2 and later. ' \
|
|
93
|
+
'Declare the database with connects_to, then pass role: or shard:'
|
|
94
|
+
end
|
|
95
|
+
unknown = options.keys - CONNECTED_TO_OPTIONS
|
|
96
|
+
if unknown.any?
|
|
97
|
+
raise ArgumentError, "Unsupported connected_to option(s): #{unknown.join(', ')}. " \
|
|
98
|
+
'Supported: role, shard, prevent_writes'
|
|
99
|
+
end
|
|
100
|
+
raise ArgumentError, 'connected_to needs role: or shard:' unless options.key?(:role) || options.key?(:shard)
|
|
74
101
|
|
|
75
|
-
|
|
76
|
-
|
|
102
|
+
options
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Runs the block on the connection of `klass` that the current thread uses,
|
|
106
|
+
# under the timeout, and restores that connection's previous timeout.
|
|
107
|
+
def call_block_under_timeout(timeout, klass, &block)
|
|
108
|
+
klass.connection_pool.with_connection do |connection|
|
|
109
|
+
engine = db_engine(connection)
|
|
110
|
+
next run_without_timeout(connection, &block) unless engine
|
|
111
|
+
|
|
112
|
+
# at least 1 millisecond: 0 turns the timeout off
|
|
113
|
+
with_engine_timeout(engine, connection, timeout.to_f.in_milliseconds.ceil, &block)
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# :reek:TooManyStatements
|
|
118
|
+
def with_engine_timeout(engine, connection, milliseconds)
|
|
119
|
+
previous = engine.current_timeout(connection)
|
|
120
|
+
engine.apply_timeout(connection, milliseconds)
|
|
121
|
+
succeeded = false
|
|
122
|
+
begin
|
|
123
|
+
result = yield
|
|
124
|
+
succeeded = true
|
|
125
|
+
result
|
|
126
|
+
ensure
|
|
127
|
+
restore_timeout(engine, connection, previous, raise_errors: succeeded)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# A failed restore must not hide the block's own error (e.g. an aborted
|
|
132
|
+
# transaction, whose rollback undoes the SET anyway).
|
|
133
|
+
def restore_timeout(engine, connection, previous, raise_errors:)
|
|
134
|
+
engine.restore_timeout(connection, previous)
|
|
135
|
+
rescue ActiveRecord::ActiveRecordError
|
|
136
|
+
raise if raise_errors
|
|
77
137
|
end
|
|
78
138
|
|
|
79
139
|
# Databases without a statement timeout (e.g. SQLite) run the block without one.
|
|
80
|
-
def
|
|
140
|
+
def run_without_timeout(connection)
|
|
141
|
+
warn_unsupported_engine(connection)
|
|
142
|
+
yield
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def warn_unsupported_engine(connection)
|
|
81
146
|
return if @unsupported_engine_warned
|
|
82
147
|
|
|
83
148
|
@unsupported_engine_warned = true
|
|
@@ -90,12 +155,12 @@ module DeployPin
|
|
|
90
155
|
defined?(::ApplicationRecord) ? ::ApplicationRecord : ActiveRecord::Base
|
|
91
156
|
end
|
|
92
157
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
158
|
+
# The engine of a connection, detected once per connection.
|
|
159
|
+
def db_engine(connection)
|
|
160
|
+
engines = (@db_engines ||= ObjectSpace::WeakMap.new)
|
|
161
|
+
return engines[connection] if engines.key?(connection)
|
|
96
162
|
|
|
97
|
-
|
|
98
|
-
ActiveRecord::Base.connection
|
|
163
|
+
engines[connection] = DeployPin::DatabaseEngine.new(connection).detect
|
|
99
164
|
end
|
|
100
165
|
end
|
|
101
166
|
end
|
|
@@ -3,23 +3,89 @@
|
|
|
3
3
|
module DeployPin
|
|
4
4
|
# Detects the database engine behind an ActiveRecord connection, to pick
|
|
5
5
|
# the right statement timeout SQL.
|
|
6
|
+
#
|
|
7
|
+
# Each engine module reads, sets and restores the session timeout of a connection:
|
|
8
|
+
# `current_timeout(connection)`, `apply_timeout(connection, milliseconds)` and
|
|
9
|
+
# `restore_timeout(connection, value)`, where `value` is what `current_timeout` returned.
|
|
6
10
|
class DatabaseEngine
|
|
7
|
-
# PostgreSQL settings.
|
|
11
|
+
# PostgreSQL settings: `statement_timeout`, in milliseconds.
|
|
8
12
|
module PostgreSQL
|
|
9
13
|
# SQL that sets the timeout, in milliseconds.
|
|
10
14
|
TIMEOUT_STATEMENT = 'SET statement_timeout TO %s'
|
|
15
|
+
|
|
16
|
+
# @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter]
|
|
17
|
+
# @return [String] the session setting with its unit, e.g. `"0"` or `"5s"`
|
|
18
|
+
def self.current_timeout(connection)
|
|
19
|
+
connection.select_value('SHOW statement_timeout')
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter]
|
|
23
|
+
# @param milliseconds [Integer]
|
|
24
|
+
# @return [void]
|
|
25
|
+
def self.apply_timeout(connection, milliseconds)
|
|
26
|
+
connection.execute(format(TIMEOUT_STATEMENT, connection.quote(Integer(milliseconds))))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter]
|
|
30
|
+
# @param value [String] a value {.current_timeout} returned
|
|
31
|
+
# @return [void]
|
|
32
|
+
def self.restore_timeout(connection, value)
|
|
33
|
+
connection.execute(format(TIMEOUT_STATEMENT, connection.quote(value.to_s)))
|
|
34
|
+
end
|
|
11
35
|
end
|
|
12
36
|
|
|
13
|
-
# MySQL settings.
|
|
37
|
+
# MySQL settings: `max_execution_time`, in milliseconds.
|
|
14
38
|
module MySQL
|
|
15
39
|
# SQL that sets the timeout, in milliseconds.
|
|
16
40
|
TIMEOUT_STATEMENT = 'SET max_execution_time = %s'
|
|
41
|
+
|
|
42
|
+
# @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter]
|
|
43
|
+
# @return [Integer] the session setting, in milliseconds
|
|
44
|
+
def self.current_timeout(connection)
|
|
45
|
+
Integer(connection.select_value('SELECT @@SESSION.max_execution_time'))
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter]
|
|
49
|
+
# @param milliseconds [Integer]
|
|
50
|
+
# @return [void]
|
|
51
|
+
def self.apply_timeout(connection, milliseconds)
|
|
52
|
+
connection.execute(format(TIMEOUT_STATEMENT, Integer(milliseconds)))
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter]
|
|
56
|
+
# @param value [Integer] a value {.current_timeout} returned
|
|
57
|
+
# @return [void]
|
|
58
|
+
def self.restore_timeout(connection, value)
|
|
59
|
+
apply_timeout(connection, value)
|
|
60
|
+
end
|
|
17
61
|
end
|
|
18
62
|
|
|
19
|
-
# MariaDB settings.
|
|
63
|
+
# MariaDB settings: `max_statement_time`, in seconds.
|
|
20
64
|
module MariaDB
|
|
21
65
|
# SQL that sets the timeout, in milliseconds: `max_statement_time` is in seconds.
|
|
22
66
|
TIMEOUT_STATEMENT = 'SET SESSION max_statement_time = %s / 1000'
|
|
67
|
+
# SQL that sets the timeout, in seconds.
|
|
68
|
+
RESTORE_STATEMENT = 'SET SESSION max_statement_time = %s'
|
|
69
|
+
|
|
70
|
+
# @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter]
|
|
71
|
+
# @return [Float] the session setting, in seconds
|
|
72
|
+
def self.current_timeout(connection)
|
|
73
|
+
Float(connection.select_value('SELECT @@SESSION.max_statement_time'))
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter]
|
|
77
|
+
# @param milliseconds [Integer]
|
|
78
|
+
# @return [void]
|
|
79
|
+
def self.apply_timeout(connection, milliseconds)
|
|
80
|
+
connection.execute(format(TIMEOUT_STATEMENT, Integer(milliseconds)))
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter]
|
|
84
|
+
# @param value [Float] a value {.current_timeout} returned, in seconds
|
|
85
|
+
# @return [void]
|
|
86
|
+
def self.restore_timeout(connection, value)
|
|
87
|
+
connection.execute(format(RESTORE_STATEMENT, format('%.6f', Float(value))))
|
|
88
|
+
end
|
|
23
89
|
end
|
|
24
90
|
|
|
25
91
|
# Engine modules by key.
|
|
@@ -13,7 +13,7 @@ module DeployPin
|
|
|
13
13
|
# @example Skip the test, or pick the framework for one task
|
|
14
14
|
# deploy_pin task backfill_users -g I --skip-test
|
|
15
15
|
# deploy_pin task backfill_users -g I --test-framework=rspec
|
|
16
|
-
class Task < Base
|
|
16
|
+
class Task < Base # rubocop:disable Metrics/ClassLength
|
|
17
17
|
# Supported test frameworks: the default directory for the tests (used
|
|
18
18
|
# when {DeployPin.tests_path} is not set) and the test file name, where
|
|
19
19
|
# `%<task>s` is the task file name without `.rb`.
|
|
@@ -21,6 +21,11 @@ module DeployPin
|
|
|
21
21
|
rspec: { path: 'spec/deploy_pin', file: '%<task>s_spec.rb' }
|
|
22
22
|
}.freeze
|
|
23
23
|
|
|
24
|
+
# The identifiers a task header accepts.
|
|
25
|
+
IDENTIFIER_FORMAT = /\A-?\d+\z/
|
|
26
|
+
# Format of one-off task identifiers: a timestamp.
|
|
27
|
+
TIMESTAMP_FORMAT = '%Y%m%d%H%M%S'
|
|
28
|
+
|
|
24
29
|
# Printed when no test framework is configured and none is detected.
|
|
25
30
|
UNDETECTED_TEST_FRAMEWORK = 'test for the task: no test framework detected. ' \
|
|
26
31
|
'Add rspec (rspec-rails in a Rails app) to the :development group, ' \
|
|
@@ -28,7 +33,8 @@ module DeployPin
|
|
|
28
33
|
|
|
29
34
|
class_option :parallel, type: :boolean, aliases: '-p', desc: 'Use the Parallel task template'
|
|
30
35
|
class_option :recurring, type: :boolean, aliases: '-r', desc: 'Run the task on every deployment'
|
|
31
|
-
class_option :identifier, aliases: '-i',
|
|
36
|
+
class_option :identifier, aliases: '-i',
|
|
37
|
+
desc: 'Identifier of a recurring task: an integer, required with --recurring'
|
|
32
38
|
argument :title, required: true, desc: 'Task name, e.g. backfill_user_names'
|
|
33
39
|
class_option :group, aliases: '-g', desc: 'Group of the task (default: DeployPin.fallback_group)'
|
|
34
40
|
class_option :author, aliases: '-a', desc: 'Author shown in the task title (default: $USER)'
|
|
@@ -59,7 +65,7 @@ module DeployPin
|
|
|
59
65
|
@author = options[:author] || ENV['USER']
|
|
60
66
|
@group = group
|
|
61
67
|
@recurring = options[:recurring]
|
|
62
|
-
@identifier = @recurring ? options[:identifier] :
|
|
68
|
+
@identifier = @recurring ? options[:identifier] : unused_identifier
|
|
63
69
|
template template_file, task_path
|
|
64
70
|
end
|
|
65
71
|
|
|
@@ -75,7 +81,8 @@ module DeployPin
|
|
|
75
81
|
|
|
76
82
|
# @return [void]
|
|
77
83
|
# @raise [Thor::Error] if the group is missing or not in {DeployPin.groups},
|
|
78
|
-
# or the test framework is
|
|
84
|
+
# a recurring task has no integer --identifier, or the test framework is
|
|
85
|
+
# not one of {TEST_FRAMEWORKS}
|
|
79
86
|
def validate
|
|
80
87
|
raise Thor::Error, set_color('Missing required option: --group', :red) if group.blank?
|
|
81
88
|
|
|
@@ -83,6 +90,7 @@ module DeployPin
|
|
|
83
90
|
raise Thor::Error, set_color("Group '#{group}' is not defined in DeployPin.groups", :red)
|
|
84
91
|
end
|
|
85
92
|
|
|
93
|
+
validate_recurring_identifier
|
|
86
94
|
validate_test_framework
|
|
87
95
|
end
|
|
88
96
|
|
|
@@ -137,6 +145,39 @@ module DeployPin
|
|
|
137
145
|
:rspec if defined?(::RSpec)
|
|
138
146
|
end
|
|
139
147
|
|
|
148
|
+
# The current timestamp, or the next second that no task in
|
|
149
|
+
# {DeployPin.tasks_path} uses: tasks generated within one second get
|
|
150
|
+
# different identifiers.
|
|
151
|
+
def unused_identifier
|
|
152
|
+
used = existing_identifiers
|
|
153
|
+
time = Time.now
|
|
154
|
+
time += 1 while used.include?(time.strftime(TIMESTAMP_FORMAT).to_i)
|
|
155
|
+
time.strftime(TIMESTAMP_FORMAT)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# Identifiers of the task files, from their names and their headers.
|
|
159
|
+
def existing_identifiers
|
|
160
|
+
Dir[File.join(File.expand_path(DeployPin.tasks_path.to_s, destination_root), '*.rb')].flat_map do |path|
|
|
161
|
+
task = DeployPin::Task.new(path)
|
|
162
|
+
task.parse
|
|
163
|
+
[File.basename(path)[/\A-?\d+/]&.to_i, task.identifier]
|
|
164
|
+
end.compact.to_set
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def validate_recurring_identifier
|
|
168
|
+
return unless options[:recurring]
|
|
169
|
+
|
|
170
|
+
identifier = options[:identifier].to_s
|
|
171
|
+
return if IDENTIFIER_FORMAT.match?(identifier)
|
|
172
|
+
|
|
173
|
+
message = if identifier.empty? || identifier == 'identifier'
|
|
174
|
+
'Recurring tasks need an identifier: --identifier <integer>, e.g. --identifier 5'
|
|
175
|
+
else
|
|
176
|
+
"Invalid identifier '#{identifier}': use an integer, e.g. --identifier 5"
|
|
177
|
+
end
|
|
178
|
+
raise Thor::Error, set_color(message, :red)
|
|
179
|
+
end
|
|
180
|
+
|
|
140
181
|
def validate_test_framework
|
|
141
182
|
framework = test_framework
|
|
142
183
|
return if !framework || TEST_FRAMEWORKS.key?(framework)
|
|
@@ -1,11 +1,22 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
# Upgrades a deploy_pins table created before DeployPin 1.7 (resumable tasks).
|
|
4
|
+
# On a table that already has the 1.7 columns it changes nothing.
|
|
3
5
|
class UpgradeDeployPins < ActiveRecord::Migration[7.0]
|
|
4
|
-
def
|
|
5
|
-
add_column :deploy_pins, :progress, :integer, default: 0,
|
|
6
|
-
|
|
6
|
+
def up
|
|
7
|
+
add_column :deploy_pins, :progress, :integer, default: 0 unless column_exists?(:deploy_pins, :progress)
|
|
8
|
+
return if column_exists?(:deploy_pins, :completed_at)
|
|
7
9
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
add_column :deploy_pins, :completed_at, :datetime, default: nil
|
|
11
|
+
# Before 1.7 a record was only created when its task completed.
|
|
12
|
+
execute "UPDATE #{quote_table_name(:deploy_pins)} SET completed_at = created_at"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# The columns may predate this migration (it changes nothing on a current
|
|
16
|
+
# table), and removing them would erase the progress and completion of the tasks.
|
|
17
|
+
def down
|
|
18
|
+
raise ActiveRecord::IrreversibleMigration,
|
|
19
|
+
'UpgradeDeployPins cannot tell whether it added the progress and completed_at columns. ' \
|
|
20
|
+
'Removing them would lose task progress and completion records; remove them by hand if you are sure.'
|
|
10
21
|
end
|
|
11
22
|
end
|
|
@@ -4,11 +4,12 @@ require 'deploy_pin/generators/base'
|
|
|
4
4
|
|
|
5
5
|
module DeployPin
|
|
6
6
|
module Generators
|
|
7
|
-
# `deploy_pin upgrade`: creates the migration that upgrades
|
|
8
|
-
#
|
|
7
|
+
# `deploy_pin upgrade`: creates the migration that upgrades a `deploy_pins`
|
|
8
|
+
# table created before DeployPin 1.7 for resumable tasks. The migration
|
|
9
|
+
# changes nothing on a table that already has the 1.7 columns.
|
|
9
10
|
class Upgrade < Base
|
|
10
11
|
templates 'upgrade'
|
|
11
|
-
desc 'Creates the migration that upgrades
|
|
12
|
+
desc 'Creates the migration that upgrades a deploy_pins table created before DeployPin 1.7'
|
|
12
13
|
|
|
13
14
|
class_option :migrations_path, type: :string, default: 'db/migrate',
|
|
14
15
|
desc: 'Directory of the ActiveRecord migrations'
|
|
@@ -7,7 +7,9 @@ module DeployPin
|
|
|
7
7
|
#
|
|
8
8
|
# The `timeout:` option sets the timeout; it defaults to
|
|
9
9
|
# {DeployPin.statement_timeout}. Pass an array to give
|
|
10
|
-
# {Database.execute_with_timeout} options too.
|
|
10
|
+
# {Database.execute_with_timeout} options too. The other options
|
|
11
|
+
# (`in_processes:`, `in_threads:`, `progress:`...) go to Parallel, in the
|
|
12
|
+
# same hash or in a separate one.
|
|
11
13
|
#
|
|
12
14
|
# @example
|
|
13
15
|
# parallel_each(1..2, in_processes: 2, timeout: 0.3.seconds) do |i|
|
|
@@ -19,6 +21,11 @@ module DeployPin
|
|
|
19
21
|
# parallel_each(1..2, in_processes: 2, timeout: [0.3.seconds, { connected_to: { role: :reading } }]) do |i|
|
|
20
22
|
# ActiveRecord::Base.connection.execute("<some db query>")
|
|
21
23
|
# end
|
|
24
|
+
#
|
|
25
|
+
# @example Parallel options and the timeout in separate hashes
|
|
26
|
+
# parallel_each(1..2, { in_threads: 2 }, { timeout: 0.3.seconds }) do |i|
|
|
27
|
+
# ActiveRecord::Base.connection.execute("<some db query>")
|
|
28
|
+
# end
|
|
22
29
|
module ParallelWrapper
|
|
23
30
|
# Prefix that maps a method to its Parallel counterpart.
|
|
24
31
|
PARALLEL_PREFIX = 'parallel_'
|
|
@@ -27,19 +34,14 @@ module DeployPin
|
|
|
27
34
|
# :reek:TooManyInstanceVariables
|
|
28
35
|
class ParallelRunner
|
|
29
36
|
# @param method_name [Symbol] the `parallel_*` method that was called
|
|
30
|
-
# @param args [Array] arguments for the Parallel method;
|
|
31
|
-
#
|
|
37
|
+
# @param args [Array] arguments for the Parallel method; the `:timeout`
|
|
38
|
+
# of a trailing hash is used for the timeout, and its other options go
|
|
39
|
+
# to Parallel. The arguments are not changed.
|
|
32
40
|
# @yield the block to run for each item
|
|
33
41
|
def initialize(method_name, *args, &db_block)
|
|
34
42
|
@method_name = method_name
|
|
35
43
|
@db_block = db_block
|
|
36
|
-
|
|
37
|
-
if args.last.is_a?(Hash) && args.last.key?(:timeout)
|
|
38
|
-
@timeout_args = args.pop
|
|
39
|
-
prepare_timeout_args
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
@parallel_args = args
|
|
44
|
+
@parallel_args = extract_timeout(args)
|
|
43
45
|
end
|
|
44
46
|
|
|
45
47
|
# @return [Object] the result of the Parallel method
|
|
@@ -64,14 +66,16 @@ module DeployPin
|
|
|
64
66
|
end
|
|
65
67
|
end
|
|
66
68
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
69
|
+
# @return [Array] the arguments for Parallel: the trailing hash without
|
|
70
|
+
# `:timeout`, left out when nothing else is in it
|
|
71
|
+
def extract_timeout(args)
|
|
72
|
+
options = args.last
|
|
73
|
+
return args unless options.is_a?(Hash) && options.key?(:timeout)
|
|
74
|
+
|
|
75
|
+
timeout = options[:timeout]
|
|
76
|
+
@timeout, @timeout_params = timeout.is_a?(Array) ? timeout : [timeout]
|
|
77
|
+
parallel_options = options.except(:timeout)
|
|
78
|
+
parallel_options.empty? ? args[0...-1] : [*args[0...-1], parallel_options]
|
|
75
79
|
end
|
|
76
80
|
|
|
77
81
|
def timeout
|
data/lib/deploy_pin/task.rb
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'ripper'
|
|
4
|
+
|
|
3
5
|
module DeployPin
|
|
4
6
|
# A task loaded from a file in {DeployPin.tasks_path}.
|
|
5
7
|
#
|
|
@@ -133,34 +135,36 @@ module DeployPin
|
|
|
133
135
|
!explicit_timeout? && !parallel?
|
|
134
136
|
end
|
|
135
137
|
|
|
138
|
+
# The `# <identifier>:<group>[:recurring]` header line. Like DeployPin
|
|
139
|
+
# 2.0.0, it matches the start of the line, so `# 3:I:` is task 3 of group I.
|
|
140
|
+
IDENTIFIER_LINE = /\A#\s*(-?\d+):(\w+):?(recurring)?/
|
|
141
|
+
# The `# task_title:` header line.
|
|
142
|
+
TITLE_LINE = /\A#\s*task_title:(.*)\z/
|
|
143
|
+
# The `# affected_areas:` header line.
|
|
144
|
+
AFFECTED_AREAS_LINE = /\A#\s*affected_areas:(.*)\z/
|
|
145
|
+
|
|
136
146
|
# Reads the header and the code from {#file}.
|
|
137
147
|
#
|
|
148
|
+
# The header is the comment block at the top of the file, up to the
|
|
149
|
+
# code delimiter (`# === task code goes down here ===`) when there is one.
|
|
150
|
+
# Metadata is read from the header only; everything after it is the
|
|
151
|
+
# {#script}, kept as written, comments and blank lines included.
|
|
152
|
+
#
|
|
138
153
|
# @return [void]
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
case line.strip
|
|
143
|
-
when /\A# (-?\d+):(\w+):?(recurring)?/
|
|
144
|
-
@identifier = Regexp.last_match(1).to_i
|
|
145
|
-
@group = Regexp.last_match(2)
|
|
146
|
-
@recurring = Regexp.last_match(3)
|
|
147
|
-
when /\A# task_title:(.+)/
|
|
148
|
-
@title = Regexp.last_match(1).strip
|
|
149
|
-
when /\A# affected_areas:(.+)/
|
|
150
|
-
@affected_areas = Regexp.last_match(1).strip
|
|
151
|
-
when /\A[^#].*/
|
|
152
|
-
@script += line
|
|
154
|
+
def parse
|
|
155
|
+
lines = each_line.to_a
|
|
156
|
+
header_size = header_length(lines)
|
|
153
157
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
end
|
|
158
|
+
lines.first(header_size).each { |line| parse_header_line(line.strip) }
|
|
159
|
+
@script = lines.drop(header_size).join
|
|
160
|
+
detect_calls
|
|
158
161
|
end
|
|
159
|
-
# :reek:TooManyStatements
|
|
160
162
|
|
|
161
163
|
# @yieldparam line [String] each line of the task source
|
|
162
|
-
# @return [void]
|
|
164
|
+
# @return [void, Enumerator] an enumerator when no block is given
|
|
163
165
|
def each_line(&block)
|
|
166
|
+
return enum_for(:each_line) unless block
|
|
167
|
+
|
|
164
168
|
if file.starts_with?('# no_file_task')
|
|
165
169
|
file.each_line(&block)
|
|
166
170
|
else
|
|
@@ -179,12 +183,34 @@ module DeployPin
|
|
|
179
183
|
end
|
|
180
184
|
|
|
181
185
|
# Whether another task has the same code but a different identifier.
|
|
186
|
+
# The code is compared without its standalone comments and blank lines
|
|
187
|
+
# (see {#executable_source}).
|
|
182
188
|
#
|
|
183
189
|
# @param other [Task]
|
|
184
190
|
# @return [Boolean]
|
|
185
191
|
def eql?(other)
|
|
186
192
|
# same script & different identifier
|
|
187
|
-
|
|
193
|
+
executable_source == other.executable_source && identifier != other.identifier
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# Ruby tokens that are neither code nor part of a literal.
|
|
197
|
+
NON_CODE_TOKENS = %i[on_sp on_nl on_ignored_nl on_comment on_embdoc_beg on_embdoc on_embdoc_end].freeze
|
|
198
|
+
|
|
199
|
+
# The {#script} without the lines that hold only comments or whitespace
|
|
200
|
+
# outside literals: what {#eql?} compares. Lines inside heredocs and other
|
|
201
|
+
# multi-line literals are kept, even blank or comment-like ones.
|
|
202
|
+
#
|
|
203
|
+
# @return [String]
|
|
204
|
+
def executable_source
|
|
205
|
+
@executable_source ||= begin
|
|
206
|
+
lines = script.lines
|
|
207
|
+
code_lines = Ripper.lex(script).each_with_object(Set.new) do |((line, _), type, token), covered|
|
|
208
|
+
next if NON_CODE_TOKENS.include?(type)
|
|
209
|
+
|
|
210
|
+
covered.merge(line..(line + token.chomp.count("\n")))
|
|
211
|
+
end
|
|
212
|
+
lines.select.with_index(1) { |_, number| code_lines.include?(number) }.join.strip
|
|
213
|
+
end
|
|
188
214
|
end
|
|
189
215
|
|
|
190
216
|
# @return [Integer] a timestamp a year ahead, used to sort negative identifiers last
|
|
@@ -223,6 +249,106 @@ module DeployPin
|
|
|
223
249
|
def parallel?
|
|
224
250
|
@parallel
|
|
225
251
|
end
|
|
252
|
+
|
|
253
|
+
private
|
|
254
|
+
|
|
255
|
+
# Number of lines in the header: the leading comments and blank lines,
|
|
256
|
+
# ending at the code delimiter if there is one, and the blank lines
|
|
257
|
+
# right after the delimiter.
|
|
258
|
+
def header_length(lines)
|
|
259
|
+
comments = lines.index { |line| !header_line?(line) } || lines.size
|
|
260
|
+
delimiter = lines.first(comments).index { |line| delimiter?(line) }
|
|
261
|
+
return comments unless delimiter
|
|
262
|
+
|
|
263
|
+
blank = lines.drop(delimiter + 1).index { |line| !line.strip.empty? } || (lines.size - delimiter - 1)
|
|
264
|
+
delimiter + 1 + blank
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def header_line?(line)
|
|
268
|
+
stripped = line.strip
|
|
269
|
+
stripped.empty? || stripped.start_with?('#')
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
# The line that ends the header in generated task files, e.g.
|
|
273
|
+
# `# === task code goes down here ===`: a comment of a run of `=`, any
|
|
274
|
+
# text without `=`, then another run of `=`. Checked without a regular
|
|
275
|
+
# expression, in linear time.
|
|
276
|
+
def delimiter?(line)
|
|
277
|
+
text = line.strip
|
|
278
|
+
return false unless text.start_with?('#')
|
|
279
|
+
|
|
280
|
+
body = text.delete_prefix('#').lstrip
|
|
281
|
+
head = leading_equals(body)
|
|
282
|
+
return false if head.zero?
|
|
283
|
+
return body.length >= 2 if head == body.length
|
|
284
|
+
|
|
285
|
+
tail = leading_equals(body.reverse)
|
|
286
|
+
tail.positive? && !body[head...-tail].include?('=')
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
def leading_equals(text)
|
|
290
|
+
text.each_char.take_while { |char| char == '=' }.size
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
# :reek:TooManyStatements
|
|
294
|
+
def parse_header_line(line)
|
|
295
|
+
case line
|
|
296
|
+
when IDENTIFIER_LINE
|
|
297
|
+
return if @identifier # the first identifier line is the header's
|
|
298
|
+
|
|
299
|
+
@identifier = Regexp.last_match(1).to_i
|
|
300
|
+
@group = Regexp.last_match(2)
|
|
301
|
+
@recurring = Regexp.last_match(3)
|
|
302
|
+
when TITLE_LINE
|
|
303
|
+
@title = Regexp.last_match(1).strip
|
|
304
|
+
when AFFECTED_AREAS_LINE
|
|
305
|
+
@affected_areas = Regexp.last_match(1).strip
|
|
306
|
+
end
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# Looks for real calls in the code, not in comments, strings, symbols,
|
|
310
|
+
# assignments or constant references.
|
|
311
|
+
def detect_calls
|
|
312
|
+
calls = []
|
|
313
|
+
collect_calls(Ripper.sexp(script), calls)
|
|
314
|
+
|
|
315
|
+
@explicit_timeout = calls.any? do |receiver, name|
|
|
316
|
+
name == 'execute_with_timeout' && database_receiver?(receiver)
|
|
317
|
+
end
|
|
318
|
+
@parallel = calls.any? { |receiver, name| parallel_receiver?(receiver) || parallel_method?(name) }
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
# Collects `[receiver, method name]` of each method call in a Ripper tree.
|
|
322
|
+
# :reek:UtilityFunction
|
|
323
|
+
def collect_calls(node, calls)
|
|
324
|
+
return unless node.is_a?(Array)
|
|
325
|
+
|
|
326
|
+
case node
|
|
327
|
+
in [:call | :command_call, receiver, _, [_, String => name, *], *] then calls << [receiver, name]
|
|
328
|
+
in [:fcall | :command | :vcall, [_, String => name, *], *] then calls << [nil, name]
|
|
329
|
+
else nil
|
|
330
|
+
end
|
|
331
|
+
node.each { |child| collect_calls(child, calls) }
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
# `Database`, `DeployPin::Database` or `::DeployPin::Database`.
|
|
335
|
+
# :reek:UtilityFunction
|
|
336
|
+
def database_receiver?(receiver)
|
|
337
|
+
case receiver
|
|
338
|
+
in [:var_ref, [:@const, 'Database', *]] then true
|
|
339
|
+
in [:const_path_ref, [:var_ref | :top_const_ref, [:@const, 'DeployPin', *]], [:@const, 'Database', *]] then true
|
|
340
|
+
else false
|
|
341
|
+
end
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
# :reek:UtilityFunction
|
|
345
|
+
def parallel_receiver?(receiver)
|
|
346
|
+
receiver in [:var_ref | :top_const_ref, [:@const, 'Parallel', *]]
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
def parallel_method?(name)
|
|
350
|
+
name.start_with?(ParallelWrapper::PARALLEL_PREFIX) && respond_to?(name, true)
|
|
351
|
+
end
|
|
226
352
|
end
|
|
227
353
|
# :reek:TooManyMethods
|
|
228
354
|
end
|
data/lib/deploy_pin/version.rb
CHANGED
data/lib/deploy_pin.rb
CHANGED
|
@@ -197,7 +197,8 @@ module DeployPin
|
|
|
197
197
|
TASKS_FILE = File.expand_path('tasks/deploy_pin_tasks.rake', __dir__)
|
|
198
198
|
|
|
199
199
|
# Defines the `deploy_pin:*` rake tasks. Rails apps get them from {Engine};
|
|
200
|
-
# other apps call this from their Rakefile.
|
|
200
|
+
# other apps call this from their Rakefile. The tasks are defined once per
|
|
201
|
+
# Rake application: calling it again does not add their actions twice.
|
|
201
202
|
#
|
|
202
203
|
# The tasks first run the app's `environment` task when it is defined (load
|
|
203
204
|
# the app and connect to the database there), else {load_config}.
|
|
@@ -215,6 +216,24 @@ module DeployPin
|
|
|
215
216
|
load TASKS_FILE
|
|
216
217
|
end
|
|
217
218
|
|
|
219
|
+
# Rake applications that have the `deploy_pin:*` tasks.
|
|
220
|
+
RAKE_APPLICATIONS = ObjectSpace::WeakMap.new
|
|
221
|
+
private_constant :RAKE_APPLICATIONS
|
|
222
|
+
|
|
223
|
+
# Records that the `deploy_pin:*` tasks are being defined in a Rake
|
|
224
|
+
# application. Called by {TASKS_FILE}, which {load_tasks} and the Rails
|
|
225
|
+
# engine load.
|
|
226
|
+
#
|
|
227
|
+
# @api private
|
|
228
|
+
# @param application [Rake::Application]
|
|
229
|
+
# @return [Boolean] false if the application has the tasks already
|
|
230
|
+
def self.register_rake_tasks(application = Rake.application)
|
|
231
|
+
# Rake::Task.clear removes them: define them again then
|
|
232
|
+
return false if RAKE_APPLICATIONS.key?(application) && application.lookup('deploy_pin:run')
|
|
233
|
+
|
|
234
|
+
RAKE_APPLICATIONS[application] = true
|
|
235
|
+
end
|
|
236
|
+
|
|
218
237
|
# Loads the configuration: the given files, or else the first of
|
|
219
238
|
# {CONFIG_FILES} that exists in `root`.
|
|
220
239
|
#
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
Description:
|
|
2
|
-
Upgrades
|
|
2
|
+
Upgrades a deploy_pins table created before DeployPin 1.7: adds the
|
|
3
|
+
progress and completed_at columns for resumable tasks, and marks the
|
|
4
|
+
existing records as completed. On a table that already has those columns
|
|
5
|
+
(created by DeployPin 1.7 or later) the migration changes nothing. It
|
|
6
|
+
cannot be rolled back, so a rollback never removes task state.
|
|
7
|
+
Outside Rails, run `deploy_pin upgrade`.
|
|
3
8
|
|
|
4
9
|
Example:
|
|
5
10
|
rails generate deploy_pin:upgrade
|
|
@@ -5,7 +5,8 @@ require 'rails/generators/migration'
|
|
|
5
5
|
require 'deploy_pin/generators/base'
|
|
6
6
|
|
|
7
7
|
module DeployPin
|
|
8
|
-
#
|
|
8
|
+
# `rails g deploy_pin:upgrade`: creates the migration that upgrades a
|
|
9
|
+
# `deploy_pins` table created before DeployPin 1.7 for resumable tasks.
|
|
9
10
|
class UpgradeGenerator < Rails::Generators::Base
|
|
10
11
|
include Rails::Generators::Migration
|
|
11
12
|
|
|
@@ -13,7 +14,7 @@ module DeployPin
|
|
|
13
14
|
# re-runs behave like other Rails generators. The templates are shared with
|
|
14
15
|
# {Generators::Upgrade}, which `deploy_pin upgrade` runs outside Rails.
|
|
15
16
|
source_root File.join(DeployPin::Generators::Base::TEMPLATES_ROOT, 'upgrade')
|
|
16
|
-
desc '
|
|
17
|
+
desc File.read(File.expand_path('USAGE', __dir__))
|
|
17
18
|
|
|
18
19
|
# @param path [String] migrations directory
|
|
19
20
|
# @return [String] the next migration timestamp
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
# Loading this file again (DeployPin.load_tasks or Rails.application.load_tasks
|
|
4
|
+
# twice) must not add the actions twice: a recurring task would run twice.
|
|
5
|
+
return unless DeployPin.register_rake_tasks(Rake.application)
|
|
6
|
+
|
|
3
7
|
namespace :deploy_pin do
|
|
4
8
|
# The groups and task identifiers of the task arguments; all groups only when
|
|
5
9
|
# no argument is given. An explicit empty selection (`invoke([])`) selects
|