forked from alphagov/publishing-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
downstream_discard_draft_worker.rb
82 lines (70 loc) · 2.32 KB
/
downstream_discard_draft_worker.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class DownstreamDiscardDraftWorker
include DownstreamQueue
include Sidekiq::Worker
include PerformAsyncInQueue
sidekiq_options queue: HIGH_QUEUE
def perform(args = {})
assign_attributes(args.symbolize_keys)
current_path = edition.try(:base_path)
if current_path
DownstreamService.update_draft_content_store(downstream_payload)
if base_path && current_path != base_path
DownstreamService.discard_from_draft_content_store(base_path)
end
elsif base_path
DownstreamService.discard_from_draft_content_store(base_path)
end
update_expanded_links
enqueue_dependencies if update_dependencies
rescue DiscardDraftBasePathConflictError => e
logger.warn(e.message)
end
private
attr_reader :base_path,
:content_id,
:locale,
:edition,
:payload_version,
:update_dependencies,
:source_command,
:source_document_type
def assign_attributes(attributes)
@base_path = attributes.fetch(:base_path)
@content_id = attributes.fetch(:content_id)
@locale = attributes.fetch(:locale)
@payload_version = Event.maximum_id
@edition = Queries::GetEditionForContentStore.call(content_id, locale, include_draft: true)
@update_dependencies = attributes.fetch(:update_dependencies, true)
@source_command = attributes[:source_command]
@source_document_type = attributes[:source_document_type]
end
def enqueue_dependencies
DependencyResolutionWorker.perform_async(
content_store: Adapters::DraftContentStore,
content_id: content_id,
locale: locale,
source_command: source_command,
source_document_type: edition&.document_type || source_document_type,
)
end
def update_expanded_links
if edition
ExpandedLinks.locked_update(
content_id: content_id,
locale: locale,
with_drafts: true,
payload_version: payload_version,
expanded_links: downstream_payload.expanded_links,
)
else
ExpandedLinks.where(
content_id: content_id,
locale: locale,
with_drafts: true,
).where("payload_version < ?", payload_version).delete_all
end
end
def downstream_payload
@downstream_payload ||= DownstreamPayload.new(edition, payload_version, draft: true)
end
end