Skip to content

Cache calls to external API services for one week#4020

Merged
flodolo merged 6 commits into
mozilla:mainfrom
flodolo:issue2664
Mar 23, 2026
Merged

Cache calls to external API services for one week#4020
flodolo merged 6 commits into
mozilla:mainfrom
flodolo:issue2664

Conversation

@flodolo
Copy link
Copy Markdown
Collaborator

@flodolo flodolo commented Mar 14, 2026

Fixes #2664

@flodolo flodolo force-pushed the issue2664 branch 2 times, most recently from ff5afb5 to 5e9d648 Compare March 17, 2026 05:48
Copy link
Copy Markdown
Collaborator

@mathjazz mathjazz left a comment

Choose a reason for hiding this comment

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

Looks good!

Left some suggestions to improve readability and reduce cache footprint.

Comment thread pontoon/machinery/utils.py Outdated
Comment thread pontoon/machinery/utils.py Outdated
Comment thread pontoon/machinery/utils.py Outdated
Comment thread pontoon/settings/base.py Outdated
Comment thread pontoon/machinery/utils.py Outdated
Comment thread pontoon/machinery/utils.py Outdated
Comment thread pontoon/machinery/views.py Outdated
Comment thread pontoon/machinery/utils.py Outdated
Comment thread pontoon/machinery/utils.py Outdated
Comment thread pontoon/machinery/views.py Outdated
Comment thread pontoon/machinery/openai_service.py Outdated
Comment thread pontoon/machinery/utils.py Outdated
@flodolo
Copy link
Copy Markdown
Collaborator Author

flodolo commented Mar 23, 2026

Will flag again once ready for review, I think I'm caching unnecessarily in one place.

@flodolo
Copy link
Copy Markdown
Collaborator Author

flodolo commented Mar 23, 2026

OK, kept all changes from last review in a single commit
3b58e80

All the code is now caching only the translation, not the structure of the response.

@flodolo flodolo requested a review from mathjazz March 23, 2026 13:47
Copy link
Copy Markdown
Collaborator

@mathjazz mathjazz left a comment

Choose a reason for hiding this comment

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

Looks good, nice work!

Just realized that we Google and Microsoft utils functions could return just the translation data (like the OpenAI service does), in which case the code handling caching would be more readable and we'd take care of the JSON response format in the view.

But I'll leave the decision to implement that or merge as is to you.

@flodolo
Copy link
Copy Markdown
Collaborator Author

flodolo commented Mar 23, 2026

Sounds good. Let me take a look when I'm done with meetings.

@flodolo flodolo force-pushed the issue2664 branch 3 times, most recently from 22fb04d to b83e1b5 Compare March 23, 2026 16:49
Also move code handling MS query to utils
@flodolo
Copy link
Copy Markdown
Collaborator Author

flodolo commented Mar 23, 2026

But I'll leave the decision to implement that or merge as is to you.

It's a bit larger than I expected, since I had to deal with the error statuses
2b07cdc (this PR)

Do you want to review the changes?

The code is now raising the exception, and using that in the view to log the error. Also move the MS code to utils.py

Comment thread pontoon/machinery/utils.py Outdated
"status": False,
"message": f"{e}",
}
client = translate.TranslationServiceClient()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why do we no longer cache the error here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Caching the error = wrapping in try/except?

Before we were catching DefaultCredentialsError. This would propagate to _machinery_error_response() and still be logged with a generic error.

I'm going to wrap it back and use a clearer error message.

Let me know if that makes sense.

Copy link
Copy Markdown
Collaborator

@mathjazz mathjazz Mar 23, 2026

Choose a reason for hiding this comment

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

Ugh, I meant "catch". :) Thanks.

Comment thread pontoon/machinery/utils.py Outdated
Comment thread pontoon/machinery/views.py Outdated
@flodolo flodolo merged commit e04d98e into mozilla:main Mar 23, 2026
3 checks passed
@flodolo flodolo deleted the issue2664 branch March 23, 2026 18:22
flodolo added a commit to flodolo/pontoon that referenced this pull request Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache API requests to external translation services

2 participants