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

Add-on store: Improve refreshing the cache #15071

Merged
merged 18 commits into from
Jul 6, 2023

Conversation

nvdaes
Copy link
Collaborator

@nvdaes nvdaes commented Jun 28, 2023

  • Refresh cache based on cache Hash from NV Access server
  • Don't use datetime to refresh cache

Link to issue number:

Fixes #15034
Fixes #15077

Summary of the issue:

The store data cache is refreshed every 6 hours. This may not be enough and it's desirable to add a mechanism to refresh it making a small request to the server, so we can request data just when a new commit has been done to the views branch of the nvaccess/addon-datastore repo.

Description of user facing changes

When opening the add-on store, if there has been changes on the server, the cached data should be updated.

Description of development approach

  • In dataManager, a function has been added to get the cacheHash from NV Access server.
  • References to date, including parameters of functions, have been replaced with variables which hold the value of the cache hash.
  • try... except clauses have been used to ensure that .json files which store cache data are valid,though perhaps a jsonschema will be more robust and will allow to control data types.
  • network.py has been tweaked.

Testing strategy:

  • Wifi has been deactivated and the store has been opened. When pressing tab to select available and updatable add-ons, and also when the checkbox to get incompatible add-ons has been checked, error messages have been displayed, since NVDA tried to get the hash.
    Also, manual tests have confirmed that new add-ons sent to the store have been retrieved.

Known issues with pull request:

When the checkbox to get incompatible add-ons is checked, the add-ons list is refreshed and this may take some seconds.

Change log entries:

n/a
New features
Changes
Bug fixes
For Developers

Code Review Checklist:

  • Pull Request description:
    • description is up to date
    • change log entries
  • Testing:
    • Unit tests
    • System (end to end) tests
    • Manual testing
  • API is compatible with existing add-ons.
  • Documentation:
    • User Documentation
    • Developer / Technical Documentation
    • Context sensitive help for GUI changes
  • UX of all users considered:
    • Speech
    • Braille
    • Low Vision
    • Different web browsers
    • Localization in other languages / culture than English
  • Security precautions taken.

@nvdaes
Copy link
Collaborator Author

nvdaes commented Jun 28, 2023

cc: @josephsl

@josephsl
Copy link
Collaborator

josephsl commented Jun 28, 2023 via email

Co-authored-by: Leonard de Ruijter <leonardder@users.noreply.github.com>
@nvdaes
Copy link
Collaborator Author

nvdaes commented Jun 29, 2023

Thanks @josephsl and @LeonarddeR for your comments.

@nvdaes
Copy link
Collaborator Author

nvdaes commented Jul 1, 2023

I think that this is ready for review.

@nvdaes nvdaes marked this pull request as ready for review July 1, 2023 03:34
@nvdaes nvdaes requested a review from a team as a code owner July 1, 2023 03:34
@nvdaes nvdaes requested a review from seanbudd July 1, 2023 03:34
@seanbudd seanbudd changed the title refreshCache Add-on store: Improve refreshing the cache Jul 2, 2023
source/_addonStore/dataManager.py Outdated Show resolved Hide resolved
source/_addonStore/dataManager.py Show resolved Hide resolved
source/_addonStore/dataManager.py Outdated Show resolved Hide resolved
source/_addonStore/models/addon.py Outdated Show resolved Hide resolved
source/_addonStore/network.py Outdated Show resolved Hide resolved
source/_addonStore/dataManager.py Outdated Show resolved Hide resolved
source/_addonStore/dataManager.py Outdated Show resolved Hide resolved
source/_addonStore/dataManager.py Outdated Show resolved Hide resolved
source/_addonStore/dataManager.py Show resolved Hide resolved
@seanbudd seanbudd marked this pull request as draft July 3, 2023 00:05
@@ -142,10 +157,10 @@ def _getCachedAddonData(self, cacheFilePath: str) -> Optional[CachedAddonsModel]
cacheData = json.load(cacheFile)
Copy link
Member

@seanbudd seanbudd Jul 3, 2023

Choose a reason for hiding this comment

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

Could you also look at fixing #15077, at the same time as #15071 (comment)

Copy link
Collaborator

Choose a reason for hiding this comment

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

@nvdaes, you have asked me to test this PR in #15077 (comment).

Testing from source on latest commit (dfe1b22) with the file from #15077, the issue is still present.

You should add a try/except around the two following lines in _getCachedAddonData:

 		with open(cacheFilePath, 'r') as cacheFile:
			cacheData = json.load(cacheFile)

@nvdaes
Copy link
Collaborator Author

nvdaes commented Jul 3, 2023

  • cacheHash = cacheData.get("cacheHash")
    

is there a reason get is used here?

This was to avoid an error if the "cacheHash" key doesn't exist, but now I'll see if I can fix this.

@nvdaes
Copy link
Collaborator Author

nvdaes commented Jul 3, 2023

@seanbudd Hope your comments are correctly addressed. Imo it would be better to create a jsonSchema for a better maintenance and control of type values in future, but for now I've used try...except KeyError to fix #15077

@nvdaes
Copy link
Collaborator Author

nvdaes commented Jul 3, 2023

I've made another commit and I'll way for further comments @seanbudd

