#! /bin/sh
#
# usage:  safe_bestrefs  --files <dataset FITS...>
#
#   options:  
#        --new-context [hst-operational | e.g. hst_0042.pmap] (optional, defaults hst-operational)
#        --files <FITS files to update...>
#
#   requires:  
#        CRDS_PATH        (cache directory)
#        *not* CRDS_SERVER_URL  (safe_bestrefs clears this to be disconnected from CRDS servers)
#
# This script is intended to be run in parallel with multiple pipeline bestrefs
# and a concurrent cron_sync.  The "safe" aspect refers to not modifying the
# CRDS cache itself, and to not stumbling into inconsistent cache states while
# another process is updating the cache.
#

#  To control when information is received from the server,  and to
#  prevent pipeline stall when the CRDS server is unavailable,
#  safe_bestrefs is configured with a bad server IP address.

#  To prevent multiple bestrefs or cron_sync from modifying the
#  cache config area,  it is chmod'ed to read-only.

#  To prevent multiple bestrefs from blocking one another,
#  they share crds.config.lock as a "read lock".

#  To prevent a pipeline stall on crds.config.lock in the
#  event the sync system fails,  safe_bestrefs only waits 3 
#  minutes on crds.config.lock and then proceeds anyway.

# -------------------------------------------------------------------------

# Add time to log messages
export CRDS_LOG_TIME=1

# safe_bestrefs should not receive updates from the server
export CRDS_SERVER_URL="https://crds-serverless-mode.stsci.edu"

# safe_bestrefs should not modify the cache.
export CRDS_READONLY_CACHE=1

# Number of times to try again for CRDS client network fails: 1 == 1 try, no retries
export CRDS_CLIENT_RETRY_COUNT=1

# Seconds to wait between retries
export CRDS_CLIENT_RETRY_DELAY_SECONDS=0

python -m crds.bestrefs --readonly-cache --update-bestrefs  $*



