Skip to content

Commit 9fba374

Browse files
committed
Add redeliver batch between times task
This task attempts to redeliver any existing batch deliveries for a given form that correspond to a given time range. This means that if there are any submissions within the given times, and there is a corresponding batch delivery that includes these submissions, then the batch will be redelivered.
1 parent f9e5861 commit 9fba374

File tree

2 files changed

+207
-0
lines changed

2 files changed

+207
-0
lines changed

lib/tasks/submissions.rake

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,55 @@ namespace :submissions do
172172
end
173173
end
174174

175+
desc "Re-deliver daily submission batches corresponding to submissions between two timestamps for a specific form"
176+
task :redeliver_daily_batches_by_date, %i[form_id start_timestamp end_timestamp dry_run] => :environment do |_, args|
177+
form_id = args[:form_id]
178+
start_timestamp = args[:start_timestamp]
179+
end_timestamp = args[:end_timestamp]
180+
dry_run_arg = args[:dry_run]
181+
dry_run = dry_run_arg == "true"
182+
183+
usage_message = "usage: rake submissions:redeliver_daily_batches_by_date[<form_id>,<start_timestamp>,<end_timestamp>,<dry_run>]".freeze
184+
185+
if form_id.blank? || start_timestamp.blank? || end_timestamp.blank? || !dry_run_arg.in?(%w[true false])
186+
abort usage_message
187+
end
188+
189+
start_time = Time.zone.parse(start_timestamp)
190+
end_time = Time.zone.parse(end_timestamp)
191+
192+
if start_time.nil? || end_time.nil?
193+
abort "Error: Invalid timestamp format. Use ISO 8601 format (e.g. '2024-01-01T00:00:00Z')"
194+
end
195+
196+
if start_time >= end_time
197+
abort "Error: Start timestamp must be before end timestamp"
198+
end
199+
200+
deliveries_to_redeliver = Delivery.daily
201+
.joins(:submissions)
202+
.where(submissions: { form_id: form_id, created_at: start_time..end_time })
203+
.distinct
204+
205+
Rails.logger.info "Time range: #{start_time} to #{end_time}"
206+
Rails.logger.info "Dry run mode: #{dry_run ? 'enabled' : 'disabled'}"
207+
208+
if deliveries_to_redeliver.any?
209+
Rails.logger.info "Found #{deliveries_to_redeliver.count} submission batches to re-deliver for form ID: #{form_id}"
210+
211+
deliveries_to_redeliver.each do |delivery|
212+
if dry_run
213+
Rails.logger.info "Would re-deliver batch with ID #{delivery.id} and delivery reference #{delivery.delivery_reference}"
214+
else
215+
Rails.logger.info "Re-delivering batch ID #{delivery.id} and delivery reference #{delivery.delivery_reference}"
216+
SendSubmissionBatchJob.perform_later(delivery:)
217+
end
218+
end
219+
else
220+
Rails.logger.info "No batches found matching the criteria"
221+
end
222+
end
223+
175224
namespace :file_answers do
176225
desc "Generate filename for file upload answers that do not have an original filename stored and schedule the submission to be sent"
177226
task :fix_missing_original_filenames, %i[reference] => :environment do |_, args|

spec/lib/tasks/submissions.rake_spec.rb

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,164 @@
535535
end
536536
end
537537

