Skip to content

Commit 171f423

Browse files
committed
(#920) Download envelope and graph dumps using S3 client
1 parent 4061cef commit 171f423

5 files changed

Lines changed: 34 additions & 21 deletions

File tree

app/services/envelope_dumps/base.rb

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ module EnvelopeDumps
33
class Base # rubocop:todo Metrics/ClassLength
44
attr_reader :envelope_download, :last_dumped_at
55

6-
delegate :envelope_community, to: :envelope_download
6+
delegate :envelope_community, :url, to: :envelope_download
77

88
def initialize(envelope_download, last_dumped_at)
99
@envelope_download = envelope_download
1010
@last_dumped_at = last_dumped_at
1111
end
1212

1313
def bucket
14+
@bucket ||= Aws::S3::Resource
15+
.new(region: ENV.fetch('AWS_REGION'))
16+
.bucket(bucket_name)
17+
end
18+
19+
def bucket_name
1420
raise NotImplementedError
1521
end
1622

@@ -40,15 +46,12 @@ def dirname
4046
end
4147

4248
def download_file # rubocop:todo Metrics/AbcSize
43-
return unless envelope_download.url?
44-
45-
log("Downloading the existing dump from #{envelope_download.url}")
49+
return unless url.present?
4650

47-
File.open(filename, 'wb') do |file|
48-
URI.parse(envelope_download.url).open do |data|
49-
file.write(data.read)
50-
end
51-
end
51+
log("Downloading the existing dump from #{url}")
52+
previous_filename = url.split('/').last
53+
object = bucket.object(previous_filename)
54+
object.get(response_target: filename)
5255

5356
log("Unarchiving the downloaded dump into #{dirname}")
5457
system("unzip -qq #{filename} -d #{dirname}", exception: true)
@@ -83,10 +86,6 @@ def published_envelopes
8386
end
8487
end
8588

86-
def region
87-
ENV.fetch('AWS_REGION')
88-
end
89-
9089
def remove_entries
9190
log('Removing recently deleted envelopes from the dump')
9291

@@ -116,7 +115,7 @@ def run
116115
end
117116

118117
def up_to_date?
119-
envelope_download.url? && published_envelopes.none? && destroy_envelope_events.none?
118+
url.present? && published_envelopes.none? && destroy_envelope_events.none?
120119
end
121120

122121
def upload_file
@@ -129,7 +128,7 @@ def upload_file
129128

130129
log('Uploading the updated dump to S3.')
131130

132-
object = Aws::S3::Resource.new(region:).bucket(bucket).object(filename)
131+
object = bucket.object(filename)
133132
object.upload_file(filename)
134133
object.public_url
135134
end

app/services/envelope_dumps/envelope_builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module EnvelopeDumps
44
class EnvelopeBuilder < Base # rubocop:todo Style/Documentation
5-
def bucket
5+
def bucket_name
66
ENV.fetch('ENVELOPE_DOWNLOADS_BUCKET')
77
end
88

app/services/envelope_dumps/graph_builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module EnvelopeDumps
44
class GraphBuilder < Base # rubocop:todo Style/Documentation
5-
def bucket
5+
def bucket_name
66
ENV.fetch('ENVELOPE_GRAPHS_BUCKET')
77
end
88

spec/services/envelope_dumps/envelope_builder_spec.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@
115115
# rubocop:todo RSpec/NestedGroups
116116
context 'with previous download' do # rubocop:todo RSpec/MultipleMemoizedHelpers, RSpec/NestedGroups
117117
# rubocop:enable RSpec/NestedGroups
118+
let(:previous_key) { Faker::Lorem.characters.first(32) }
119+
118120
let(:dump) do
119121
buffer = StringIO.new
120122

@@ -133,7 +135,7 @@
133135
:envelope_download,
134136
envelope_community:,
135137
started_at: now + 1.second,
136-
url: Faker::Internet.url
138+
url: "#{Faker::Internet.url}/#{previous_key}"
137139
)
138140
end
139141

@@ -144,13 +146,17 @@
144146
before do
145147
PaperTrail.enabled = true
146148

149+
allow(bucket).to receive(:object).with(previous_key).and_return(s3_object)
150+
151+
allow(s3_object).to receive(:get).with(response_target: key) do
152+
File.write(key, dump)
153+
end
154+
147155
envelope2.update_column(:updated_at, envelope_download.started_at)
148156

149157
travel_to envelope_download.started_at do
150158
envelope3.destroy
151159
end
152-
153-
stub_request(:get, envelope_download.url).to_return(body: dump)
154160
end
155161

156162
after do

spec/services/envelope_dumps/graph_builder_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888

8989
# rubocop:todo RSpec/NestedGroups
9090
context 'with previous download' do # rubocop:todo RSpec/MultipleMemoizedHelpers, RSpec/NestedGroups
91+
let(:previous_key) { Faker::Lorem.characters.first(32) }
92+
9193
# rubocop:enable RSpec/NestedGroups
9294
let(:dump) do
9395
buffer = StringIO.new
@@ -107,7 +109,7 @@
107109
:envelope_download,
108110
envelope_community:,
109111
started_at: now + 1.second,
110-
url: Faker::Internet.url
112+
url: "#{Faker::Internet.url}/#{previous_key}"
111113
)
112114
end
113115

@@ -118,6 +120,12 @@
118120
before do
119121
PaperTrail.enabled = true
120122

123+
allow(bucket).to receive(:object).with(previous_key).and_return(s3_object)
124+
125+
allow(s3_object).to receive(:get).with(response_target: key) do
126+
File.write(key, dump)
127+
end
128+
121129
envelope2.update_column(:updated_at, envelope_download.started_at)
122130

123131
travel_to envelope_download.started_at do

0 commit comments

Comments
 (0)