Skip to content

Commit 1967459

Browse files
Richie Thomasgeoffharcourt
authored andcommitted
Replace automatic parallelization of backup jobs with opt-in
1 parent 37ca4cb commit 1967459

File tree

5 files changed

+131
-13
lines changed

5 files changed

+131
-13
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ with `heroku ______ --remote staging` or `heroku ______ --remote production`:
104104
watch production ps
105105
staging open
106106

107+
You can optionally parallelize a DB restore by passing `--parallelize`
108+
as a flag to the `development` or `production` commands:
109+
```
110+
development restore-from production --parallelize
111+
```
112+
107113
[2]: http://redis.io/commands
108114

109115
Convention

lib/parity/backup.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Backup
1010
def initialize(args)
1111
@from, @to = args.values_at(:from, :to)
1212
@additional_args = args[:additional_args] || BLANK_ARGUMENTS
13+
@parallelize = args[:parallelize] || false
1314
end
1415

1516
def restore
@@ -24,7 +25,9 @@ def restore
2425

2526
private
2627

27-
attr_reader :additional_args, :from, :to
28+
attr_reader :additional_args, :from, :to, :parallelize
29+
30+
alias :parallelize? :parallelize
2831

2932
def restore_from_development
3033
reset_remote_database
@@ -115,10 +118,10 @@ def database_yaml_file
115118
end
116119

117120
def processor_cores
118-
if ruby_version_over_2_2?
121+
if parallelize? && ruby_version_over_2_2?
119122
Etc.nprocessors
120123
else
121-
2
124+
1
122125
end
123126
end
124127

lib/parity/environment.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def restore
5757
Backup.new(
5858
from: arguments.first,
5959
to: environment,
60+
parallelize: parallelize?,
6061
additional_args: additional_restore_arguments,
6162
).restore
6263
end
@@ -72,10 +73,13 @@ def forced?
7273
arguments.include?("--force")
7374
end
7475

76+
def parallelize?
77+
arguments.include?("--parallelize")
78+
end
79+
7580
def additional_restore_arguments
76-
(arguments.drop(1) - ["--force"] + [restore_confirmation_argument]).
77-
compact.
78-
join(" ")
81+
(arguments.drop(1) - ["--force", "--parallelize"] +
82+
[restore_confirmation_argument]).compact.join(" ")
7983
end
8084

8185
def restore_confirmation_argument

spec/parity/backup_spec.rb

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
allow(Kernel).to receive(:system)
88
allow(Etc).to receive(:nprocessors).and_return(number_of_processes)
99

10-
Parity::Backup.new(from: "production", to: "development").restore
10+
Parity::Backup.new(
11+
from: "production",
12+
to: "development",
13+
parallelize: true,
14+
).restore
1115

1216
expect(Kernel).
1317
to have_received(:system).
@@ -31,7 +35,11 @@
3135
allow(Kernel).to receive(:system)
3236
allow(Etc).to receive(:respond_to?).with(:nprocessors).and_return(false)
3337

34-
Parity::Backup.new(from: "production", to: "development").restore
38+
Parity::Backup.new(
39+
from: "production",
40+
to: "development",
41+
parallelize: false,
42+
).restore
3543

3644
expect(Kernel).
3745
to have_received(:system).
@@ -44,7 +52,64 @@
4452
with(drop_development_database_drop_command)
4553
expect(Kernel).
4654
to have_received(:system).
47-
with(restore_from_local_temp_backup_command(cores: 2))
55+
with(restore_from_local_temp_backup_command(cores: 1))
56+
expect(Kernel).
57+
to have_received(:system).
58+
with(delete_local_temp_backup_command)
59+
end
60+
61+
it "restores backups in parallel when the right flag is set" do
62+
allow(IO).to receive(:read).and_return(database_fixture)
63+
allow(Kernel).to receive(:system)
64+
allow(Etc).to receive(:nprocessors).and_return(12)
65+
66+
Parity::Backup.new(
67+
from: "production",
68+
to: "development",
69+
parallelize: true,
70+
).restore
71+
72+
expect(Kernel).
73+
to have_received(:system).
74+
with(make_temp_directory_command)
75+
expect(Kernel).
76+
to have_received(:system).
77+
with(download_remote_database_command)
78+
expect(Kernel).
79+
to have_received(:system).
80+
with(drop_development_database_drop_command)
81+
expect(Kernel).
82+
to have_received(:system).
83+
with(restore_from_local_temp_backup_command(cores: 12))
84+
expect(Kernel).
85+
to have_received(:system).
86+
with(delete_local_temp_backup_command)
87+
end
88+
89+
it "does not restore backups in parallel when the right flag is set" +
90+
"but the ruby version is under 2.2" do
91+
allow(IO).to receive(:read).and_return(database_fixture)
92+
allow(Kernel).to receive(:system)
93+
allow(Etc).to receive(:respond_to?).with(:nprocessors).and_return(false)
94+
95+
Parity::Backup.new(
96+
from: "production",
97+
to: "development",
98+
parallelize: true,
99+
).restore
100+
101+
expect(Kernel).
102+
to have_received(:system).
103+
with(make_temp_directory_command)
104+
expect(Kernel).
105+
to have_received(:system).
106+
with(download_remote_database_command)
107+
expect(Kernel).
108+
to have_received(:system).
109+
with(drop_development_database_drop_command)
110+
expect(Kernel).
111+
to have_received(:system).
112+
with(restore_from_local_temp_backup_command(cores: 1))
48113
expect(Kernel).
49114
to have_received(:system).
50115
with(delete_local_temp_backup_command)
@@ -55,7 +120,11 @@
55120
allow(Kernel).to receive(:system)
56121
allow(Etc).to receive(:nprocessors).and_return(number_of_processes)
57122