538+
describe "submissions:redeliver_daily_batches_by_date" do
539+
subject(:task) do
540+
Rake::Task["submissions:redeliver_daily_batches_by_date"]
541+
.tap(&:reenable)
542+
end
543+
544+
let(:form_id) { 1 }
545+
let(:other_form_id) { 2 }
546+
let(:start_time) { "2024-01-01T00:00:00Z" }
547+
let(:end_time) { "2024-01-02T00:00:00Z" }
548+
let(:dry_run) { "false" }
549+
550+
let!(:batched_submission) do
551+
create :submission,
552+
:sent,
553+
form_id:,
554+
created_at: Time.parse("2024-01-01T12:00:00Z"),
555+
reference: "ref1"
556+
end
557+
558+
let!(:batch_delivery) do
559+
create :delivery,
560+
:daily_scheduled_delivery,
561+
:failed,
562+
submissions: [batched_submission],
563+
created_at: Time.parse("2024-01-02T02:00:00Z"),
564+
delivery_reference: "batch1"
565+
end
566+
567+
let!(:next_day_batch_delivery) do
568+
submission = create :submission,
569+
:sent,
570+
form_id:,
571+
created_at: Time.parse("2024-01-02T12:00:00Z"),
572+
reference: "ref1"
573+
create :delivery,
574+
:daily_scheduled_delivery,
575+
:failed,
576+
submissions: [submission],
577+
created_at: Time.parse("2024-01-03T02:00:00Z"),
578+
delivery_reference: "batch-next-day"
579+
end
580+
581+
let!(:other_form_batch_delivery) do
582+
submission = create :submission,
583+
:sent,
584+
form_id: other_form_id,
585+
created_at: Time.parse("2024-01-01T12:00:00Z"),
586+
reference: "ref2"
587+
create :delivery,
588+
:daily_scheduled_delivery,
589+
:failed,
590+
submissions: [submission],
591+
created_at: Time.parse("2024-01-02T02:00:00Z"),
592+
delivery_reference: "batch2"
593+
end
594+
595+
context "with valid arguments" do
596+
let(:valid_args) { [form_id, start_time, end_time, dry_run] }
597+
598+
it "enqueues matching batch delivery for re-delivery" do
599+
expect {
600+
task.invoke(*valid_args)
601+
}.to have_enqueued_job(SendSubmissionBatchJob).with(delivery: batch_delivery)
602+
end
603+
604+
it "does not enqueue a batch delivery outside the time range" do
605+
expect {
606+
task.invoke(*valid_args)
607+
}.not_to have_enqueued_job(SendSubmissionBatchJob).with(delivery: next_day_batch_delivery)
608+
end
609+
610+
it "does not enqueue another form's batch deliveries" do
611+
expect {
612+
task.invoke(*valid_args)
613+
}.not_to have_enqueued_job(SendSubmissionBatchJob).with(delivery: other_form_batch_delivery)
614+
end
615+
616+
context "when dry_run is true" do
617+
let(:dry_run) { "true" }
618+
619+
it "does not enqueue any jobs" do
620+
expect {
621+
task.invoke(*valid_args)
622+
}.not_to have_enqueued_job(SendSubmissionJob)
623+
end
624+
end
625+
626+
context "when no submissions took place between the given times" do
627+
let(:valid_args) { [form_id, "2025-01-01T00:00:00Z", "2025-01-02T00:00:00Z", dry_run] }
628+
629+
it "does not enqueue any jobs" do
630+
expect {
631+
task.invoke(*valid_args)
632+
}.not_to have_enqueued_job(SendSubmissionBatchJob)
633+
end
634+
end
635+
end
636+
637+
context "with invalid arguments" do
638+
it "aborts when form_id is missing" do
639+
expect {
640+
task.invoke("", start_time, end_time, dry_run)
641+
}.to raise_error(SystemExit)
642+
.and output("usage: rake submissions:redeliver_daily_batches_by_date[<form_id>,<start_timestamp>,<end_timestamp>,<dry_run>]\n").to_stderr
643+
end
644+
645+
it "aborts when start_timestamp is missing" do
646+
expect {
647+
task.invoke(form_id, "", end_time, dry_run)
648+
}.to raise_error(SystemExit)
649+
.and output("usage: rake submissions:redeliver_daily_batches_by_date[<form_id>,<start_timestamp>,<end_timestamp>,<dry_run>]\n").to_stderr
650+
end
651+
652+
it "aborts when end_timestamp is missing" do
653+
expect {
654+
task.invoke(form_id, start_time, "", dry_run)
655+
}.to raise_error(SystemExit)
656+
.and output("usage: rake submissions:redeliver_daily_batches_by_date[<form_id>,<start_timestamp>,<end_timestamp>,<dry_run>]\n").to_stderr
657+
end
658+
659+
it "aborts when start_timestamp is invalid" do
660+
expect {
661+
task.invoke(form_id, "invalid-date", end_time, dry_run)
662+
}.to raise_error(SystemExit)
663+
.and output("Error: Invalid timestamp format. Use ISO 8601 format (e.g. '2024-01-01T00:00:00Z')\n").to_stderr
664+
end
665+
666+
it "aborts when end_timestamp is invalid" do
667+
expect {
668+
task.invoke(form_id, start_time, "invalid-date", dry_run)
669+
}.to raise_error(SystemExit)
670+
.and output("Error: Invalid timestamp format. Use ISO 8601 format (e.g. '2024-01-01T00:00:00Z')\n").to_stderr
671+
end
672+
673+
it "aborts when start_timestamp is after end_timestamp" do
674+
expect {
675+
task.invoke(form_id, "2024-01-02T00:00:00Z", "2024-01-01T00:00:00Z", dry_run)
676+
}.to raise_error(SystemExit)
677+
.and output("Error: Start timestamp must be before end timestamp\n").to_stderr
678+
end
679+
680+
it "aborts when start_timestamp equals end_timestamp" do
681+
expect {
682+
task.invoke(form_id, start_time, start_time, dry_run)
683+
}.to raise_error(SystemExit)
684+
.and output("Error: Start timestamp must be before end timestamp\n").to_stderr
685+
end
686+
687+
it "aborts when dry_run is an invalid value" do
688+
expect {
689+
task.invoke(form_id, start_time, start_time, "foo")
690+
}.to raise_error(SystemExit)
691+
.and output("usage: rake submissions:redeliver_daily_batches_by_date[<form_id>,<start_timestamp>,<end_timestamp>,<dry_run>]\n").to_stderr
692+
end
693+
end
694+
end
695+
538696
describe "submissions:file_answers:fix_missing_original_filenames" do
539697
subject(:task) do
540698
Rake::Task["submissions:file_answers:fix_missing_original_filenames"]

0 commit comments

Comments
 (0)