mirror of https://github.com/docusealco/docuseal
parent
e55b5b47cd
commit
4180d02a1b
@ -0,0 +1,4 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class ApplicationJob < ActiveJob::Base
|
||||
end
|
||||
@ -0,0 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class GenerateSubmitterResultAttachmentsJob < ApplicationJob
|
||||
def perform(submitter)
|
||||
Submissions::EnsureResultGenerated.call(submitter)
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,31 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: document_generation_events
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# event_name :string not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# submitter_id :bigint not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_document_generation_events_on_submitter_id (submitter_id)
|
||||
# index_document_generation_events_on_submitter_id_and_event_name (submitter_id,event_name) UNIQUE WHERE ((event_name)::text = ANY ((ARRAY['start'::character varying, 'complete'::character varying])::text[]))
|
||||
#
|
||||
# Foreign Keys
|
||||
#
|
||||
# fk_rails_... (submitter_id => submitters.id)
|
||||
#
|
||||
class DocumentGenerationEvent < ApplicationRecord
|
||||
belongs_to :submitter
|
||||
|
||||
enum :event_name, {
|
||||
complete: 'complete',
|
||||
fail: 'fail',
|
||||
start: 'start',
|
||||
retry: 'retry'
|
||||
}, scope: false
|
||||
end
|
||||
@ -0,0 +1,14 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class CreateDocumentGenerationEvents < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
create_table :document_generation_events do |t|
|
||||
t.references :submitter, null: false, foreign_key: true, index: true
|
||||
t.string :event_name, null: false
|
||||
|
||||
t.index %i[submitter_id event_name], unique: true, where: "event_name IN ('start', 'complete')"
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,58 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Submissions
|
||||
module EnsureResultGenerated
|
||||
WAIT_FOR_RETRY = 2.seconds
|
||||
CHECK_EVENT_INTERVAL = 1.second
|
||||
CHECK_COMPLETE_TIMEOUT = 20.seconds
|
||||
|
||||
WaitForCompleteTimeout = Class.new(StandardError)
|
||||
|
||||
module_function
|
||||
|
||||
def call(submitter)
|
||||
return submitter.documents if submitter.document_generation_events.complete.exists?
|
||||
|
||||
events =
|
||||
DocumentGenerationEvent.uncached do
|
||||
DocumentGenerationEvent.where(submitter:).order(:created_at).to_a
|
||||
end
|
||||
|
||||
if events.present? && events.last.event_name.in?(%w[start retry])
|
||||
wait_for_complete_or_fail(submitter)
|
||||
else
|
||||
submitter.document_generation_events.create!(event_name: events.present? ? :retry : :start)
|
||||
|
||||
GenerateResultAttachments.call(submitter)
|
||||
|
||||
submitter.document_generation_events.create!(event_name: :complete)
|
||||
end
|
||||
rescue ActiveRecord::RecordNotUnique
|
||||
sleep WAIT_FOR_RETRY
|
||||
|
||||
retry
|
||||
rescue StandardError
|
||||
submitter.document_generation_events.create!(event_name: :fail)
|
||||
|
||||
raise
|
||||
end
|
||||
|
||||
def wait_for_complete_or_fail(submitter)
|
||||
total_wait_time = 0
|
||||
|
||||
loop do
|
||||
sleep CHECK_EVENT_INTERVAL
|
||||
total_wait_time += CHECK_EVENT_INTERVAL
|
||||
|
||||
last_event =
|
||||
DocumentGenerationEvent.uncached do
|
||||
DocumentGenerationEvent.where(submitter:).order(:created_at).last
|
||||
end
|
||||
|
||||
break last_event if last_event.event_name.in?(%w[complete fail])
|
||||
|
||||
raise WaitForCompleteTimeout if total_wait_time > CHECK_COMPLETE_TIMEOUT
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in new issue