Skip to content

Commit 16a235d

Browse files
committed
(#860) Track time of envelope download job enqueuing
1 parent 002d216 commit 16a235d

File tree

6 files changed

+67
-13
lines changed

6 files changed

+67
-13
lines changed

app/api/entities/envelope_download.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ class EnvelopeDownload < Grape::Entity
55
expose :display_status, as: :status,
66
documentation: { type: 'string', desc: 'Status of download' }
77

8+
expose :enqueued_at,
9+
documentation: { type: 'string', desc: 'When the download was enqueued' },
10+
if: ->(object) { object.pending? }
11+
12+
expose :finished_at,
13+
documentation: { type: 'string', desc: 'When the download finished' },
14+
if: ->(object) { object.finished? }
15+
16+
expose :started_at,
17+
documentation: { type: 'string', desc: 'When the download started' },
18+
if: ->(object) { object.in_progress? }
19+
820
expose :url,
921
documentation: { type: 'string', desc: 'AWS S3 URL' },
1022
if: ->(object) { object.finished? }

app/api/v1/envelopes.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ class Envelopes < MountableAPI
8484

8585
desc 'Starts an envelope download'
8686
post do
87-
@envelope_download.update_column(:status, :pending)
87+
@envelope_download.update!(
88+
enqueued_at: Time.current,
89+
status: :pending
90+
)
91+
8892
DownloadEnvelopesJob.perform_later(@envelope_download.id)
8993
present @envelope_download, with: API::Entities::EnvelopeDownload
9094
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddEnqueuedAtToEnvelopeDownloads < ActiveRecord::Migration[8.0]
2+
def change
3+
add_column :envelope_downloads, :enqueued_at, :datetime
4+
end
5+
end

db/structure.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ CREATE TABLE public.envelope_downloads (
325325
url character varying,
326326
created_at timestamp(6) without time zone NOT NULL,
327327
updated_at timestamp(6) without time zone NOT NULL,
328-
status character varying DEFAULT 'pending'::character varying NOT NULL
328+
status character varying DEFAULT 'pending'::character varying NOT NULL,
329+
enqueued_at timestamp(6) without time zone
329330
);
330331

331332

@@ -1888,6 +1889,7 @@ ALTER TABLE ONLY public.envelopes
18881889
SET search_path TO "$user", public;
18891890

18901891
INSERT INTO "schema_migrations" (version) VALUES
1892+
('20250925025616'),
18911893
('20250922224518'),
18921894
('20250921174021'),
18931895
('20250902034147'),

spec/api/v1/envelopes_spec.rb

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,17 @@
180180
envelope_download = EnvelopeDownload.last
181181
expect(envelope_download.envelope_community).to eq(envelope_community)
182182
expect(envelope_download.status).to eq('pending')
183+
184+
expect_json_sizes(2)
185+
expect_json('enqueued_at', nil)
186+
expect_json('status', 'pending')
183187
end
184188
end
185189

186190
# rubocop:todo RSpec/NestedGroups
187191
context 'with envelope download' do # rubocop:todo RSpec/MultipleMemoizedHelpers, RSpec/NestedGroups
188192
# rubocop:enable RSpec/NestedGroups
189-
before do
193+
let!(:envelope_download) do
190194
create(
191195
:envelope_download,
192196
envelope_community:,
@@ -207,6 +211,8 @@
207211
it 'returns `in progress`' do
208212
expect { perform_request }.not_to change(EnvelopeDownload, :count)
209213
expect_status(:ok)
214+
expect_json_sizes(2)
215+
expect_json('started_at', envelope_download.started_at.as_json)
210216
expect_json('status', 'in_progress')
211217
end
212218
end
@@ -216,13 +222,17 @@
216222
# rubocop:todo RSpec/NestedGroups
217223
context 'failed' do # rubocop:todo RSpec/ContextWording, RSpec/MultipleMemoizedHelpers, RSpec/NestedGroups
218224
# rubocop:enable RSpec/NestedGroups
219-
let(:status) { :finished }
220225
let(:internal_error_message) { Faker::Lorem.sentence }
226+
let(:status) { :finished }
227+
let(:url) { Faker::Internet.url }
221228

222229
it 'returns `failed`' do
223230
expect { perform_request }.not_to change(EnvelopeDownload, :count)
224231
expect_status(:ok)
232+
expect_json_sizes(3)
233+
expect_json('finished_at', envelope_download.finished_at.as_json)
225234
expect_json('status', 'failed')
235+
expect_json('url', url)
226236
end
227237
end
228238
# rubocop:enable RSpec/MultipleMemoizedHelpers
@@ -238,6 +248,8 @@
238248
it 'returns `finished` and URL' do
239249
expect { perform_request }.not_to change(EnvelopeDownload, :count)
240250
expect_status(:ok)
251+
expect_json_sizes(3)
252+
expect_json('finished_at', envelope_download.finished_at.as_json)
241253
expect_json('status', 'finished')
242254
expect_json('url', url)
243255
end
@@ -280,17 +292,26 @@
280292
end
281293

282294
context 'with valid token' do
295+
let(:now) { Time.current.change(usec: 0) }
296+
283297
context 'without envelope download' do # rubocop:todo RSpec/NestedGroups
284298
# rubocop:todo RSpec/MultipleExpectations
285-
it 'creates new pending download and enqueues job' do
299+
it 'creates new pending download and enqueues job' do # rubocop:todo RSpec/ExampleLength
286300
# rubocop:enable RSpec/MultipleExpectations
287-
expect { perform_request }.to change(EnvelopeDownload, :count).by(1)
301+
travel_to now do
302+
expect { perform_request }.to change(EnvelopeDownload, :count).by(1)
303+
end
304+
288305
expect_status(:created)
289306

290307
envelope_download = EnvelopeDownload.last
291308
expect(envelope_download.envelope_community).to eq(envelope_community)
292309
expect(envelope_download.status).to eq('pending')
293310

311+
expect_json_sizes(2)
312+
expect_json('enqueued_at', now.as_json)
313+
expect_json('status', 'pending')
314+
294315
expect(ActiveJob::Base.queue_adapter.enqueued_jobs.size).to eq(1)
295316

296317
job = ActiveJob::Base.queue_adapter.enqueued_jobs.first
@@ -299,18 +320,27 @@
299320
end
300321
end
301322

323+
# rubocop:todo RSpec/MultipleMemoizedHelpers
302324
context 'with envelope download' do # rubocop:todo RSpec/NestedGroups
303325
let!(:envelope_download) do
304326
create(:envelope_download, :finished, envelope_community:)
305327
end
306328

307329
it 'enqueues job for existing download' do
308-
expect { perform_request }.to not_change(EnvelopeDownload, :count)
309-
.and enqueue_job(DownloadEnvelopesJob).with(envelope_download.id)
330+
travel_to now do
331+
expect { perform_request }.to not_change(EnvelopeDownload, :count)
332+
.and enqueue_job(DownloadEnvelopesJob).with(envelope_download.id)
333+
end
334+
310335
expect_status(:created)
311336
expect(envelope_download.reload.status).to eq('pending')
337+
338+
expect_json_sizes(2)
339+
expect_json('enqueued_at', now.as_json)
340+
expect_json('status', 'pending')
312341
end
313342
end
343+
# rubocop:enable RSpec/MultipleMemoizedHelpers
314344
end
315345
end
316346
end

spec/factories/envelope_downloads.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
FactoryBot.define do
22
factory :envelope_download do
3+
enqueued_at { Time.current.change(usec: 0) }
34
# rubocop:todo FactoryBot/FactoryAssociationWithStrategy
45
envelope_community { create(:envelope_community, :with_random_name) }
56
# rubocop:enable FactoryBot/FactoryAssociationWithStrategy
67

78
trait :failed do
8-
finished_at { Time.current }
9+
finished_at { Time.current.change(usec: 0) }
910
internal_error_message { Faker::Lorem.sentence }
10-
started_at { Time.current }
11+
started_at { Time.current.change(usec: 0) }
1112
status { :finished }
1213
end
1314

1415
trait :finished do
15-
finished_at { Time.current }
16-
started_at { Time.current }
16+
finished_at { Time.current.change(usec: 0) }
17+
started_at { Time.current.change(usec: 0) }
1718
status { :finished }
1819
end
1920

2021
trait :in_progress do
21-
started_at { Time.current }
22+
started_at { Time.current.change(usec: 0) }
2223
status { :in_progress }
2324
end
2425
end

0 commit comments

Comments
 (0)