-
Notifications
You must be signed in to change notification settings - Fork 16
/
dependency_resolution_worker.rb
110 lines (89 loc) · 2.89 KB
/
dependency_resolution_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
class DependencyResolutionWorker
include Sidekiq::Worker
include PerformAsyncInQueue
sidekiq_options queue: :dependency_resolution
def perform(args = {})
assign_attributes(args)
send_source_stats
dependencies.each do |(content_id, locale)|
send_downstream(content_id, locale)
end
orphaned_content_ids_for_locale.each { |content_id| send_downstream(content_id, locale) }
end
private
attr_reader :content_id,
:locale,
:content_store,
:orphaned_content_ids,
:source_command,
:source_document_type,
:source_fields
def assign_attributes(args)
@content_id = args.fetch("content_id")
@locale = args.fetch("locale")
@content_store = args.fetch("content_store").constantize
@orphaned_content_ids = args.fetch("orphaned_content_ids", [])
@source_command = args["source_command"]
@source_document_type = args["source_document_type"]
@source_fields = args.fetch("source_fields", [])
end
def orphaned_content_ids_for_locale
Document
.distinct
.joins(:editions)
.where(editions: { content_store: content_stores },
content_id: orphaned_content_ids,
locale:)
.pluck(:content_id)
end
def content_stores
draft? ? %w[draft live] : %w[live]
end
def send_source_stats
prefix = "dependency_resolution.source"
GovukStatsd.increment("#{prefix}.command.#{source_command}") if source_command
GovukStatsd.increment("#{prefix}.document_type.#{source_document_type}") if source_document_type
source_fields.each do |field|
GovukStatsd.increment("#{prefix}.field.#{field}")
end
end
def send_downstream(content_id, locale)
downstream_draft(content_id, locale)
downstream_live(content_id, locale)
end
def dependencies
Queries::ContentDependencies.new(
content_id:,
locale:,
content_stores:,
).call
end
def draft?
content_store == Adapters::DraftContentStore
end
def downstream_draft(dependent_content_id, locale)
return unless draft?
DownstreamDraftWorker.perform_async_in_queue(
DownstreamDraftWorker::LOW_QUEUE,
"content_id" => dependent_content_id,
"locale" => locale,
"update_dependencies" => false,
"dependency_resolution_source_content_id" => content_id,
"source_command" => source_command,
"source_fields" => source_fields,
)
end
def downstream_live(dependent_content_id, locale)
return if draft?
DownstreamLiveWorker.perform_async_in_queue(
DownstreamLiveWorker::LOW_QUEUE,
"content_id" => dependent_content_id,
"locale" => locale,
"message_queue_event_type" => "links",
"update_dependencies" => false,
"dependency_resolution_source_content_id" => content_id,
"source_command" => source_command,
"source_fields" => source_fields,
)
end
end