Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

cache API exercises task added

Bonicki Wojciechdcb0dd1c49d4
diff --git a/wger/exercises/cache.py b/wger/exercises/cache.py
new file mode 100644
index 000000000..deda67bd3
--- /dev/null
+++ b/wger/exercises/cache.py
@@ -0,0 +1,46 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+from typing import Callable
+# Standard Library
+from wger.exercises.models import (
+ Exercise,
+)
+from wger.utils.cache import reset_exercise_api_cache
+from wger.exercises.api.serializers import ExerciseInfoSerializer
+
+
+def cache_exercise(exercise: Exercise, force=False, print_fn: Callable=print, style_fn: Callable=lambda x: x):
+ """
+ Caches a provided exercise.
+ """
+ if force:
+ print_fn(f"Force updating cache for exercise base {exercise.uuid}")
+ reset_exercise_api_cache(exercise.uuid)
+ else:
+ print_fn(f"Warming cache for exercise base {exercise.uuid}")
+
+ serializer = ExerciseInfoSerializer(exercise)
+ serializer.data
+
+
+def cache_api_exercises(
+ print_fn: Callable,
+ force: bool,
+ style_fn: Callable=lambda x: x,
+):
+ print_fn("*** Caching API exercises ***")
+ for exercise in Exercise.with_translations.all():
+ cache_exercise(exercise, force, print_fn, style_fn)
+ print_fn(style_fn("Exercises cached!\n"))
diff --git a/wger/exercises/management/commands/warmup-exercise-api-cache.py b/wger/exercises/management/commands/warmup-exercise-api-cache.py
index 2483e5fa3..a73200cb8 100644
--- a/wger/exercises/management/commands/warmup-exercise-api-cache.py
+++ b/wger/exercises/management/commands/warmup-exercise-api-cache.py
@@ -16,10 +16,8 @@
from django.core.management.base import BaseCommand
# wger
-from wger.exercises.api.serializers import ExerciseInfoSerializer
from wger.exercises.models import Exercise
-from wger.utils.cache import reset_exercise_api_cache
-
+from wger.exercises.cache import cache_exercise
class Command(BaseCommand):
"""
@@ -48,20 +46,9 @@ class Command(BaseCommand):
if exercise_id:
exercise = Exercise.objects.get(pk=exercise_id)
- self.handle_cache(exercise, force)
+ cache_exercise(exercise, force, self.stdout.write)
return
for exercise in Exercise.with_translations.all():
- self.handle_cache(exercise, force)
-
- def handle_cache(self, exercise: Exercise, force: bool):
- if force:
- self.stdout.write(f'Force updating cache for exercise base {exercise.uuid}')
- else:
- self.stdout.write(f'Warming cache for exercise base {exercise.uuid}')
-
- if force:
- reset_exercise_api_cache(exercise.uuid)
+ cache_exercise(exercise, force, self.stdout.write)
- serializer = ExerciseInfoSerializer(exercise)
- serializer.data
diff --git a/wger/exercises/tasks.py b/wger/exercises/tasks.py
index de5d25683..6113f1668 100644
--- a/wger/exercises/tasks.py
+++ b/wger/exercises/tasks.py
@@ -35,6 +35,7 @@ from wger.exercises.sync import (
sync_licenses,
sync_muscles,
)
+from wger.exercises.cache import cache_api_exercises
logger = logging.getLogger(__name__)
@@ -70,9 +71,18 @@ def sync_videos_task():
download_exercise_videos(logger.info)
+def cache_api_exercises_task():
+ """
+ Fetches all exercises from database and caches them.
+ """
+ force = settings.WGER_SETTINGS["CACHE_API_EXERCISES_CELERY_FORCE_UPDATE"]
+ cache_api_exercises(logger.info, force)
+
+
@app.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
- if settings.WGER_SETTINGS['SYNC_EXERCISES_CELERY']:
+ if settings.WGER_SETTINGS["SYNC_EXERCISES_CELERY"]:
sender.add_periodic_task(
crontab(
hour=str(random.randint(0, 23)),
@@ -80,10 +90,10 @@ def setup_periodic_tasks(sender, **kwargs):
day_of_week=str(random.randint(0, 6)),
),
sync_exercises_task.s(),
- name='Sync exercises',
+ name="Sync exercises",
)
- if settings.WGER_SETTINGS['SYNC_EXERCISE_IMAGES_CELERY']:
+ if settings.WGER_SETTINGS["SYNC_EXERCISE_IMAGES_CELERY"]:
sender.add_periodic_task(
crontab(
hour=str(random.randint(0, 23)),
@@ -91,10 +101,10 @@ def setup_periodic_tasks(sender, **kwargs):
day_of_week=str(random.randint(0, 6)),
),
sync_images_task.s(),
- name='Sync exercise images',
+ name="Sync exercise images",
)
- if settings.WGER_SETTINGS['SYNC_EXERCISE_VIDEOS_CELERY']:
+ if settings.WGER_SETTINGS["SYNC_EXERCISE_VIDEOS_CELERY"]:
sender.add_periodic_task(
crontab(
hour=str(random.randint(0, 23)),
@@ -102,5 +112,16 @@ def setup_periodic_tasks(sender, **kwargs):
day_of_week=str(random.randint(0, 6)),
),
sync_videos_task.s(),
- name='Sync exercise videos',
+ name="Sync exercise videos",
+ )
+
+ if settings.WGER_SETTINGS["CACHE_API_EXERCISES_CELERY"]:
+ sender.add_periodic_task(
+ crontab(
+ hour=str(random.randint(0, 23)),
+ minute=str(random.randint(0, 59)),
+ day_of_week=str(random.randint(0, 6)),
+ ),
+ cache_api_exercises_task.s(),
+ name="Cache API exercises",
)
diff --git a/wger/settings_global.py b/wger/settings_global.py
index 9041763c1..59d6b814a 100644
--- a/wger/settings_global.py
+++ b/wger/settings_global.py
@@ -549,6 +549,8 @@ WGER_SETTINGS = {
'SYNC_EXERCISE_VIDEOS_CELERY': False,
'SYNC_INGREDIENTS_CELERY': False,
'SYNC_OFF_DAILY_DELTA_CELERY': False,
+ 'CACHE_API_EXERCISES_CELERY': True,
+ 'CACHE_API_EXERCISES_CELERY_FORCE_UPDATE': True,
'TWITTER': False,
'MASTODON': 'https://fosstodon.org/@wger',
'USE_CELERY': False,