-
Notifications
You must be signed in to change notification settings - Fork 358
[ENG-9831] Cedar metadata record creation/deletion in share for collection submission #11710
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
Open
ihorsokhanexoft
wants to merge
10
commits into
CenterForOpenScience:feature/es2-consolidation
Choose a base branch
from
ihorsokhanexoft:feature/ENG-9831
base: feature/es2-consolidation
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3634ac5
added cedar metadata record creation/deletion in share for collection…
ihorsokhanexoft f52c484
removed validate_required_metadata redefinition
ihorsokhanexoft ec8fb9f
enqueue cedar record update in share
ihorsokhanexoft 06ef868
added focus_iri to shtrove POST request
ihorsokhanexoft 52c8fed
separate cedar record identifier to avoid potential issues
ihorsokhanexoft 13ccfd3
separate cedar record serialization
ihorsokhanexoft b1630a1
added retry, moved tasks to task__update_share
ihorsokhanexoft 3561ed0
added more tests, make delete cedar record function working without c…
ihorsokhanexoft ff4876f
fixed tests
ihorsokhanexoft c6d35f4
simplified turtle test and task__update_share
ihorsokhanexoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,10 @@ | |
| """ | ||
| from http import HTTPStatus | ||
| import logging | ||
| from rdflib import Graph | ||
|
|
||
| from django.apps import apps | ||
| from django.db.models import Q | ||
| from celery.utils.time import get_exponential_backoff_interval | ||
| import requests | ||
|
|
||
|
|
@@ -14,6 +16,7 @@ | |
| from framework.encryption import ensure_bytes | ||
| from framework.sentry import log_exception | ||
| from osf.external.gravy_valet.exceptions import GVException | ||
| from osf.metadata.rdfutils import OSF | ||
| from osf.metadata.osf_gathering import ( | ||
| OsfmapPartition, | ||
| pls_get_magic_metadata_basket, | ||
|
|
@@ -64,6 +67,105 @@ def _enqueue_update_share(osfresource): | |
| enqueue_task(task__update_share.s(_osfguid_value)) | ||
|
|
||
|
|
||
| def retry_shtrove_request(self_celery_task, _response): | ||
| try: | ||
| _response.raise_for_status() | ||
| except Exception as e: | ||
| log_exception(e) | ||
| if _response.status_code == HTTPStatus.TOO_MANY_REQUESTS: | ||
| retry_after = _response.headers.get('Retry-After') | ||
| try: | ||
| countdown = int(retry_after) | ||
| except (TypeError, ValueError): | ||
| retries = getattr(self_celery_task.request, 'retries', 0) | ||
| countdown = get_exponential_backoff_interval( | ||
| factor=4, | ||
| retries=retries, | ||
| maximum=2 * 60, | ||
| full_jitter=True, | ||
| ) | ||
| raise self_celery_task.retry(exc=e, countdown=countdown) | ||
|
|
||
| raise self_celery_task.retry(exc=e) | ||
|
|
||
|
|
||
| def cedar_record_to_turtle(referent, cedar_record): | ||
| graph = Graph() | ||
| iri = referent.get_semantic_iri() | ||
| full_metadata = { | ||
| '@id': iri, | ||
| OSF.hasCedarRecord: cedar_record.metadata, | ||
| } | ||
| graph.parse(data=full_metadata, format='json-ld') | ||
|
|
||
| return graph.serialize(format='turtle') | ||
|
|
||
|
|
||
| @celery_app.task(bind=True) | ||
| def share_update_cedar_metadata_record(self, referent_id, cedar_record_pk): | ||
| from osf.models import Guid, CedarMetadataRecord | ||
|
|
||
| guid = Guid.load(referent_id) | ||
| referent = guid.referent | ||
| cedar_record = CedarMetadataRecord.objects.filter(pk=cedar_record_pk).first() | ||
| if not cedar_record: | ||
| return | ||
|
|
||
| serialized_data = cedar_record_to_turtle(referent, cedar_record) | ||
| response = requests.post( | ||
| shtrove_ingest_url(), | ||
| params={ | ||
| 'focus_iri': referent.get_semantic_iri(), | ||
| 'record_identifier': _shtrove_cedar_record_identifier(cedar_record._id, cedar_record.template.cedar_id), | ||
| 'is_supplementary': True, | ||
| }, | ||
| headers={ | ||
| 'Content-Type': 'text/turtle; charset=utf-8', | ||
| **_shtrove_auth_headers(referent), | ||
| }, | ||
| data=ensure_bytes(serialized_data), | ||
| ) | ||
| retry_shtrove_request(self, response) | ||
|
|
||
|
|
||
| @celery_app.task(bind=True) | ||
| def share_delete_cedar_metadata_record( | ||
| self, | ||
| cedar_referent___id, | ||
| cedar_record___id, | ||
| cedar_template_cedar_id, | ||
| ): | ||
| from osf.models import Guid | ||
| referent = Guid.load(cedar_referent___id).referent | ||
| response = requests.delete( | ||
| shtrove_ingest_url(), | ||
| params={ | ||
| 'record_identifier': _shtrove_cedar_record_identifier(cedar_record___id, cedar_template_cedar_id), | ||
| }, | ||
| headers=_shtrove_auth_headers(referent), | ||
| ) | ||
| retry_shtrove_request(self, response) | ||
|
|
||
|
|
||
| def _schedule_cedar_record_updates(guid_instance): | ||
| for cedar_record in guid_instance.cedar_metadata_records.filter( | ||
| is_published=True, | ||
| template__should_index_for_search=True, | ||
| ): | ||
| enqueue_task(share_update_cedar_metadata_record.s(guid_instance._id, cedar_record.pk)) | ||
|
|
||
| for cedar_record in guid_instance.cedar_metadata_records.filter( | ||
| Q(is_published=False) | Q(template__should_index_for_search=False), | ||
| ): | ||
| enqueue_task( | ||
| share_delete_cedar_metadata_record.s( | ||
|
Comment on lines
+160
to
+161
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto, |
||
| cedar_record.guid._id, | ||
| cedar_record._id, | ||
| cedar_record.template.cedar_id, | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| @celery_app.task( | ||
| bind=True, | ||
| acks_late=True, | ||
|
|
@@ -94,36 +196,21 @@ def task__update_share(self, guid: str, is_backfill=False, osfmap_partition_name | |
| log_exception(e) | ||
| raise self.retry(exc=e) | ||
|
|
||
| try: | ||
| _response.raise_for_status() | ||
| except Exception as e: | ||
| log_exception(e) | ||
| if _response.status_code == HTTPStatus.TOO_MANY_REQUESTS: | ||
| retry_after = _response.headers.get('Retry-After') | ||
| try: | ||
| countdown = int(retry_after) | ||
| except (TypeError, ValueError): | ||
| retries = getattr(self.request, 'retries', 0) | ||
| countdown = get_exponential_backoff_interval( | ||
| factor=4, | ||
| retries=retries, | ||
| maximum=2 * 60, | ||
| full_jitter=True, | ||
| ) | ||
| raise self.retry(exc=e, countdown=countdown) | ||
|
|
||
| if HTTPStatus(_response.status_code).is_server_error: | ||
| raise self.retry(exc=e) | ||
| else: # success response | ||
| if not _is_deletion: | ||
| # enqueue followup task for supplementary metadata | ||
| _next_partition = _next_osfmap_partition(_osfmap_partition) | ||
| if _next_partition is not None: | ||
| task__update_share.delay( | ||
| guid, | ||
| is_backfill=is_backfill, | ||
| osfmap_partition_name=_next_partition.name, | ||
| ) | ||
| retry_shtrove_request(self, _response) | ||
| # success response | ||
| if _is_deletion: | ||
| return | ||
|
|
||
| # enqueue followup task for supplementary metadata | ||
| _next_partition = _next_osfmap_partition(_osfmap_partition) | ||
| if _next_partition is not None: | ||
| task__update_share.delay( | ||
| guid, | ||
| is_backfill=is_backfill, | ||
| osfmap_partition_name=_next_partition.name, | ||
| ) | ||
| else: | ||
| _schedule_cedar_record_updates(_osfid_instance) | ||
|
|
||
|
|
||
| def pls_send_trove_record(osf_item, *, is_backfill: bool, osfmap_partition: OsfmapPartition): | ||
|
|
@@ -179,6 +266,10 @@ def _shtrove_record_identifier(osf_item, osfmap_partition: OsfmapPartition): | |
| ) | ||
|
|
||
|
|
||
| def _shtrove_cedar_record_identifier(cedar_record___id, template_cedar_id) -> str: | ||
| return f'{cedar_record___id}/CedarMetadataRecord:{template_cedar_id}' | ||
|
|
||
|
|
||
| def _shtrove_auth_headers(osf_item): | ||
| _nonfile_item = ( | ||
| osf_item.target | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmmph, looking closer at
framework.celery_tasks.handlers.enqueue_task... since this is running on a celery worker, not in a request context, enqueue_task will, surprisingly, run the task immediately, synchronously -- i'm not sure if there was any good reason for that, but for these tasks-starting-tasks seems like we should just use plain celery task methodsdelayorapply_async