11#! /bin/bash
2+ # AUTHOR: Chris Brozdowski
3+ # DATE: 2025-02-20
4+ #
5+ # 1. Go to SPYGLASS_REPO_PATH and pull the latest changes from the master branch
6+ # 2. Test the SPYGLASS_CONDA_ENV conda environment
7+ # 3. Test the connection to the database
8+ # 4. Run the cleanup script
9+ #
10+ # This script is intended to be run as a cron job, weekly or more frequently.
11+ # It will store a log of its output in SPYGLASS_LOG.
12+ # If any of the operations fail, an email will be sent to SPYGLASS_EMAIL_DEST
213
3- # SETUP:
4- # 1. Create a conda environment with datajoint installed.
5- # 2. Edit the variables below to match your setup.
6- # 3. Set up the cron job (See README.md)
7- # Note that the log file will be truncated to the last 1000 lines.
14+ SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
15+ source " $SCRIPT_DIR /.env" # load environment variables from this directory
816
9- SPYGLASS_CONDA_ENV=spyglass
10- SPYGLASS_REPO_PATH=/home/franklab/spyglass
11- SPYGLASS_LOG=/home/franklab/spyglass/spyglass.log
17+ if [[ -z " ${SPYGLASS_CONDA_ENV} " \
18+ || -z " ${SPYGLASS_REPO_PATH} " \
19+ || -z " ${SPYGLASS_LOG} " ]]; then
20+ echo " Error: SPYGLASS_CONDA_ENV, SPYGLASS_REPO_PATH,
21+ and SPYGLASS_LOG must be set in .env"
22+ exit 1
23+ fi
24+
25+ EMAIL_TEMPLATE=$( cat << -EOF
26+ From: "Spyglass" <$SPYGLASS_EMAIL_SRC >
27+ To: $SPYGLASS_EMAIL_DEST
28+ Subject: cron fail - $( date " +%Y-%m-%d" )
29+
30+ %s
31+ EOF
32+ )
33+
34+ on_fail () { # $1: error message. Echo message and send as email
35+ echo " Error: $1 "
36+ if [ -z " $SPYGLASS_EMAIL_SRC " ]; then
37+ return 1 # No email source, so don't send an email
38+ fi
39+ local error_msg=" $1 "
40+ local content
41+ content=$( printf " $EMAIL_TEMPLATE " " $error_msg " )
42+
43+ curl -o /dev/null --ssl-reqd \
44+ --url " smtps://smtp.gmail.com:465" \
45+ --user " ${SPYGLASS_EMAIL_SRC} :${SPYGLASS_EMAIL_PASS} " \
46+ --mail-from " $SPYGLASS_EMAIL_SRC " \
47+ --mail-rcpt " $SPYGLASS_EMAIL_DEST " \
48+ -T <( echo " $content " )
49+ }
1250
1351exec >> $SPYGLASS_LOG 2>&1
1452
@@ -17,30 +55,33 @@ echo "SPYGLASS CRON JOB START: $(date +"%Y-%m-%d %H:%M:%S")"
1755
1856# Run from the root of the spyglass repository
1957cd $SPYGLASS_REPO_PATH || \
20- { echo " Error: Could not change to the spyglass directory" ; exit 1; }
58+ { on_fail " Could not find repo path: $SPYGLASS_REPO_PATH " ; exit 1; }
59+
2160
2261# Update the spyglass repository
23- git pull https://github.com/LorenFrankLab/spyglass.git master > /dev/null || \
24- { echo " Error: $PWD Could not update the spyglass repository" ; exit 1; }
62+ git pull --quiet \
63+ https://github.com/LorenFrankLab/spyglass.git master > /dev/null || \
64+ { on_fail " Could not update the spyglass repo $PWD " ; exit 1; }
2565
2666# Test conda environment
2767if ! conda env list | grep -q $SPYGLASS_CONDA_ENV ; then
28- echo " Error: Conda environment $SPYGLASS_CONDA_ENV not found"
29- exit 1
68+ on_fail " Conda environment $SPYGLASS_CONDA_ENV not found"
69+ exit 1
3070fi
3171
3272# convenience function to run a command in the spyglass conda environment
3373conda_run () { conda run --name $SPYGLASS_CONDA_ENV " $@ " ; }
3474
3575# Test connection to the database
36- conda_run python -c " import datajoint as dj; dj.conn()" > /dev/null || \
37- { echo " Error: Could not connect to the database" ; exit 1; }
76+ CONN_TEST=" import datajoint as dj; dj.logger.setLevel('ERROR'); dj.conn()"
77+ conda_run python -c " $CONN_TEST " > /dev/null || \
78+ { on_fail " Could not connect to the database" ; exit 1; }
3879
3980# Run cleanup script
4081conda_run python maintenance_scripts/cleanup.py
4182
4283echo " SPYGLASS CRON JOB END"
4384
4485# truncate long log file
45- tail -n 1000 " $SPYGLASS_LOG " > " ${SPYGLASS_LOG} .tmp" && \
46- mv " ${SPYGLASS_LOG} .tmp" " $SPYGLASS_LOG "
86+ tail -n ${SPYGLASS_MAX_LOG :- 1000} " $SPYGLASS_LOG " > " ${SPYGLASS_LOG} .tmp" \
87+ && mv " ${SPYGLASS_LOG} .tmp" " $SPYGLASS_LOG "
0 commit comments