Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: DAH-2556 check translations cache LastModifiedDate #2283

Merged
merged 15 commits into from
Sep 17, 2024

Conversation

cade-exygy
Copy link
Collaborator

@cade-exygy cade-exygy commented Sep 6, 2024

Description

This PR adds methods to compare the LastModifiedDate of the listing to the LastModifiedDate of the translations cache. Current behavior is when the listing modified date is newer than the translation cache, reprocess translations. If the listing modified date is older than the translations, refresh the listing.

Includes refactoring that breaks down existing functionality into smaller methods.

DAH-2556

Checklist before requesting review

Version Control

  • branch name begins with angular if it contains updates to Angular code
  • branch name contains the Jira ticket number
  • PR name follows type: TICKET-NUMBER Description format, e.g. feat: DAH-123 New Feature

Code quality

Review instructions

  • instructions specify which environment(s) it applies to
  • instructions have already been performed at least once
  • instructions can be followed by PA testers
  • instructions specify if it can only be followed by an engineer

Request review

  • PR has needs review label
  • Use Housing Eng group to automatically assign reviewers, and/or assign specific engineers
  • If time sensitive, notify engineers in Slack

Review

For engineers locally:
Run memcached
Run yarn start
Run bundle exec rake translate:subscribe

Load a listing detail page -> since a listing will initially have no translations cache, this should trigger the translations to be processed.

Refresh page -> The listing's translation cache should be valid -> no translations call

Update a field in Salesforce -> Event processor updates cache -> LastModifiedDates should match when the listing cache is refreshed next

Refresh page -> (May refresh listing) -> Should not reprocess translations

Kill the Eventsubscriber process -> Update a field in salesforce -> No translation call

Refresh page -> LastModifiedDates should be different -> translations are reprocessed

