Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[optimization] record CountedUsage in a celery task #10091

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions api/metrics/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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):
Expand Down
10 changes: 10 additions & 0 deletions osf/metrics/tasks.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions website/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down