@nvdaes nvdaes requested a review from seanbudd July 3, 2023 10:04
@nvdaes nvdaes marked this pull request as ready for review July 3, 2023 10:37
@nvdaes
Copy link
Collaborator Author

nvdaes commented Jul 3, 2023

Hi @CyrilleB79 . You wrote:

You should add a try/except around the two following lines in _getCachedAddonData:

I've tried to address your comment. Please try again if you can.

@CyrilleB79
Copy link
Collaborator

Hi @CyrilleB79 . You wrote:

You should add a try/except around the two following lines in _getCachedAddonData:

I've tried to address your comment. Please try again if you can.

Yes NVDA loads successfully now and the file seems to be recreated.

Having a look at the code, I would say that it may be valuable to log the exception or at least a debugWarning message. Indeed it's not expected to fail loading the file:

  • Either someone has modified it manually; in this case there is nothing to do.
  • Or it's an old incompatible version coming from an older alpha; nothing to do either.
  • Or there is a bug in our current code and logging a warning would help to debug it.

I wonder if making a backup of this badly formatted file before overwriting it would be needed in case of a bug to help debugging it.

Surely the current PR fix the startup issue described in #15077. But is there an issue remaining? Why was a badly formatted file created initially? @seanbudd what is your opinion on this?

@nvdaes
Copy link
Collaborator Author

nvdaes commented Jul 3, 2023

Thanks Cyrille: I had thought about this. Let"s wait for Sean feedback in case he has some preference about the kind of warning that maybe writen in the log.

@seanbudd
Copy link
Member

seanbudd commented Jul 4, 2023

I wonder if making a backup of this badly formatted file before overwriting it would be needed in case of a bug to help debugging it.

I think this is unnecessary as these caches are not an important state file - and can easily be wiped and replaced

@seanbudd
Copy link
Member

seanbudd commented Jul 4, 2023

@nvdaes - could you add references to #15077 and otherwise update the PR descripition?

Copy link
Member

@seanbudd seanbudd left a comment

Choose a reason for hiding this comment

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

The changes generally look good to me

source/_addonStore/network.py Outdated Show resolved Hide resolved
source/_addonStore/network.py Outdated Show resolved Hide resolved
source/_addonStore/network.py Outdated Show resolved Hide resolved
source/_addonStore/network.py Outdated Show resolved Hide resolved
source/_addonStore/dataManager.py Show resolved Hide resolved
source/_addonStore/dataManager.py Show resolved Hide resolved
@nvdaes
Copy link
Collaborator Author

nvdaes commented Jul 4, 2023 via email

@seanbudd seanbudd marked this pull request as draft July 4, 2023 05:07
@AppVeyorBot
Copy link

See test results for failed build of commit d9a8f1ae2c

@nvdaes
Copy link
Collaborator Author

nvdaes commented Jul 4, 2023

@seanbudd 24o53:

In case it was confusing the suggestion here as to remove the unused variable _baseURL

I think it's all done.

@nvdaes nvdaes requested a review from seanbudd July 4, 2023 05:37
@nvdaes nvdaes marked this pull request as ready for review July 4, 2023 06:21
Copy link
Member

@seanbudd seanbudd 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 to me, just 1 minor suggestion

shouldRefreshData = (
not self._compatibleAddonCache
or self._compatibleAddonCache.nvdaAPIVersion != addonAPIVersion.CURRENT
or _DataManager._cachePeriod < (datetime.now() - self._compatibleAddonCache.cachedAt)
or cacheHash and (self._compatibleAddonCache.cacheHash != cacheHash)
Copy link
Member

Choose a reason for hiding this comment

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

I think we should always refresh the cache if fetching the cacheHash fails.
This assumes we can treat "None" as a valid cacheHash,

Suggested change
or cacheHash and (self._compatibleAddonCache.cacheHash != cacheHash)
or cacheHash is None
or self._compatibleAddonCache.cacheHash != cacheHash

shouldRefreshData = (
not self._latestAddonCache
or _DataManager._cachePeriod < (datetime.now() - self._latestAddonCache.cachedAt)
or cacheHash and (self._latestAddonCache.cacheHash != cacheHash)
Copy link
Member

Choose a reason for hiding this comment

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

same here re: cacheHash being None

Suggested change
or cacheHash and (self._latestAddonCache.cacheHash != cacheHash)
or cacheHash is None
or self._compatibleAddonCache.cacheHash != cacheHash

@seanbudd seanbudd marked this pull request as draft July 6, 2023 02:42
@nvdaes nvdaes marked this pull request as ready for review July 6, 2023 03:19
@nvdaes nvdaes requested a review from seanbudd July 6, 2023 03:20
Copy link
Member

@seanbudd seanbudd left a comment

Choose a reason for hiding this comment

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

thanks @nvdaes

@AppVeyorBot
Copy link

See test results for failed build of commit 28d1edcadb

@seanbudd seanbudd merged commit 54a4ec5 into nvaccess:master Jul 6, 2023
@nvaccessAuto nvaccessAuto added this to the 2023.2 milestone Jul 6, 2023
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.

Critical error when starting alpha Add-on store: add ability to manually refresh store data cache
7 participants