-
Notifications
You must be signed in to change notification settings - Fork 8
/
record_fetcher.rb
51 lines (45 loc) · 1.83 KB
/
record_fetcher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class RecordFetcher
include ActiveModel::Model
attr_accessor :record
EXCEPTIONS = [VBMS::ClientError, VVA::ClientError].freeze
SECONDS_TO_AUTO_UNLOCK = 90
def process
s = Redis::Semaphore.new("record_#{record.id}".to_s,
url: Rails.application.secrets.redis_url_cache,
stale_client_timeout: 5,
expiration: SECONDS_TO_AUTO_UNLOCK)
s.lock(SECONDS_TO_AUTO_UNLOCK)
content_from_s3 || content_from_vbms
rescue *EXCEPTIONS => error
Rails.logger.error("Caught #{error}")
nil
ensure
s&.unlock
end
private
def content_from_vbms
content = MetricsService.record("#{record.manifest_source.name} v2_fetch_document_file",
service: record.manifest_source.name.downcase.to_sym,
name: "v2_fetch_document_file") do
record.service.v2_fetch_document_file(record)
end
content = MetricsService.record("ImageConverterService for #{record.s3_filename}",
service: :image_converter,
name: "image_converter_service") do
ImageConverterService.new(image: content, record: record).process
end
MetricsService.record("S3: RecordFetcher store content for #{record.s3_filename}",
service: :s3,
name: "content_from_vbms") do
S3Service.store_file(record.s3_filename, content)
end
content
end
def content_from_s3
@content_from_s3 ||= MetricsService.record("S3: RecordFetcher fetch content for: #{record.s3_filename}",
service: :s3,
name: "content_from_s3") do
S3Service.fetch_content(record.s3_filename)
end
end
end