Skip to content

Commit

Permalink
Created a script for loading the MKR cache
Browse files Browse the repository at this point in the history
  • Loading branch information
cubicbyte committed Feb 10, 2023
1 parent 08e3894 commit b2b9265
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tools/load-cache.bat
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions tools/load-cache.sh
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit b2b9265

Please sign in to comment.