From b2b926548cf654480717bcfec5c4a5a39bff63ac Mon Sep 17 00:00:00 2001 From: Bohdan Marukhnenko Date: Thu, 9 Feb 2023 16:43:46 +0200 Subject: [PATCH] Created a script for loading the MKR cache --- tools/load-cache.bat | 57 ++++++++++++++++++++++++++++++++++++++++++++ tools/load-cache.sh | 55 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 tools/load-cache.bat create mode 100644 tools/load-cache.sh diff --git a/tools/load-cache.bat b/tools/load-cache.bat new file mode 100644 index 0000000..253394f --- /dev/null +++ b/tools/load-cache.bat @@ -0,0 +1,57 @@ +@echo off + +SET CUR_YEAR=%date:~6,4% +SET /a NEX_YEAR=%CUR_YEAR% + 1 + +SET DATE_START=%CUR_YEAR%-01-01 +SET DATE_END=%NEX_YEAR%-01-01 +SET API_URL=https://mia.mobil.knute.edu.ua + +SET SCRIPT_DIR=%~dp0 +SET ROOT_DIR=%~dp0.. + +:: Check if cache is installed +if exist "%ROOT_DIR%\cache\temp-mkr-cache.loaded" ( + call :setup_cache || exit 1 + exit 0 +) + +call :clear_temp_cache + +:: Install cacher if not installed +echo Installing cacher +start cmd /k call "%SCRIPT_DIR%install-cacher.bat" -y || ( + echo Failed to install cacher + exit 1 +) && echo Cacher installed + +:: Load cache +"%ROOT_DIR%\bin\cacher.exe" -url=%API_URL% -dateStart=%DATE_START% -dateEnd=%DATE_END% -output="%ROOT_DIR%\cache\temp-mkr-cache.sqlite" || ( + echo Failed to load cache + call :clear_temp_cache + exit 1 +) + +call :setup_cache || exit 1 + + +:: Setup cache file +:setup_cache +echo Setting up cache file +move "%ROOT_DIR%\cache\temp-mkr-cache.sqlite" "%ROOT_DIR%\cache\mkr-cache.sqlite" || ( + echo Failed to setup cache file. Perhaps you forgot to stop the bot first? + copy /y NUL "%ROOT_DIR%\cache\temp-mkr-cache.loaded" > NUL + exit /b 1 +) +call :clear_temp_cache +echo Done +exit /b 0 + +:: Function that clears broken cache in case of error +:clear_temp_cache +echo Clearing temp cache +del "%ROOT_DIR%\cache\temp-mkr-cache.*" 2>nul || ( + echo Failed to clear cache + exit /b 1 +) +exit /b 0 diff --git a/tools/load-cache.sh b/tools/load-cache.sh new file mode 100644 index 0000000..9282409 --- /dev/null +++ b/tools/load-cache.sh @@ -0,0 +1,55 @@ +DATE_START=$(date -d $(date +%Y)-01-01 +%Y-%m-%d) # Start date of the current year +DATE_END=$(date -d $(date -d +1year +%Y)-01-01 +%Y-%m-%d) # Start date of the next year +API_URL=https://mia.mobil.knute.edu.ua + +SCRIPT_DIR=$(dirname $(realpath $0)) +ROOT_DIR=$(dirname $SCRIPT_DIR) + +echo $DATE_START $DATE_END + +exit 0 + +# Check if cache is already loaded +if [ -f $ROOT_DIR/cache/temp-mkr-cache.loaded ]; then + exit $(setup_cache) +fi + +clear_temp_cache + +# Install cacher if not installed +echo Installing cacher +bash $SCRIPT_DIR/install-cacher.sh -y || ( + echo Failed to install cacher + exit 1 +) && echo Cacher installed + +# Load cache +bash $ROOT_DIR/tools/load_cache.sh -url=$API_URL -dateStart=$DATE_START -dateEnd=$DATE_END -output="$ROOT_DIR/cache/temp-mkr-cache.sqlite" || ( + echo Failed to load cache + clear_temp_cache + exit 1 +) + +setup_cache || exit 1 + + +# Setup cache file +function setup_cache { + mv $ROOT_DIR/cache/temp-mkr-cache.sqlite $ROOT_DIR/cache/mkr-cache.sqlite || ( + echo Failed to setup cache file. Perhaps you forgot to stop the bot first? + touch $ROOT_DIR/cache/temp-mkr-cache.loaded + return 1 + ) + clear_temp_cache + echo Done + return 0 +} + +# Function that clears broken cache in case of error +function clear_temp_cache { + rm $ROOT_DIR/cache/temp-mkr-cache.* || ( + echo Failed to clear temp cache + return 1 + ) + return 0 +}