Skip to content

Commit

Permalink
use a temp file for cache downloads to prevent data loss when cache f…
Browse files Browse the repository at this point in the history
…ailed (#8)
  • Loading branch information
Ivan-1F committed Jun 26, 2024
1 parent e454473 commit 8ebd743
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions mcdreforged_plugin_manager/storage/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def stop(self):

class Cache(PluginStorage):
CACHE_PATH = os.path.join(psi.get_data_folder(), 'everything.json')
TMP_CACHE_PATH = os.path.join(psi.get_data_folder(), 'everything.json.tmp')

@new_thread('MPMCache')
def cache(self):
Expand All @@ -53,13 +54,15 @@ def cache(self):
psi.logger.info(tr('cache.cache'))

try:
# remove cache if exist
if os.path.exists(self.CACHE_PATH) and os.path.isfile(self.CACHE_PATH):
os.remove(self.CACHE_PATH)
download_file(config.source, self.CACHE_PATH)
download_file(config.source, self.TMP_CACHE_PATH)
except Exception as e:
psi.logger.warning(tr('cache.exception', e))
else:
# remove cache if exist
if os.path.exists(self.CACHE_PATH) and os.path.isfile(self.CACHE_PATH):
os.remove(self.CACHE_PATH)
os.rename(self.TMP_CACHE_PATH, self.CACHE_PATH)

self.__load()
psi.logger.info(tr('cache.cached', self.plugin_amount - before))

Expand Down

0 comments on commit 8ebd743

Please sign in to comment.