diff --git a/invenio_config_tuw/curations/tasks.py b/invenio_config_tuw/curations/tasks.py index 2fef1b56b1caa1da76b05223334780f4518afc93..7956786d76631786fbb50508e10c191ea6b5a693 100644 --- a/invenio_config_tuw/curations/tasks.py +++ b/invenio_config_tuw/curations/tasks.py @@ -15,6 +15,7 @@ from celery.schedules import crontab from flask import current_app, url_for from invenio_access.permissions import system_identity from invenio_notifications.tasks import broadcast_notification +from invenio_pidstore.models import PIDDoesNotExistError from invenio_rdm_records.proxies import current_rdm_records_service as records_service from invenio_requests.customizations.event_types import CommentEventType from invenio_requests.proxies import current_events_service as events_service @@ -145,7 +146,7 @@ def send_open_requests_reminder_to_reviewers(request_ids: List[str]): @shared_task(ignore_result=True) def remind_uploaders_about_accepted_reviews( remind_after_days: Optional[List[int]] = None, -): +) -> List[str]: """Find curation reviews that were accepted a while ago and remind the uploaders. ``remind_after_days`` specifies after how many days of inactivity reminders @@ -156,7 +157,6 @@ def remind_uploaders_about_accepted_reviews( remind_after_days = [1, 3, 5, 7, 10, 14, 30] # first, we get a list of all requests that have been updated in the last year - # but excluding today (with the brackets "[ ... }") # # note: the date query is intended to set a soft limit on the number of results # to avoid unbounded degradation over time @@ -173,10 +173,11 @@ def remind_uploaders_about_accepted_reviews( q=( "type:rdm-curation AND " "status:accepted AND " - f"updated:[{start_date} TO {today}}}" + f"updated:[{start_date} TO {today}]" ), ) + records_reminded = [] now = datetime.now(tz=UTC) for request in accepted_curation_requests: if isinstance(request, dict): @@ -184,18 +185,27 @@ def remind_uploaders_about_accepted_reviews( # BEWARE: other than for resolving the topic, this is useless! request = requests_service.record_cls(request) - record = request.topic.resolve() - if record.is_published: - continue + try: + # quick sanity check: don't notify about weird zombie requests + record = request.topic.resolve() + if record.is_published: + continue + except PIDDoesNotExistError: + pass # check if we're hitting one of the reminder dates timestamp = _get_last_request_action_timestamp(request) if abs((now - timestamp).days) in remind_after_days: send_acceptance_reminder_to_uploader.delay(record.pid.pid_value) + records_reminded.append(record.pid.pid_value) + + return records_reminded @shared_task(ignore_result=True) -def remind_reviewers_about_open_reviews(remind_after_days: Optional[List[int]] = None): +def remind_reviewers_about_open_reviews( + remind_after_days: Optional[List[int]] = None, +) -> List[str]: """Remind a user about having an accepted review for an unpublished record. ``remind_after_days`` specifies after how many days of inactivity reminders @@ -220,10 +230,13 @@ def remind_reviewers_about_open_reviews(remind_after_days: Optional[List[int]] = # BEWARE: other than for resolving the topic, this is useless! request = requests_service.record_cls(request) - # quick sanity check: don't notify about weird zombie requests - record = request.topic.resolve() - if record.is_published: - continue + try: + # quick sanity check: don't notify about weird zombie requests + record = request.topic.resolve() + if record and record.is_published: + continue + except PIDDoesNotExistError: + pass # check if we're hitting one of the reminder dates timestamp = _get_last_request_action_timestamp(request) @@ -233,6 +246,8 @@ def remind_reviewers_about_open_reviews(remind_after_days: Optional[List[int]] = if stale_request_ids: send_open_requests_reminder_to_reviewers.delay(stale_request_ids) + return stale_request_ids + @shared_task(ignore_result=True) def auto_review_curation_request(request_id: str):