def add_artifacts()

in Allura/allura/tasks/index_tasks.py [0:0]


def add_artifacts(ref_ids, update_solr=True, update_refs=True,
                  solr_hosts: Iterable[str] = (),
                  solr_creds: Iterable[tuple[str, str]] = (),
                  ):
    '''
    Add the referenced artifacts to SOLR and shortlinks.
    '''
    from allura import model as M
    from allura.lib.search import find_shortlinks

    # task params end up as instrumented lists, need to make this a list of plain tuples
    solr_creds = [tuple(cred) for cred in solr_creds]

    exceptions = []
    solr_updates = []
    with _indexing_disabled(M.session.artifact_orm_session._get()):
        for ref in M.ArtifactReference.query.find(dict(_id={'$in': ref_ids})):
            try:
                artifact = ref.artifact
                if artifact is None:
                    continue
                # c.project and .app are normally set, so keep using them
                # During a reindex or other batch jobs, they are not though, so set it from artifact
                app = getattr(c, 'app', None) or artifact.app
                project = getattr(c, 'project', None) or artifact.project
                with h.push_config(c, project=project, app=app):
                    s = artifact.solarize()
                    if s is None:
                        continue
                    if update_solr:
                        solr_updates.append(s)
                    if update_refs:
                        if isinstance(artifact, M.Snapshot):
                            continue
                        # Find shortlinks in the raw text, not the escaped html
                        # created by the `solarize()`.
                        link_text = artifact.index().get('text') or ''
                        shortlinks = find_shortlinks(link_text)
                        ref.references = [link.ref_id for link in shortlinks]
            except Exception:
                log.error('Error indexing artifact %s', ref._id)
                exceptions.append(sys.exc_info())

        def _add_artifact(solr: pysolr.Solr, artifacts: list):
            try:
                solr.add(artifacts)
            except HTTPRequestEntityTooLarge:
                if len(artifacts) > 1:
                    log.warning(f"Solr.add raised HTTPRequestEntityTooLarge. Splitting {len(artifacts)} updates into two batches.")
                    _add_artifact(solr, artifacts[:len(artifacts) // 2])
                    _add_artifact(solr, artifacts[len(artifacts) // 2:])
                else:
                    log.info("Solr.add raised HTTPRequestEntityTooLarge but there is only one artifact. Raising exception.")
                    raise

        _add_artifact(__get_solr(solr_hosts, solr_creds), solr_updates)

    if len(exceptions) == 1:
        raise exceptions[0][1].with_traceback(exceptions[0][2])
    if exceptions:
        raise CompoundError(*exceptions)
    check_for_dirty_ming_records('add_artifacts task')