def _update_database(self):
"""
Determines if a database update is needed and if so downloads a new one from MaxMind
"""
if not self._download_database:
logger.warning("_update_database called while in pre-specified database"
" mode... returning without updates...")
return
logger.info("Checking for MaxMind database updates...")
try:
if not self._is_update_needed():
logger.info("No database updates to retrieve.")
return
logger.info("Retrieving MaxMind Database...")
response = requests.get(self.MAXMIND_FREE_DB_URL)
response.raise_for_status()
logger.info('Retrieved MaxMind database.')
data = gzip.GzipFile(fileobj=StringIO.StringIO(response.content))
# Write the database to a temporary file
fd, file_path = tempfile.mkstemp()
with os.fdopen(fd, 'wb') as temp_file:
temp_file.write(data.read())
self._swap_database(file_path)
logger.info("MaxMind database updated.")
except:
logger.exception("Failed to update MaxMind database.")
finally:
# Schedule this function to run again in the configured update interval
self._update_thread = Timer(self._update_interval * 60 * 60, self._update_database)
self._update_thread.daemon = True
self._update_thread.start()
评论列表
文章目录