58-
Parity::Backup.new(from: "production", to: "development").restore
123+
Parity::Backup.new(
124+
from: "production",
125+
to: "development",
126+
parallelize: true,
127+
).restore
59128

60129
expect(Kernel).
61130
to have_received(:system).

spec/parity/environment_spec.rb

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@
66
allow(Kernel).to receive(:system).and_return(true)
77
end
88

9+
it "restores in parallel when passed the --parallelize flag" do
10+
backup = stub_parity_backup
11+
allow(Parity::Backup).to receive(:new).and_return(backup)
12+
13+
Parity::Environment.new("development",
14+
["restore", "staging", "--parallelize"]).run
15+
16+
expect(Parity::Backup).to have_received(:new).
17+
with(
18+
from: "staging",
19+
to: "development",
20+
parallelize: true,
21+
additional_args: "",
22+
)
23+
expect(backup).to have_received(:restore)
24+
end
25+
926
it "passes through arguments with correct quoting" do
1027
Parity::Environment.new(
1128
"production",
@@ -54,6 +71,7 @@
5471
with(
5572
from: "production",
5673
to: "staging",
74+
parallelize: false,
5775
additional_args: "--confirm parity-integration-staging",
5876
)
5977
expect(backup).to have_received(:restore)
@@ -71,6 +89,7 @@
7189
with(
7290
from: "production",
7391
to: "staging",
92+
parallelize: false,
7493
additional_args: "--confirm parity-staging",
7594
)
7695
expect(backup).to have_received(:restore)
@@ -88,6 +107,7 @@
88107
with(
89108
from: "production",
90109
to: "staging",
110+
parallelize: false,
91111
additional_args: "--confirm parity-staging",
92112
)
93113
expect(backup).to have_received(:restore)
@@ -104,6 +124,7 @@
104124
with(
105125
from: "production",
106126
to: "staging",
127+
parallelize: false,
107128
additional_args: "--confirm parity-staging",
108129
)
109130
expect(backup).to have_received(:restore)
@@ -116,7 +137,12 @@
116137
Parity::Environment.new("development", ["restore", "production"]).run
117138

118139
expect(Parity::Backup).to have_received(:new).
119-
with(from: "production", to: "development", additional_args: "")
140+
with(
141+
from: "production",
142+
to: "development",
143+
parallelize: false,
144+
additional_args: "",
145+
)
120146
expect(backup).to have_received(:restore)
121147
end
122148

@@ -127,7 +153,12 @@
127153
Parity::Environment.new("development", ["restore", "staging"]).run
128154

129155
expect(Parity::Backup).to have_received(:new).
130-
with(from: "staging", to: "development", additional_args: "")
156+
with(
157+
from: "staging",
158+
to: "development",
159+
parallelize: false,
160+
additional_args: "",
161+
)
131162
expect(backup).to have_received(:restore)
132163
end
133164

@@ -152,7 +183,12 @@
152183
Parity::Environment.new("production", ["restore", "staging", "--force"]).run
153184

154185
expect(Parity::Backup).to have_received(:new).
155-
with(from: "staging", to: "production", additional_args: "")
186+
with(
187+
from: "staging",
188+
to: "production",
189+
parallelize: false,
190+
additional_args: "",
191+
)
156192
expect(backup).to have_received(:restore)
157193
end
158194

0 commit comments

Comments
 (0)