-
Notifications
You must be signed in to change notification settings - Fork 360
[ENG-9044] Add manage command to resync preprint dois v1 #11617
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
Vlad0n20
wants to merge
8
commits into
CenterForOpenScience:feature/pbs-26-15
Choose a base branch
from
Vlad0n20:fix/ENG-9044
base: feature/pbs-26-15
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
8 commits
Select commit
Hold shift + click to select a range
630c7ff
Add manage command to resync preprint dois v1
Vlad0n20 f943f05
Update command
Vlad0n20 9a50376
Added a class-level docstring
Vlad0n20 571020f
Register unversioned DOI alongside versioned on Crossref submissions
Vlad0n20 928b8b7
Add daily Celery beat task and unversioned DOI tracking for preprint …
Vlad0n20 1973527
fix test
Vlad0n20 7d513bc
fix comments
bodintsov 0a2b707
fix tests
bodintsov 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,239 @@ | ||
| import logging | ||
|
|
||
| from django.contrib.contenttypes.models import ContentType | ||
| from django.core.management.base import BaseCommand | ||
| from django.db.models import Q | ||
|
|
||
| from framework.celery_tasks import app | ||
| from osf.models import Preprint, Identifier | ||
| from osf.models.base import VersionedGuidMixin | ||
| from osf.management.commands.sync_doi_metadata import async_request_identifier_update | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| def get_preprints_needing_v1_doi(provider_id=None): | ||
| content_type = ContentType.objects.get_for_model(Preprint) | ||
|
|
||
| already_versioned_ids = Identifier.objects.filter( | ||
| content_type=content_type, | ||
| category='doi', | ||
| deleted__isnull=True, | ||
| value__contains=VersionedGuidMixin.GUID_VERSION_DELIMITER, | ||
| ).values_list('object_id', flat=True) | ||
|
|
||
| public_query = Q(is_published=True, is_public=True, deleted__isnull=True) | ||
| withdrawn_query = Q(date_withdrawn__isnull=False, ever_public=True) | ||
|
|
||
| qs = Preprint.objects.filter( | ||
| versioned_guids__version=1, | ||
| ).filter( | ||
| public_query | withdrawn_query | ||
| ).exclude( | ||
| id__in=already_versioned_ids | ||
| ).exclude( | ||
| tags__name='qatest', | ||
| tags__system=True, | ||
| ).select_related('provider').distinct() | ||
|
|
||
| if provider_id: | ||
| qs = qs.filter(provider___id=provider_id) | ||
|
|
||
| return qs | ||
|
|
||
|
|
||
| def resync_preprint_dois_v1(dry_run=True, batch_size=1000, provider_id=None): | ||
| preprints_to_update = get_preprints_needing_v1_doi(provider_id=provider_id) | ||
|
|
||
| total = preprints_to_update.count() | ||
| logger.info( | ||
| f'{"[DRY RUN] " if dry_run else ""}' | ||
| f'{total} preprints need v1 DOI resync' | ||
| + (f' (provider={provider_id})' if provider_id else '') | ||
| ) | ||
|
|
||
| if batch_size: | ||
| preprints_iterable = preprints_to_update[:batch_size] | ||
| else: | ||
| preprints_iterable = preprints_to_update.iterator() | ||
|
|
||
| queued = 0 | ||
| skipped = 0 | ||
| errored = 0 | ||
| for preprint in preprints_iterable: | ||
| if not preprint.provider.doi_prefix: | ||
| logger.warning( | ||
| f'Skipping preprint {preprint._id}: ' | ||
| f'provider {preprint.provider._id} has no DOI prefix' | ||
| ) | ||
| skipped += 1 | ||
| continue | ||
|
|
||
| if dry_run: | ||
| logger.info(f'[DRY RUN] Would resync DOI for preprint {preprint._id}') | ||
| queued += 1 | ||
| continue | ||
|
|
||
| try: | ||
| async_request_identifier_update.apply_async(kwargs={'preprint_id': preprint._id}) | ||
| logger.info(f'Queued DOI resync for preprint {preprint._id}') | ||
| queued += 1 | ||
| except Exception: | ||
| logger.exception(f'Failed to queue DOI resync for preprint {preprint._id}') | ||
| errored += 1 | ||
|
|
||
| logger.info( | ||
| f'{"[DRY RUN] " if dry_run else ""}' | ||
| f'Done: {queued} preprints queued, {skipped} skipped (no DOI prefix), {errored} errored' | ||
| ) | ||
| if not dry_run and batch_size: | ||
| logger.info( | ||
| f'Estimated remaining after this batch: ~{max(0, total - queued - skipped - errored)}. ' | ||
| f'Re-run this command until 0 preprints remain.' | ||
| ) | ||
|
|
||
|
|
||
| def get_preprints_needing_unversioned_doi(provider_id=None): | ||
| content_type = ContentType.objects.get_for_model(Preprint) | ||
|
|
||
| already_has_unversioned = Identifier.objects.filter( | ||
| content_type=content_type, | ||
| category='doi_unversioned', | ||
| deleted__isnull=True, | ||
| ).values_list('object_id', flat=True) | ||
|
|
||
| has_versioned_doi = Identifier.objects.filter( | ||
| content_type=content_type, | ||
| category='doi', | ||
| deleted__isnull=True, | ||
| value__contains=VersionedGuidMixin.GUID_VERSION_DELIMITER, | ||
| ).values_list('object_id', flat=True) | ||
|
|
||
| public_query = Q(is_published=True, is_public=True, deleted__isnull=True) | ||
| withdrawn_query = Q(date_withdrawn__isnull=False, ever_public=True) | ||
|
|
||
| qs = Preprint.objects.filter( | ||
| versioned_guids__version=1, | ||
| id__in=has_versioned_doi, | ||
| ).filter( | ||
| public_query | withdrawn_query | ||
| ).exclude( | ||
| id__in=already_has_unversioned | ||
| ).exclude( | ||
| tags__name='qatest', | ||
| tags__system=True, | ||
| ).select_related('provider').distinct() | ||
|
|
||
| if provider_id: | ||
| qs = qs.filter(provider___id=provider_id) | ||
|
|
||
| return qs | ||
|
|
||
|
|
||
| def register_missing_unversioned_dois(dry_run=True, batch_size=1000, provider_id=None): | ||
| preprints_to_update = get_preprints_needing_unversioned_doi(provider_id=provider_id) | ||
|
|
||
| total = preprints_to_update.count() | ||
| logger.info( | ||
| f'{"[DRY RUN] " if dry_run else ""}' | ||
| f'{total} preprints need unversioned DOI registration' | ||
| + (f' (provider={provider_id})' if provider_id else '') | ||
| ) | ||
|
|
||
| if batch_size: | ||
| preprints_iterable = preprints_to_update[:batch_size] | ||
| else: | ||
| preprints_iterable = preprints_to_update.iterator() | ||
|
|
||
| queued = 0 | ||
| skipped = 0 | ||
| errored = 0 | ||
| for preprint in preprints_iterable: | ||
| if not preprint.provider.doi_prefix: | ||
| logger.warning( | ||
| f'Skipping preprint {preprint._id}: ' | ||
| f'provider {preprint.provider._id} has no DOI prefix' | ||
| ) | ||
| skipped += 1 | ||
| continue | ||
|
|
||
| if dry_run: | ||
| logger.info(f'[DRY RUN] Would register unversioned DOI for preprint {preprint._id}') | ||
| queued += 1 | ||
| continue | ||
|
|
||
| try: | ||
| async_request_identifier_update.apply_async(kwargs={'preprint_id': preprint._id}) | ||
| logger.info(f'Queued unversioned DOI registration for preprint {preprint._id}') | ||
| queued += 1 | ||
| except Exception: | ||
| logger.exception(f'Failed to queue unversioned DOI registration for preprint {preprint._id}') | ||
| errored += 1 | ||
|
|
||
| logger.info( | ||
| f'{"[DRY RUN] " if dry_run else ""}' | ||
| f'Unversioned DOI pass done: {queued} queued, {skipped} skipped, {errored} errored' | ||
| ) | ||
| if not dry_run and batch_size: | ||
| logger.info( | ||
| f'Estimated unversioned remaining after this batch: ~{max(0, total - queued - skipped - errored)}. ' | ||
| f'Re-run until 0 preprints remain.' | ||
| ) | ||
|
|
||
|
|
||
| @app.task(name='osf.management.commands.resync_preprint_dois_v1', max_retries=0) | ||
| def resync_preprint_dois_v1_task(batch_size=1000, dry_run=False, provider_id=None): | ||
| resync_preprint_dois_v1( | ||
| dry_run=dry_run, | ||
| batch_size=batch_size, | ||
| provider_id=provider_id, | ||
| ) | ||
| register_missing_unversioned_dois( | ||
| dry_run=dry_run, | ||
| batch_size=batch_size, | ||
| provider_id=provider_id, | ||
| ) | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = ( | ||
| 'Resync DOIs for version-1 preprints that are missing the versioned DOI suffix (_v1). ' | ||
| 'Processes preprints in batches and queues Crossref deposit tasks. ' | ||
| 'IMPORTANT: This command must be run repeatedly until it reports 0 preprints remaining, ' | ||
| 'as each run only processes a single batch. ' | ||
| 'Check remaining count with --dry_run before and after each run.' | ||
| ) | ||
|
|
||
| def add_arguments(self, parser): | ||
| super().add_arguments(parser) | ||
| parser.add_argument( | ||
| '--dry_run', | ||
| action='store_true', | ||
| dest='dry_run', | ||
| help='Log what would be done without submitting to Crossref.', | ||
| ) | ||
| parser.add_argument( | ||
| '--batch_size', | ||
| '-b', | ||
| type=int, | ||
| default=1000, | ||
| help=( | ||
| 'Maximum number of preprints to process per run (default: 1000). ' | ||
| 'The command processes the first N eligible preprints and exits; ' | ||
| 're-run the command to continue with the next batch.' | ||
| ), | ||
|
Comment on lines
+219
to
+223
Collaborator
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. Oh, I see. This is an OK alternative to the loop I suggested. Just need to make sure whoever runs this command is aware of the fact that they need to keep running this command until none exists. Cc @adlius for your input on this. |
||
| ) | ||
| parser.add_argument( | ||
| '--provider', | ||
| '-p', | ||
| type=str, | ||
| default=None, | ||
| dest='provider_id', | ||
| help='Restrict to a single provider _id (e.g. socarxiv).', | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
| resync_preprint_dois_v1( | ||
| dry_run=options['dry_run'], | ||
| batch_size=options['batch_size'], | ||
| provider_id=options['provider_id'], | ||
| ) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.