Skip to content

Commit

Permalink
add background task to periodically invalidate device cache (#242)
Browse files Browse the repository at this point in the history
* add background task to periodically invalidate device cache

* fix test lib dependency version issue

* code review updates
  • Loading branch information
edaniszewski committed Feb 1, 2019
1 parent 1de6a1e commit 0245ca3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
24 changes: 23 additions & 1 deletion synse/factory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Factory for creating Synse Server Sanic application instances."""
# pylint: disable=unused-variable,unused-argument

import asyncio
import os

from sanic import Sanic
Expand All @@ -9,7 +10,7 @@

import synse
from synse import config, errors, utils
from synse.cache import configure_cache
from synse.cache import clear_all_meta_caches, configure_cache
from synse.log import LOGGING, logger, setup_logger
from synse.response import json
from synse.routes import aliases, base, core
Expand Down Expand Up @@ -62,6 +63,9 @@ def make_app():

configure_cache()

# Add background tasks
app.add_task(periodic_cache_invalidation)

# Log out metadata for Synse Server and the application configuration
logger.info('Synse Server:')
logger.info(' version: {}'.format(synse.__version__))
Expand All @@ -72,6 +76,24 @@ def make_app():
return app


async def periodic_cache_invalidation():
"""Periodically invalidate the caches so they are rebuilt."""
interval = 3 * 60 # 3 minutes

while True:
await asyncio.sleep(interval)
logger.info('task [periodic cache invalidation]: Clearing device caches')

try:
await clear_all_meta_caches()
except Exception as e:
logger.error(
'task [periodic cache invalidation]: Failed to clear device caches, '
'will try again in {}s: {}'
.format(interval, e)
)


def _disable_favicon(app):
"""Return empty response when looking for favicon.
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ deps=
asynctest
pytest-asyncio
isort>=4.2.5
pytest>=3.6
; FIXME: pytest must be <4.0.0 for pytest-profiling
pytest<4.0.0
pytest-cov>=2.5.1
pytest-html>=1.14.2
pytest-mock>=1.6.0
Expand Down

0 comments on commit 0245ca3

Please sign in to comment.