mad/plugins/AutoResetAndRetryDownloads.sh
2024-11-08 09:32:54 +00:00

93 lines
5.5 KiB
Bash
Executable file

#! Name: AutoResetAndRetryDownloads.sh
#! Author: kittykat
#! Version: 2024.09.27
#! Desc: Runs mad.sh reset after processing all urls and then relaunches MAD
#! Usage: Edit LoadPlugin="" line in mad.sh or mad.config
#! LoadPlugin="AutoResetAndRetryDownloads.sh"
#!
#! Plugin System: Additional Hook Functions added for easy of overriding:
#!
#! To Hook a Function, your function must begin with the hookable function name, and be unique
#! Best practice: HooKFuncName_<yourfilename>
#! ie.
#! PostSuccessfulDownload_myfilename() {
#! }
#!
#! Available Hook Functions:
#! -------------------------
#! * OnLoad(): Occurs after load mad.config / load plugins (prior to processing).
#! * BeginProcessing(): Occurs immediately after beginning processin of urls.txt (loops with Catnaps).
#! * PreProcessUrl(): occurs immediately after reading in an unprocessed url (^http) line to process.
#! * PostSuccessfulDownload(): occurs after a download success (is marked #OK# in the urls.txt).
#! * PostFailedDownload(): occurs after a download fails (is marked #FAIL# in the urls.txt).
#! * PostFailRetryDownload(): occurs after a download fails with a retry (is marked #RETRY# in the urls.txt).
#! * DoneProcessingAllUrls(): occurs after all the urls have finished processing (no flocks or other terms downloading).
#! * PostSuccessfulUpload(): occurs after an upload success (after upload completed ticket is created in ./downloads/).
#! * PostFailedUpload(): occurs after an upload fails definitively -- #FAIL# in the temp_upload_handler.txt
#! * PostFailRetryUpload(): occurs after an upload fails with a retry (network drop, unexpected result)
#! * DoneProcessingAllUploads: occurs after alll the files have finished processing
#!
DoneProcessingAllUrls_AutoResetAndRetryDownloads() {
local plugName='AutoResetAndRetryDownloads'
local plugFunc='DoneProcessingAllUrls_AutoResetAndRetryDownloads'
if [ "${DebugPluginsEnabled}" == "true" ]; then
echo -e "[${PINK}DEBUG${NC}]: Running ${PINK}$plugFunc${NC} in ${BLUE}$plugName${NC} ...${NC}"
fi
local pInputFile="$1"
arard_ticketactivetimeout=60
arard_ticketexpirytimeout=540
if [ "$LoopThroughFileUntilComplete" == "true" ] && [ ! -z $CatnapDuration ] && ((CatnapDuration > 0)); then
arard_ticketactivetimeout=$(( arard_ticketactivetimeout + (CatnapDuration * 60) ))
arard_ticketexpirytimeout=$(( arard_ticketexpirytimeout + (CatnapDuration * 60) ))
fi
if grep -Eqi '^#(http.*#RETRY#|direct=http.*#RETRY#).*$' "$pInputFile" ; then
if [ ! -f "${WorkDir}/.temp/AutoResetAndRetryDownloadsarard_startTime.txt" ]; then
echo $(date +%s) > "${WorkDir}/.temp/AutoResetAndRetryDownloadsarard_startTime.txt"
else
arard_startTime=$(cat "${WorkDir}/.temp/AutoResetAndRetryDownloadsarard_startTime.txt")
arard_currTime=$(date +%s)
arard_durationseconds=$((arard_currTime - arard_startTime))
if ((arard_durationseconds > arard_ticketexpirytimeout)); then
if [ "${DebugPluginsEnabled}" == "true" ]; then
echo -e "[${PINK}DEBUG${NC}]: Ticket expiry occurred ($arard_durationseconds > $arard_ticketexpirytimeout)..${NC}"
fi
echo $(date +%s) > "${WorkDir}/.temp/AutoResetAndRetryDownloadsarard_startTime.txt"
arard_startTime=$(cat "${WorkDir}/.temp/AutoResetAndRetryDownloadsarard_startTime.txt")
arard_currTime=$(date +%s)
arard_durationseconds=$((arard_currTime - arard_startTime))
elif ((arard_durationseconds > arard_ticketactivetimeout)); then
if [ "${DebugPluginsEnabled}" == "true" ]; then
echo -e "[${PINK}DEBUG${NC}]: Ticket inactive ($arard_durationseconds > $arard_ticketactivetimeout)..${NC}"
fi
for ((a=1; a<=2; a++)); do
printf "\033[1A\r"
printf "\33[2K\r"
done
echo -e "${NC}"
echo -e "${YELLOW}Reset&Retry${NC}: Done processing... ${BLUE}$((arard_durationseconds/60))${NC} min(s). Expiry ${PINK}$(( (arard_ticketexpirytimeout - arard_durationseconds)/60 ))${NC} min(s).${NC}"
return 0
fi
if [ "${DebugPluginsEnabled}" == "true" ]; then
echo -e "[${PINK}DEBUG${NC}]: Ticket active ($arard_durationseconds)..${NC}"
fi
for ((a=1; a<=2; a++)); do
printf "\033[1A\r"
printf "\33[2K\r"
done
echo -e "${NC}"
echo -e "${YELLOW}Reset&Retry${NC}: Dozing ${BLUE}1${NC} min. Processing for ${BLUE}$((arard_durationseconds/60))${NC} of ${BLUE}$((arard_ticketactivetimeout/60))${NC} min(s)${NC}. Expiry ${PINK}$(( (arard_ticketexpirytimeout - arard_durationseconds)/60 ))${NC} min(s).${NC}"
sleep 1m
fi
if [ ! -f "${WorkDir}/.temp/AutoResetAndRetryDownloadsCnt.lk" ]; then
touch "${WorkDir}/.temp/AutoResetAndRetryDownloadsCnt.lk"
trap "rm -f ${WorkDir}/.temp/AutoResetAndRetryDownloadsCnt.lk; echo ""; tput cnorm; exit" 0 1 2 3 6 15
madReset "$pInputFile" "true" "false" "false" "true"
rm -f "${WorkDir}/.temp/AutoResetAndRetryDownloadsCnt.lk"
fi
qChkLineCount=$(grep -Ei '^(http|direct=http)' "${pInputFile}" | wc -l | awk '{ print $1 }')
if ((qChkLineCount > 0)) ; then
ReloadScript "$@"
fi
fi
return 0
}