diff --git a/app/controllers/message_drafts_controller.rb b/app/controllers/message_drafts_controller.rb index de7107105..96552b20b 100644 --- a/app/controllers/message_drafts_controller.rb +++ b/app/controllers/message_drafts_controller.rb @@ -41,7 +41,7 @@ def submit end def submit_all - @message_drafts.each(&:submit) + Govbox::SubmitMultipleMessageDraftsJob.perform_later(@message_drafts.to_a) end def destroy diff --git a/app/jobs/govbox/finish_message_drafts_submit_job.rb b/app/jobs/govbox/finish_message_drafts_submit_job.rb new file mode 100644 index 000000000..563aa0e06 --- /dev/null +++ b/app/jobs/govbox/finish_message_drafts_submit_job.rb @@ -0,0 +1,5 @@ +class Govbox::FinishMessageDraftsSubmitJob < ApplicationJob + def perform(batch, params) + Govbox::SyncBoxJob.set(wait: 3.minutes).perform_later(batch.properties[:box]) + end +end diff --git a/app/jobs/govbox/submit_message_draft_job.rb b/app/jobs/govbox/submit_message_draft_job.rb index 59b388aaa..c5768e9f6 100644 --- a/app/jobs/govbox/submit_message_draft_job.rb +++ b/app/jobs/govbox/submit_message_draft_job.rb @@ -1,5 +1,5 @@ class Govbox::SubmitMessageDraftJob < ApplicationJob - def perform(message_draft, upvs_client: UpvsEnvironment.upvs_client) + def perform(message_draft, schedule_sync: true, upvs_client: UpvsEnvironment.upvs_client) message_draft_data = { posp_id: message_draft.metadata["posp_id"], posp_version: message_draft.metadata["posp_version"], @@ -19,7 +19,7 @@ def perform(message_draft, upvs_client: UpvsEnvironment.upvs_client) success, response_status = sktalk_api.receive_and_save_to_outbox(message_draft_data) if success message_draft.metadata["status"] = "submitted" - Govbox::SyncBoxJob.set(wait: 3.minutes).perform_later(message_draft.thread.folder.box) + Govbox::SyncBoxJob.set(wait: 3.minutes).perform_later(message_draft.thread.folder.box) if schedule_sync else handle_submit_fail(message_draft, response_status) end diff --git a/app/jobs/govbox/submit_multiple_message_drafts_job.rb b/app/jobs/govbox/submit_multiple_message_drafts_job.rb new file mode 100644 index 000000000..d3c2a3a01 --- /dev/null +++ b/app/jobs/govbox/submit_multiple_message_drafts_job.rb @@ -0,0 +1,7 @@ +class Govbox::SubmitMultipleMessageDraftsJob < ApplicationJob + def perform(message_drafts, jobs_batch: GoodJob::Batch.new) + message_drafts.each { |message_draft| message_draft.submit(jobs_batch: jobs_batch) } + + jobs_batch.enqueue(on_finish: Govbox::FinishMessageDraftsSubmitJob, box: message_draft.thread.folder.box) + end +end diff --git a/app/models/message_draft.rb b/app/models/message_draft.rb index cfbf64429..ca52ed1f7 100644 --- a/app/models/message_draft.rb +++ b/app/models/message_draft.rb @@ -36,10 +36,14 @@ def self.create_message_reply(original_message: , author:) ) end - def submit + def submit(jobs_batch: nil) return false unless submittable? - Govbox::SubmitMessageDraftJob.perform_later(self) + if jobs_batch + jobs_batch.add { Govbox::SubmitMessageDraftJob.perform_later(self, schedule_sync: false) } + else + Govbox::SubmitMessageDraftJob.perform_later(self) + end metadata["status"] = "being_submitted" save!