From f40eae2908bf6a7c1d7a9d62aa065402e614c32a Mon Sep 17 00:00:00 2001 From: Abram Booth Date: Wed, 26 Oct 2022 16:07:45 -0400 Subject: [PATCH] record CountedUsage in a celery task --- api/metrics/serializers.py | 23 +++++++++++++++-------- osf/metrics/tasks.py | 10 ++++++++++ website/settings/defaults.py | 1 + 3 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 osf/metrics/tasks.py diff --git a/api/metrics/serializers.py b/api/metrics/serializers.py index 8c65db4c300..b835b8563c7 100644 --- a/api/metrics/serializers.py +++ b/api/metrics/serializers.py @@ -4,7 +4,9 @@ from api.base.serializers import BaseAPISerializer from api.base.utils import absolute_reverse +from framework.celery_tasks.handlers import enqueue_task from osf.metrics.counted_usage import CountedUsage +from osf.metrics.tasks import record_counted_usage from website import settings as website_settings @@ -60,14 +62,19 @@ def validate(self, data): return data def create(self, validated_data): - return CountedUsage.record( - platform_iri=website_settings.DOMAIN, - provider_id=validated_data.get('provider_id'), - item_guid=validated_data.get('item_guid'), - session_id=validated_data['session_id'], # must be provided by the view - action_labels=validated_data.get('action_labels'), - pageview_info=validated_data.get('pageview_info'), - ) + record_kwargs = { # passed to CountedUsage.record() + 'platform_iri': website_settings.DOMAIN, + 'provider_id': validated_data.get('provider_id'), + 'item_guid': validated_data.get('item_guid'), + 'session_id': validated_data['session_id'], # must be provided by the view + 'action_labels': validated_data.get('action_labels'), + 'pageview_info': validated_data.get('pageview_info'), + } + if website_settings.USE_CELERY: + enqueue_task(record_counted_usage.s(record_kwargs)) + else: + record_counted_usage(record_kwargs) + return {} class ReportNameSerializer(ser.BaseSerializer): diff --git a/osf/metrics/tasks.py b/osf/metrics/tasks.py new file mode 100644 index 00000000000..2070c3048e7 --- /dev/null +++ b/osf/metrics/tasks.py @@ -0,0 +1,10 @@ +from framework.celery_tasks import app as celery_app +from osf.metrics.counted_usage import CountedUsage + + +@celery_app.task(bind=True, max_retries=5, default_retry_delay=60) +def record_counted_usage(self, record_kwargs): + try: + CountedUsage.record(**record_kwargs) + except Exception as exc: + self.retry(exc=exc) diff --git a/website/settings/defaults.py b/website/settings/defaults.py index 1fa3882e0b7..e75b5f96b22 100644 --- a/website/settings/defaults.py +++ b/website/settings/defaults.py @@ -430,6 +430,7 @@ class CeleryConfig: 'osf.management.commands.populate_branched_from', 'osf.management.commands.cumulative_plos_metrics', 'osf.management.commands.daily_reporters_go', + 'osf.metrics.tasks', } med_pri_modules = {