# Path: zenodo/modules/openaire/tasks.py
# @shared_task(ignore_result=True, max_retries=6,
#              default_retry_delay=4 * 60 * 60, rate_limit='100/m')
# def openaire_direct_index(record_uuid, retry=True):
#     """Send record for direct indexing at OpenAIRE.
# 
#     :param record_uuid: Record Metadata UUID.
#     :type record_uuid: str
#     """
#     try:
#         record = ZenodoRecord.get_record(record_uuid)
# 
#         # Bail out if not an OpenAIRE record.
#         if not (is_openaire_publication(record) or
#                 is_openaire_dataset(record) or
#                 is_openaire_software(record) or
#                 is_openaire_other(record)):
#             return
# 
#         data = openaire_json_v1.serialize(record.pid, record)
#         url = '{}/feedObject'.format(
#             current_app.config['OPENAIRE_API_URL'])
#         req = _openaire_request_factory()
#         res = req.post(url, data=data, timeout=10)
# 
#         if not res.ok:
#             raise OpenAIRERequestError(res.text)
# 
#         res_beta = None
#         if current_app.config['OPENAIRE_API_URL_BETA']:
#             url_beta = '{}/feedObject'.format(
#                 current_app.config['OPENAIRE_API_URL_BETA'])
#             res_beta = req.post(url_beta, data=data, timeout=10)
# 
#         if res_beta and not res_beta.ok:
#             raise OpenAIRERequestError(res_beta.text)
#         else:
#             recid = record.get('recid')
#             current_cache.delete('openaire_direct_index:{}'.format(recid))
#     except Exception as exc:
#         recid = record.get('recid')
#         current_cache.set('openaire_direct_index:{}'.format(recid),
#                           datetime.now(), timeout=-1)
#         if retry:
#             openaire_direct_index.retry(exc=exc)
#         else:
#             raise exc


import json
import os
import click
from io import SEEK_END, SEEK_SET
from flask.cli import with_appcontext
from invenio_cache import current_cache
from invenio_db import db
from invenio_files_rest.models import ObjectVersion
from invenio_pidstore.models import PersistentIdentifier
from invenio_records.api import Record
from invenio_records.models import RecordMetadata
from zenodo.modules.deposit.resolvers import deposit_resolver
from zenodo.modules.deposit.tasks import datacite_register
from zenodo.modules.records.resolvers import record_resolver
from ..openaire.tasks import openaire_direct_index
from ..records.resolvers import record_resolver
from .grants import OpenAIREGrantsDump
from .openaire import OpenAIRECommunitiesMappingUpdater
from .tasks import has_corrupted_files_meta, repair_record_metadata, \
    sync_record_oai, update_oaisets_cache, update_search_pattern_sets
    click.secho('{0}'.format(json.dumps(mapping, indent=4,
                                        separators=(', ', ': '))), fg='blue')

def _iter_openaire_direct_index_keys():
    return current_cache.cache._read_clients.scan_iter(
        match=(current_cache.cache.key_prefix + 'openaire_direct_index*'))

@utils.command('list_failed_openaire_records')
@with_appcontext
def list_failed_openaire_records():
    """Lists records that failed to get indexed in OpenAIRE."""
    failed_record_recids = []
    for key in _iter_openaire_direct_index_keys():
        recid = key.split('openaire_direct_index:')[1]
        failed_record_recids.append(key)
    click.echo('\n'.join(failed_record_recids))


@utils.command('index_failed_openaire_records')
@click.option('--eager', '-e', default=False)
@with_appcontext
def retry_indexing_failed_openaire_records(eager):
    """Retries indexing of records that failed to get indexed in OpenAIRE."""
    for key in _iter_openaire_direct_index_keys():
        recid = key.split('openaire_direct_index:')[1]
        recid, record  = record_resolver.resolve(recid)
        click.echo("Indexing record with id {}".format(recid))
        if eager:
            openaire_direct_index(str(recid.object_uuid), retry=False)
        else:

