Skip to content

Commit

Permalink
Merge pull request #1475 from department-of-veterans-affairs/Daniel/A…
Browse files Browse the repository at this point in the history
…PPEAL-34625

hotfix/APPEALS-34625
  • Loading branch information
raymond-hughes authored Dec 21, 2023
2 parents 7e08c05 + f3aa69e commit 4ed40be
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app/jobs/v2/download_manifest_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class V2::DownloadManifestJob < ApplicationJob
queue_as :high_priority

def perform(manifest_source, user = nil)
return if manifest_source.current?
manifest_source.update(status: :pending)

RequestStore.store[:current_user] = user if user
Raven.extra_context(manifest_source: manifest_source.id)
Expand Down
8 changes: 4 additions & 4 deletions app/models/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Manifest < ApplicationRecord

def start!
# Reset stale manifests.
update!(fetched_files_status: :initialized) if stale?
update!(fetched_files_status: :initialized) if ready_for_refresh?

vbms_source.start!
vva_source.start! unless FeatureToggle.enabled?(:skip_vva)
Expand All @@ -54,9 +54,9 @@ def download_and_package_files!
end
end

# completed? because it can be recent pending with stale fetched_files_at
def stale?
completed? && fetched_files_at && fetched_files_at < UI_HOURS_UNTIL_EXPIRY.hours.ago
# ready_for_refresh? we want to refresh the manifest after 72 hours regardless of the state
def ready_for_refresh?
!initialized? && fetched_files_at && fetched_files_at < UI_HOURS_UNTIL_EXPIRY.hours.ago
end

def completed?
Expand Down
25 changes: 9 additions & 16 deletions app/models/manifest_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,13 @@ class ManifestSource < ApplicationRecord

delegate :file_number, to: :manifest

SECONDS_TO_AUTO_UNLOCK = 5

def start!
s = Redis::Semaphore.new("download_manifest_source_#{id}".to_s,
url: Rails.application.secrets.redis_url_cache,
expiration: SECONDS_TO_AUTO_UNLOCK)
s.lock(SECONDS_TO_AUTO_UNLOCK) do
return if current? || pending?
update(status: :pending)
end

begin
V2::DownloadManifestJob.perform_later(self, RequestStore[:current_user])
rescue StandardError
update(status: :initialized)
return if current? || processing?
V2::DownloadManifestJob.perform_later(self, RequestStore[:current_user])
rescue StandardError
update(status: :initialized)

raise
end
raise
end

def service
Expand All @@ -53,4 +42,8 @@ def expiry_hours
def current?
success? && fetched_at && fetched_at > expiry_hours.hours.ago
end

def processing?
pending? && fetched_at && fetched_at > 24.hours.ago
end
end
18 changes: 9 additions & 9 deletions app/services/manifest_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ class ManifestFetcher
EXCEPTIONS = [VBMS::ClientError, VVA::ClientError].freeze

def process
begin
DocumentCreator.new(manifest_source: manifest_source, external_documents: documents).create
manifest_source.update!(status: :success, fetched_at: Time.zone.now)
documents
rescue *EXCEPTIONS => e
manifest_source.update!(status: :failed)
ExceptionLogger.capture(e)
[]
end
DocumentCreator.new(manifest_source: manifest_source, external_documents: documents).create
manifest_source.update!(status: :success, fetched_at: Time.zone.now)
documents
rescue *EXCEPTIONS => e
manifest_source.update!(status: :failed)
ExceptionLogger.capture(e)
raise e
ensure
return documents
end

def documents
Expand Down
15 changes: 13 additions & 2 deletions spec/models/manifest_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@
end
end

context "when manifest is pending" do
context "when manifest is pending less than 24 hours" do
before do
source.update_attributes!(fetched_at: Time.zone.now - 7.hours, status: :pending)
source.update_attributes!(fetched_at: Time.zone.now - 3.hours, status: :pending)
end

it "does not start the manifest job" do
Expand All @@ -93,6 +93,17 @@
end
end

context "when manifest is pending more than 24 hours" do
before do
source.update_attributes!(fetched_at: Time.zone.now - 25.hours, status: :pending)
end

it "starts the manifest job" do
subject
expect(V2::DownloadManifestJob).to have_received(:perform_later)
end
end

context "when fetched less than 3 hours ago" do
before do
source.update_attributes!(fetched_at: Time.zone.now - 2.hours, status: :success)
Expand Down
6 changes: 3 additions & 3 deletions spec/services/manifest_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
allow(VBMSService).to receive(:v2_fetch_documents_for).and_raise(VBMS::ClientError)
end

it "saves manifest status as failed" do
expect(subject).to eq []
it "saves manifest status as failed and propagates errors" do
expect{ subject }.to raise_error(VBMS::ClientError)
expect(source.reload.status).to eq "failed"
expect(source.reload.fetched_at).to be_nil
end
Expand Down Expand Up @@ -99,7 +99,7 @@
end

it "saves manifest status as failed" do
expect(subject).to eq []
expect { subject }.to raise_error(VVA::ClientError)
expect(source.reload.status).to eq "failed"
expect(source.reload.fetched_at).to be_nil
end
Expand Down

0 comments on commit 4ed40be

Please sign in to comment.