Skip to content

Commit

Permalink
Refactor document syncing
Browse files Browse the repository at this point in the history
Extract common logic into `Operation` from subclasses, so they can focus
on the unique behaviour for each respective operation.
  • Loading branch information
csutter committed Aug 6, 2024
1 parent 12605ee commit ef276ae
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 77 deletions.
43 changes: 9 additions & 34 deletions app/services/discovery_engine/sync/delete.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,21 @@
module DiscoveryEngine::Sync
class Delete < Operation
def call
lock.acquire

unless version_cache.sync_required?
super do
client.delete_document(name: document_name)
rescue Google::Cloud::NotFoundError => e
increment_counter("already_not_present")
log(
Logger::Severity::INFO,
"Ignored as newer version already synced",
)
Metrics::Exported.increment_counter(
:discovery_engine_requests, type: "delete", status: "ignored_outdated"
"Did not delete document as it doesn't exist remotely (#{e.message}).",
)
return
end
end

client.delete_document(name: document_name)

version_cache.set_as_latest_synced_version
private

log(Logger::Severity::INFO, "Successfully deleted")
Metrics::Exported.increment_counter(
:discovery_engine_requests, type: "delete", status: "success"
)
rescue Google::Cloud::NotFoundError => e
log(
Logger::Severity::INFO,
"Did not delete document as it doesn't exist remotely (#{e.message}).",
)
Metrics::Exported.increment_counter(
:discovery_engine_requests, type: "delete", status: "already_not_present"
)
rescue Google::Cloud::Error => e
log(
Logger::Severity::ERROR,
"Failed to delete document due to an error (#{e.message})",
)
GovukError.notify(e)
Metrics::Exported.increment_counter(
:discovery_engine_requests, type: "delete", status: "error"
)
ensure
lock.release
def type
:delete
end
end
end
26 changes: 26 additions & 0 deletions app/services/discovery_engine/sync/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@ def initialize(content_id, payload_version: nil, client: nil)
@client = client || ::Google::Cloud::DiscoveryEngine.document_service(version: :v1)
end

def call
lock.acquire

if version_cache.sync_required?
yield

version_cache.set_as_latest_synced_version

increment_counter("success")
log(Logger::Severity::INFO, "Successful #{type}")
else
increment_counter("ignored_outdated")
log(Logger::Severity::INFO, "Ignored as newer version already synced")
end
rescue Google::Cloud::Error => e
increment_counter("error")
log(Logger::Severity::ERROR, "Failed to #{type} document due to an error (#{e.message})")
GovukError.notify(e)
ensure
lock.release
end

private

attr_reader :content_id, :payload_version, :client
Expand All @@ -32,5 +54,9 @@ def log(level, message)
)
Rails.logger.add(level, combined_message)
end

def increment_counter(status)
Metrics::Exported.increment_counter(:discovery_engine_requests, type:, status:)
end
end
end
57 changes: 17 additions & 40 deletions app/services/discovery_engine/sync/put.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,29 @@ def initialize(content_id, metadata = nil, content: "", payload_version: nil, cl
end

def call
lock.acquire

unless version_cache.sync_required?
log(
Logger::Severity::INFO,
"Ignored as newer version already synced",
)
Metrics::Exported.increment_counter(
:discovery_engine_requests, type: "put", status: "ignored_outdated"
super do
client.update_document(
document: {
id: content_id,
name: document_name,
json_data: metadata.merge(payload_version:).to_json,
content: {
mime_type: MIME_TYPE,
# The Google client expects an IO object to extract raw byte content from
raw_bytes: StringIO.new(content),
},
},
allow_missing: true,
)
return
end

client.update_document(
document: {
id: content_id,
name: document_name,
json_data: metadata.merge(payload_version:).to_json,
content: {
mime_type: MIME_TYPE,
# The Google client expects an IO object to extract raw byte content from
raw_bytes: StringIO.new(content),
},
},
allow_missing: true,
)

version_cache.set_as_latest_synced_version

log(Logger::Severity::INFO, "Successfully added/updated")
Metrics::Exported.increment_counter(
:discovery_engine_requests, type: "put", status: "success"
)
rescue Google::Cloud::Error => e
log(
Logger::Severity::ERROR,
"Failed to add/update document due to an error (#{e.message})",
)
GovukError.notify(e)
Metrics::Exported.increment_counter(:discovery_engine_requests, type: "put", status: "error")
ensure
lock.release
end

private

attr_reader :metadata, :content

def type
:put
end
end
end
2 changes: 1 addition & 1 deletion spec/services/discovery_engine/sync/delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
it "logs the delete operation" do
expect(logger).to have_received(:add).with(
Logger::Severity::INFO,
"[DiscoveryEngine::Sync::Delete] Successfully deleted content_id:some_content_id payload_version:1",
"[DiscoveryEngine::Sync::Delete] Successful delete content_id:some_content_id payload_version:1",
)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/services/discovery_engine/sync/put_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
it "logs the put operation" do
expect(logger).to have_received(:add).with(
Logger::Severity::INFO,
"[DiscoveryEngine::Sync::Put] Successfully added/updated content_id:some_content_id payload_version:1",
"[DiscoveryEngine::Sync::Put] Successful put content_id:some_content_id payload_version:1",
)
end
end
Expand Down Expand Up @@ -123,7 +123,7 @@
it "logs the failure" do
expect(logger).to have_received(:add).with(
Logger::Severity::ERROR,
"[DiscoveryEngine::Sync::Put] Failed to add/update document due to an error (Something went wrong) content_id:some_content_id payload_version:1",
"[DiscoveryEngine::Sync::Put] Failed to put document due to an error (Something went wrong) content_id:some_content_id payload_version:1",
)
end

Expand Down

0 comments on commit ef276ae

Please sign in to comment.