# Path: zenodo/modules/openaire/tasks.py
# @shared_task(ignore_result=True, max_retries=6,
#              default_retry_delay=4 * 60 * 60, rate_limit='100/m')
# def openaire_direct_index(record_uuid, retry=True):
#     """Send record for direct indexing at OpenAIRE.
# 
#     :param record_uuid: Record Metadata UUID.
#     :type record_uuid: str
#     """
#     try:
#         record = ZenodoRecord.get_record(record_uuid)
# 
#         # Bail out if not an OpenAIRE record.
#         if not (is_openaire_publication(record) or
#                 is_openaire_dataset(record) or
#                 is_openaire_software(record) or
#                 is_openaire_other(record)):
#             return
# 
#         data = openaire_json_v1.serialize(record.pid, record)
#         url = '{}/feedObject'.format(
#             current_app.config['OPENAIRE_API_URL'])
#         req = _openaire_request_factory()
#         res = req.post(url, data=data, timeout=10)
# 
#         if not res.ok:
#             raise OpenAIRERequestError(res.text)
# 
#         res_beta = None
#         if current_app.config['OPENAIRE_API_URL_BETA']:
#             url_beta = '{}/feedObject'.format(
#                 current_app.config['OPENAIRE_API_URL_BETA'])
#             res_beta = req.post(url_beta, data=data, timeout=10)
# 
#         if res_beta and not res_beta.ok:
#             raise OpenAIRERequestError(res_beta.text)
#         else:
#             recid = record.get('recid')
#             current_cache.delete('openaire_direct_index:{}'.format(recid))
#     except Exception as exc:
#         recid = record.get('recid')
#         current_cache.set('openaire_direct_index:{}'.format(recid),
#                           datetime.now(), timeout=-1)
#         if retry:
#             openaire_direct_index.retry(exc=exc)
#         else:
#             raise exc


# Path: zenodo/modules/utils/cli.py
import json
import os
import click
from io import SEEK_END, SEEK_SET
from flask.cli import with_appcontext
from invenio_cache import current_cache
from invenio_db import db
from invenio_files_rest.models import ObjectVersion
from invenio_pidstore.models import PersistentIdentifier
from invenio_records.api import Record
from invenio_records.models import RecordMetadata
from zenodo.modules.deposit.resolvers import deposit_resolver
from zenodo.modules.deposit.tasks import datacite_register
from zenodo.modules.records.resolvers import record_resolver
from ..openaire.tasks import openaire_direct_index
from ..records.resolvers import record_resolver
from .grants import OpenAIREGrantsDump
from .openaire import OpenAIRECommunitiesMappingUpdater
from .tasks import has_corrupted_files_meta, repair_record_metadata, \
    sync_record_oai, update_oaisets_cache, update_search_pattern_sets

    click.secho('{0}'.format(json.dumps(mapping, indent=4,
                                        separators=(', ', ': '))), fg='blue')

def _iter_openaire_direct_index_keys():
    return current_cache.cache._read_clients.scan_iter(
        match=(current_cache.cache.key_prefix + 'openaire_direct_index*'))

@utils.command('list_failed_openaire_records')
@with_appcontext
def list_failed_openaire_records():
    """Lists records that failed to get indexed in OpenAIRE."""
    failed_record_recids = []
    for key in _iter_openaire_direct_index_keys():
        recid = key.split('openaire_direct_index:')[1]
        failed_record_recids.append(key)
    click.echo('\n'.join(failed_record_recids))


@utils.command('index_failed_openaire_records')
@click.option('--eager', '-e', default=False)
@with_appcontext
def retry_indexing_failed_openaire_records(eager):
    """Retries indexing of records that failed to get indexed in OpenAIRE."""
    for key in _iter_openaire_direct_index_keys():
        recid = key.split('openaire_direct_index:')[1]
        recid, record  = record_resolver.resolve(recid)
        click.echo("Indexing record with id {}".format(recid))
        if eager:
            openaire_direct_index(str(recid.object_uuid), retry=False)
        else:

            openaire_direct_index.delay(