Zum Inhalt

The Bookkeeping Processing Interface

Most modules interact with the bookkeeping system through ProcessingInterface. The interface hands out ProcessingJob objects, which describe the publication and processing location a module should work on, and accepts those jobs back together with the module's file changes.

Use ProcessingInterface as a context manager so bookkeeping connections and open jobs can be handled correctly when processing finishes or an exception occurs.

Opening the processing interface

from bookkeeping.processinginterface import ProcessingInterface

with ProcessingInterface(
    config_file="example.json",
    module_name="relaxng_scanner",
    module_version="latest git commit hash",
) as pi:
    pass

The constructor option max_parallel controls how many bookkeeping jobs may be open concurrently. Most modules should keep the default and receive, process, and return one job before requesting the next. Modules that intentionally batch work can set a higher value, but every open job must be returned before its source connection is closed.

Receiving existing publications

ProcessingInterface.obtain_jobs()

obtain_jobs(
    limit=None,
    indicate_source_end=False,
    indicate_pipeline_end=False,
    ignore_suspension=False,
    max_jobs_pipeline=0,
)

obtain_jobs() is a generator that yields ProcessingJob objects for publications assigned to the current module.

Parameters:

  • limit: restrict processing to "journals" or "processing-groups"; None processes both.
  • indicate_source_end: yield an EndOfSourceIndicator before moving away from a source.
  • indicate_pipeline_end: yield an EndOfPipelineIndicator after a pipeline.
  • ignore_suspension: include jobs whose suspension end time is still in the future.
  • max_jobs_pipeline: for positive values, cap the number of jobs handed out per pipeline. Queued jobs are selected before stopped, manual, or unassigned jobs.

When end indicators are requested, handle them before treating the yielded object as a normal publication job.

ProcessingInterface.create_temp_folder()

temp_dir_abs, temp_dir_name = pi.create_temp_folder(job)

For a non-startup job, this copies the previous module state into a temporary module directory. It returns the absolute temporary path and the temporary directory name required by return_job().

ProcessingInterface.return_job()

return_job(
    job,
    temp_dir,
    processed_dir,
    created_files=None,
    modified_files=None,
    deleted_files=None,
    complete_check=False,
    add_flags=None,
    add_derived_from=None,
    add_publication_properties=None,
    add_location_properties=None,
    suspend_until=None,
    derivate_check_location=None,
    derivate_check_iri=None,
)

return_job() finalizes a ProcessingJob, persists the bookkeeping updates, and removes it from the interface's open-job list. Processing success or stop state is derived from processed_dir; passing None records an unsuccessful attempt and leaves the publication available for retry. The method returns True when finalization succeeds and False otherwise.

File lists contain paths relative to the module directory:

  • created_files: files created by the module.
  • modified_files: existing files changed by the module.
  • deleted_files: existing files removed by the module.

Report all changed files except the documentation history file. complete_check=True performs the more expensive full comparison between the filesystem and the bookkeeping graph. suspend_until, when provided, must be a timezone-aware datetime.

Normal processing example

from bookkeeping.processinginterface import (
    EndOfPipelineIndicator,
    EndOfSourceIndicator,
    ProcessingInterface,
)

with ProcessingInterface(
    config_file="example.json",
    module_name="relaxng_scanner",
    module_version="latest git commit hash",
) as pi:
    for job in pi.obtain_jobs(
        indicate_source_end=True,
        indicate_pipeline_end=True,
    ):
        if isinstance(job, (EndOfSourceIndicator, EndOfPipelineIndicator)):
            continue

        temp_dir_abs, temp_dir_name = pi.create_temp_folder(job)

        # Process files in temp_dir_abs.
        created_files = ["relaxng_valid.txt"]
        processed_dir_name = temp_dir_name.removeprefix("temp-")

        pi.return_job(
            job=job,
            temp_dir=temp_dir_name,
            processed_dir=processed_dir_name,
            created_files=created_files,
        )

Delivery lookup

Use pi.get_delivery_from_job_information(job) when a module needs the latest delivery associated with the publication represented by a job. The method returns a delivery component object or None when no matching delivery is registered.

Creating jobs at workflow startup

Startup modules such as crossrefcrawler create the first processing location themselves. They obtain configured source identifiers and create ProcessingJob instances for newly harvested publications.

ProcessingInterface.obtain_startup_identifier()

obtain_startup_identifier(
    identifier_name="ISSN",
    limit=None,
    indicate_source_end=False,
)

This generator yields configured identifiers such as ISSNs. Use these identifiers while the interface's source, group, and pipeline context is active.

ProcessingInterface.create_startup_job()

create_startup_job(
    provider_publication_identifiers,
    article_dir=None,
    activity_start=None,
)

provider_publication_identifiers accepts one identifier or an iterable of identifiers, for example a DOI. article_dir may be supplied when the job is created or later when it is returned. The return value is a startup ProcessingJob.

ProcessingInterface.return_startup_job()

return_startup_job() accepts the startup job, article_dir, temp_dir, and processed_dir, followed by the same file-change, flag, property, and suspension options used for normal jobs. It selects the appropriate next pipeline, registers the new location, and returns True on successful finalization or discard.

for issn in pi.obtain_startup_identifier(limit="journals"):
    for doi, metadata in get_dois_from_issn(issn):
        temp_folder = create_temp_folder_with_metadata(doi, metadata)
        job = pi.create_startup_job(
            provider_publication_identifiers=f"doi:{doi}",
            article_dir=temp_folder.parent,
        )

        # Return the job while its source and pipeline context is still active.
        pi.return_startup_job(
            job=job,
            article_dir=None,
            temp_dir=temp_folder.name,
            processed_dir=temp_folder.name.removeprefix("temp-"),
            created_files=["metadata-article.json"],
        )

If article_dir is still unknown or processed_dir is None or still starts with the temporary-directory prefix, the startup job is discarded without registering a processing location.

Cached startup jobs

For batches, return_startup_job_cached() accepts the same startup-job information but collects it for batch persistence instead of immediately writing it to the graph. All cached entries must belong to the same active source, group, and pipeline context.

Call process_startup_cache() to write and process the batch. It first writes a startup_job_cache-*.json recovery file. If writing that recovery data fails, it raises bookkeeping.exceptions.StartupCacheNotWritten. Incomplete cache files can be recovered with rescue_cached_jobs().

from bookkeeping.exceptions import StartupCacheNotWritten

try:
    pi.process_startup_cache()
except StartupCacheNotWritten:
    discard_batch()
    raise
except Exception:
    mark_batch_complete()
    raise
else:
    mark_batch_complete()