add logging
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -1,2 +1,3 @@
 | 
			
		||||
*.swp
 | 
			
		||||
*.csv
 | 
			
		||||
log
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										34
									
								
								update
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								update
									
									
									
									
									
								
							@@ -3,13 +3,7 @@
 | 
			
		||||
# Settings
 | 
			
		||||
CSV_URL="https://www8.cao.go.jp/chosei/shukujitsu/syukujitsu.csv"
 | 
			
		||||
 | 
			
		||||
# Internal configuration settings
 | 
			
		||||
LOCK_DIR="lock"
 | 
			
		||||
 | 
			
		||||
TMP_CSV_FILE="holidays-jp-tmp.csv"
 | 
			
		||||
RAW_CSV_FILE="holidays-jp-raw.csv"
 | 
			
		||||
CSV_FILE="holidays-jp.csv"
 | 
			
		||||
 | 
			
		||||
# Constants
 | 
			
		||||
EXIT_SUCCESS=0
 | 
			
		||||
EXIT_FAILURE=1
 | 
			
		||||
 | 
			
		||||
@@ -23,18 +17,44 @@ if ! command -v nkf >/dev/null; then
 | 
			
		||||
  exit $EXIT_FAILURE
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Logging
 | 
			
		||||
CURRENT_MONTH=$(date "+%Yw%U%z")
 | 
			
		||||
SCRIPT_NAME=$(basename $0)
 | 
			
		||||
LOG_DIR="log"
 | 
			
		||||
LOG_FILE="$LOG_DIR/$SCRIPT_NAME_$CURRENT_MONTH"
 | 
			
		||||
 | 
			
		||||
get_current_time() {
 | 
			
		||||
  date "+%Y-%m-%d %H:%M:%S%z"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
log() {
 | 
			
		||||
  local timestamp=$(get_current_time)
 | 
			
		||||
  local message=$1
 | 
			
		||||
  echo "$timestamp $message" >>$LOG_FILE
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Acquire the lock or exit
 | 
			
		||||
LOCK_DIR="lock"
 | 
			
		||||
if ! mkdir "$LOCK_DIR" 2>/dev/null; then
 | 
			
		||||
  echo "Another process is holding the lock. Exiting."
 | 
			
		||||
  exit $EXIT_FAILURE
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
mkdir -p $LOG_DIR
 | 
			
		||||
 | 
			
		||||
# Internal configuration settings
 | 
			
		||||
TMP_CSV_FILE="holidays-jp-tmp.csv"
 | 
			
		||||
RAW_CSV_FILE="holidays-jp-raw.csv"
 | 
			
		||||
CSV_FILE="holidays-jp.csv"
 | 
			
		||||
 | 
			
		||||
# Run
 | 
			
		||||
curl -sS -L -o $TMP_CSV_FILE $CSV_URL
 | 
			
		||||
if ! diff -q $RAW_CSV_FILE $TMP_CSV_FILE >/dev/null 2>&1; then
 | 
			
		||||
  mv $TMP_CSV_FILE $RAW_CSV_FILE
 | 
			
		||||
  nkf -w $RAW_CSV_FILE >$CSV_FILE
 | 
			
		||||
  log "Changed"
 | 
			
		||||
else
 | 
			
		||||
  log "No Change"
 | 
			
		||||
  rm $TMP_CSV_FILE
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user