#! /bin/sh 

# usage:  cron_sync
#
#    options:  --check-files --check-sha1sum --fetch-references [context selection]
#
#    example context selections:
#        nothing ==  hst-operational
#        --contexts hst_0257.pmap hst_0283.pmap hst-operational
#        --last 5
#        --all
#
#   requires:  
#        CRDS_PATH        (cache directory)
#        CRDS_SERVER_URL  (server synced from)
#        CRDS_LOCKS       (locks directory)
#
# This script is intended to be run in a cron job at a relatively high
# rate, say once every 30 minutes.  
#
# Several things should be considered for cron rate:
#
#   latency     --  the rate at which cron_sync will notice new files.
# 
#   reliability --  the chance that the unsafe_sync will wind up
#                   transferring reference data while bestrefs is
#                   blocked.
#
#   system load  -- doing both safe and unsafe syncs currently runs
#                   for about a minute,  presumably using primarily
#                   CPU and network,  not memory or disk.   This is
#                   the nominal case where nothing is new or changing.


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

# default CRDS_LOCKS to CRDS_PATH unless it is known that the CRDS_PATH
# file system has broken flock's
if [[ -z "$CRDS_LOCKS" ]]
then
    export CRDS_LOCKS="$CRDS_PATH"
fi
mkdir -p $CRDS_LOCKS

# Add time to log messages
export CRDS_LOG_TIME=1

# Make the cache writable by default,  most common.
export CRDS_READONLY_CACHE=0

# Use a plugin downloader,  defaults to wget
export CRDS_DOWNLOAD_MODE=plugin

#
# CUSTOM PLUGINS: CRDS will run another program like this filling in for
# ${SOURCE_URL} and ${OUTPUT_PATH}.  Use that syntax exactly.   You could
# e.g. use axel instead of wget providing your own command line template.
#
# export CRDS_DOWNLOAD_PLUGIN="/usr/bin/wget --no-check-certificate --quiet ${SOURCE_URL}  -O ${OUTPUT_PATH}"
#

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

( flock --exclusive --nonblock 200 # --nonblock means fail on unavailable locks,  cron collision

  if [[ $? == 1 ]]
  then
    echo "cron_sync: WARNING: failed to obtain crds.sync.lock"
    exit 1
  else
     echo "cron_sync: INFO: obtained crds.sync.lock"
    
     # sync doesn't block bestrefs, syncs rules and references,  doesn't update context / config
     export CRDS_CLIENT_RETRY_COUNT=60
     export CRDS_CLIENT_RETRY_DELAY_SECONDS=10
     # override pipeline global readonly setting.
     export CRDS_READONLY_CACHE=0
     chmod 644 ${CRDS_PATH}/config/{hst,jwst}/* >& /dev/null 
     python -m crds.sync $* --verbose --stats # nominally --last 10 --fetch-references
    
  fi
    
) 200> ${CRDS_LOCKS}/crds.sync.lock 
exit 0

#
#   Summarizing overall design of sync + bestrefs in pipelines:
#
#   crds.sync.lock ensures that no two cron_syncs run concurrently.
#
#   crds.sync does not block bestrefs
#   bestrefs does not block bestrefs
#   sync transfers files then updates the config area last
#   bestrefs does not update the cache when run with --readonly-cache
#
#   To prevent a race condition where the context updates before the arrival
#   of mappings,  all programs the default setting in the pipeline environment
#   should be export CRDS_READONLY_CACHE=1.
#