genouest / biomaj-download

Download microservice for BioMAJ
GNU Affero General Public License v3.0
1 stars 7 forks source link

Biomaj-download

About

PyPI version

Microservice to manage the downloads of biomaj.

A protobuf interface is available in biomaj_download/message/downmessage_pb2.py to exchange messages between BioMAJ and the download service. Messages go through RabbitMQ (to be installed).

Python3 support only, python2 support is dropped

Protobuf

If you make changes to protobuf code, you need to compile it to python code:

cd /tmp/protoc/
PB_REL="https://github.com/protocolbuffers/protobuf/releases"
curl -LO $PB_REL/download/v23.2/protoc-23.2-linux-x86_64.zip  # Version used by GitHub Actions currently
unzip protoc-23.2-linux-x86_64.zip
cd ..../biomaj_download/message/
/tmp/protoc/bin/protoc --python_out=. downmessage.proto

Development

Test

To run the test suite, use:

LOCAL_IRODS=0 pytest -v tests/biomaj_tests.py

This command skips the test that need a local iRODS server.

Some test might fail due to network connection. You can skip them with:

NETWORK=0 pytest -v tests/biomaj_tests.py

Run

Message consumer

export BIOMAJ_CONFIG=path_to_config.yml
python bin/biomaj_download_consumer.py

Web server

If package is installed via pip, you need a file named gunicorn_conf.py containing somehwhere on local server:

def worker_exit(server, worker):
    from prometheus_client import multiprocess
    multiprocess.mark_process_dead(worker.pid)

If you cloned the repository and installed it via python setup.py install, just refer to the gunicorn_conf.py in the cloned repository.

export BIOMAJ_CONFIG=path_to_config.yml
rm -rf ..path_to/prometheus-multiproc
mkdir -p ..path_to/prometheus-multiproc
export prometheus_multiproc_dir=..path_to/prometheus-multiproc
gunicorn -c gunicorn_conf.py biomaj_download.biomaj_download_web:app

Web processes should be behind a proxy/load balancer, API base url /api/download

Prometheus endpoint metrics are exposed via /metrics on web server

Retrying

A common problem when downloading a large number of files is the handling of temporary failures (network issues, server too busy to answer, etc.). Since version 3.1.2, biomaj-download uses the Tenacity library which is designed to handle this. This mechanism is configurable through 2 downloader-specific options (see Download options): stop_condition and wait_policy.

When working on python code, you can pass instances of Tenacity's stop_base and wait_base respectively. This includes classes defined in Tenacity or your own derived classes.

For bank configuration those options also parse strings read from the configuration file. This parsing is based on the Simple Eval library. The rules are straightforward:

However, in this case, you can't use your own conditions. The complete list of stop conditions is:

The complete list of wait policies is:

Please refer to Tenacity doc for their meaning and their parameters.

Examples (inspired by Tenacity doc):

Note that some protocols (e.g. FTP) classify errors as temporary or permanent (for example trying to download inexisting file). More generally, we could distinguish permanent errors based on error codes, etc. and not retry in this case. However in our experience, so called permanent errors may well be temporary. Therefore downloaders always retry whatever the error. In some cases, this is a waste of time but generally this is worth it.

Host keys

When using the sftp protocol, biomaj-download must check the host key. Those keys are stored in a file (for instance ~/.ssh/known_hosts).

Two options are available to configure this:

When the host and the key are found in the file, the connection is accepted. If the host is found but the key missmatches, the connection is rejected (this usually indicates a problem or a change of configuration on the remote server). When the host is not found, the decision depends on the value of ssh_new_host:

See the description of the options in Download options.

Download options

Since version 3.0.26, you can use the set_options method to pass a dictionary of downloader-specific options. The following list shows some options and their effect (the option to set is the key and the parameter is the associated value):

Those options can be set in bank properties. See file global.properties.example in biomaj module.