@cade-exygy cade-exygy added the needs review Pull request needs review label Sep 6, 2024
@cade-exygy cade-exygy changed the title feat: check translations cache LastModifiedDate feat: DAH-2556 check translations cache LastModifiedDate Sep 6, 2024
@hshaosf hshaosf temporarily deployed to dahlia-webapp-pr-2283 September 6, 2024 19:22 Inactive
def cache_listing_translations(listing_id, keys, translations, last_modified)
Force::Request.new(parse_response: true).cached_get(
"/ListingDetails/#{CGI.escape(listing_id)}", nil, true
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: It's not entirely clear to me why cached_get was added here. Don't we do that earlier in the chain of method calls?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding it here would guarantee that the listing is refreshed anytime we run translations for a listing. (Either by event trigger or a full process call).

This solves the issue of https://sfgovdt.jira.com/browse/DAH-2575, as well as ensures the last modified date of the listing is immediately correct for when the listing service is hit.

Testing through the different cases in the review instructions may provide some clarity to my above open question.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on our convo in the translations sync, can we remove the force update listing call here?

@@ -53,7 +54,10 @@ def self.listing(id, opts = {})
listing
end

def self.translations_valid?(listing_translations)
def self.translations_valid?(listing_translations, listing_last_modified)
return false unless listing_translations[:LastModifiedDate] == listing_last_modified
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: This seems to duplicate the method CacheService#listing_unchanged?, which gets run every 10 minutes via the prefetch job. Does this make the prefetch job redundant now? I.e. do we still need a background process to update caches for all modified listings every 10 minutes, when we are now doing it on every visit to a listing details page?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good question and is related to the open question in the description.

This function compares the listing LastModifiedDate and the translations cache LastModifiedDate.

This returns false when:

  • A listing has been updated but the event subscriber failed to update the translations.

  • With the force=true call in the google_translate_service, we should always refresh the listing object when we update translations. So in theory, the LastModifiedDate of the listing should be guaranteed to be accurate for the purposes of the comparison.

If this function is false, then we process translations -> force update the listing -> translate all fields.

So yes, if the event subscriber is working -> then everytime we update a listing -> we translate and force update the listing. We could probably remove the prefetch job in this case.

If the event subscriber is down for some reason, then we would not update translations or force refresh the listing.
If we disable the prefetch job under this scenario: The listing wouldn't reflect the new changes until a manual force refresh. And we wouldn't catch the disagreement on the listing service call.

We could remove the force=true call from the google translate service, and then this check in the listing detail would return false due to the translations being newer. (Unless the prefetch job gets it first). The intended behavior would be to refresh the listing. Then the dates should match.

This ticket primarily aims to resolve the potential complications of the event subscriber failing. We do plan on implementing event replays so that is a future consideration.

Hopefully this provides some insight into the potential paths forward. We could keep the prefetch job and introduce some redundancy that would cover all of the potential scenarios.

We could remove the force=true call from the google_translation service, and just let the listing_service be responsible for updating the listing cache. Perhaps that would simplify things for us.

Maybe we should make the LastModifiedDate comparison have two outcomes: if the listing date is older than the translations -> refresh the listing. If the translation date is older than the listing -> refresh the translations.

@cade-exygy cade-exygy temporarily deployed to dahlia-webapp-pr-2283 September 8, 2024 17:07 Inactive
@cade-exygy cade-exygy marked this pull request as ready for review September 11, 2024 16:44
@jimlin-sfgov jimlin-sfgov mentioned this pull request Sep 11, 2024
18 tasks
@cade-exygy cade-exygy temporarily deployed to dahlia-webapp-pr-2283 September 11, 2024 19:08 Inactive
if listing_is_outdated?(listing_translations[:LastModifiedDate],
listing['LastModifiedDate'])
Rails.logger.info("Listing is outdated for #{listing_id}")
refresh_listing_cache(listing_id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we discussed not refreshing the listing cache in the translations flow?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We removed it from the translation service. We do want to refresh the listing when we know that the listing is outdated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That wasn't my understanding. I thought we wanted to avoid updating the listing cache on page load - if we update on page load than the cache is moot.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I discussed this above:

Maybe we should make the LastModifiedDate comparison have two outcomes: if the listing date is older than the translations -> refresh the listing. If the translation date is older than the listing -> refresh the translations.

But it is a fair question. Relates to this ticket as well: https://sfgovdt.jira.com/browse/DAH-2575

We can wait for the 10 minutes: which means english values will be out of date until a listing recache if we update translations via an event. If we have an event miss, then we would have to wait the 10 minutes to resolve those differences as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed in a meeting and decided to move forward with updating the listing cache!

@cade-exygy cade-exygy temporarily deployed to dahlia-webapp-pr-2283 September 12, 2024 17:12 Inactive
@cade-exygy cade-exygy temporarily deployed to dahlia-webapp-pr-2283 September 12, 2024 17:13 Inactive
@cade-exygy cade-exygy temporarily deployed to dahlia-webapp-pr-2283 September 12, 2024 17:15 Inactive
@cade-exygy cade-exygy temporarily deployed to dahlia-webapp-pr-2283 September 12, 2024 17:31 Inactive
@cade-exygy cade-exygy temporarily deployed to dahlia-webapp-pr-2283 September 16, 2024 18:18 Inactive
Copy link
Contributor

@tallulahkay tallulahkay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good!! Do you think it's possible to get the test coverage up?

@cade-exygy cade-exygy temporarily deployed to dahlia-webapp-pr-2283 September 17, 2024 16:25 Inactive
@cade-exygy cade-exygy temporarily deployed to dahlia-webapp-pr-2283 September 17, 2024 16:25 Inactive
@cade-exygy cade-exygy temporarily deployed to dahlia-webapp-pr-2283 September 17, 2024 16:37 Inactive
@cade-exygy cade-exygy temporarily deployed to dahlia-webapp-pr-2283 September 17, 2024 16:59 Inactive
@cade-exygy cade-exygy merged commit 456c5fc into main Sep 17, 2024
17 of 18 checks passed
@cade-exygy cade-exygy deleted the DAH-2556-handle-outdated-translation-cache branch September 17, 2024 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs review Pull request needs review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants