fix: Correct loop behavior, add parameter to reserve intensive optimizations

This commit is contained in:
Carlos Solís 2025-02-10 03:25:14 +00:00
parent 4924cd7933
commit d989d762f1

View file

@ -6,14 +6,20 @@ group=www-data
fileperm=660 fileperm=660
db=friendica db=friendica
folder=/var/www/friendica folder=/var/www/friendica
nfile=/tmp/n.csv intense_optimizations=${1:-"0"}
nlock=/tmp/n.lock thread_multiplier=1
if [[ -f ${nfile} ]]; then nfolder="/tmpʾfriendica-remove-invalid-photos"
nfile="${nfolder}/n$(date +%s).csv"
nlock="${nfolder}/n$(date +%s).lock"
if [[ ! -d "${nfolder}" ]]; then
mkdir "${nfolder}"
fi
if [[ -f "${nfile}" ]]; then
rm -rf "${nfile}" && touch "${nfile}" rm -rf "${nfile}" && touch "${nfile}"
else else
touch "${nfile}" touch "${nfile}"
fi fi
if [[ -f ${nlock} ]]; then if [[ -f "${nlock}" ]]; then
rm -rf "${nlock}" rm -rf "${nlock}"
fi fi
#Internal parameters: #Internal parameters:
@ -27,12 +33,15 @@ lastid=0
maxid=$(mariadb "${db}" -B -N -q -e "select max(\`id\`) from contact") maxid=$(mariadb "${db}" -B -N -q -e "select max(\`id\`) from contact")
#Limit per batch #Limit per batch
limit=$(((maxid / 1000) + 1)) limit=$(((maxid / 1000) + 1))
#https:// = 8 characters | /avatar/ = 8 characters dbcount=0
#indexlength=$(("${#url}" + 16)) if [[ "${intense_optimizations}" -gt 0 ]]; then
#mariadb "${db}" -e "alter table contact add index if not exists photo_index (photo(${indexlength}))" #https:// = 8 characters | /avatar/ = 8 characters
#Add to the loop, reset values indexlength=$(("${#url}" + 16))
#dbcount=$(mariadb "${db}" -B -N -q -e "select count(\`id\`) from contact where photo like 'https:\/\/${url}/avatar/%' and (id in (select cid from \`user-contact\`) or id in (select \`uid\` from \`user\`) or \`id\` in (select \`contact-id\` from \`group_member\`))") mariadb "${db}" -e "alter table contact add index if not exists photo_index (photo(${indexlength}))"
dbcount=$(mariadb "${db}" -B -N -q -e "select count(\`id\`) from contact where photo like 'https:\/\/${url}/avatar/%'") dbcount=$(mariadb "${db}" -B -N -q -e "select count(\`id\`) from contact where photo like 'https:\/\/${url}/avatar/%'")
else
dbcount=$(mariadb "${db}" -B -N -q -e "select count(\`id\`) from contact where photo like 'https:\/\/${url}/avatar/%' and (id in (select cid from \`user-contact\`) or id in (select \`uid\` from \`user\`) or \`id\` in (select \`contact-id\` from \`group_member\`))")
fi
loop() { loop() {
result_string="" result_string=""
@ -148,6 +157,9 @@ loop() {
fi fi
else else
result_string=$(printf "%s No remote" "${result_string}") result_string=$(printf "%s No remote" "${result_string}")
#If the avatar is not valid, set it as blank in the database
mariadb "${db}" -N -B -q -e "update contact set avatar= \"\", photo = \"\", thumb = \"\", micro = \"\" where id = \"${id}\"" &
result_string=$(printf "%s (blanked)" "${result_string}")
#If no remote avatar is found, we would blank the photo/thumb/micro and let the avatar cache process fix them later, but it's empty already here #If no remote avatar is found, we would blank the photo/thumb/micro and let the avatar cache process fix them later, but it's empty already here
error_found=1 error_found=1
fi fi
@ -207,15 +219,30 @@ loop() {
cd "${folder}" || exit cd "${folder}" || exit
echo "${n} ${nt}" >"${nfile}" echo "${n} ${nt}" >"${nfile}"
until [[ $((nt + limit)) -gt "${dbcount}" ]]; do until [[ $((nt + limit)) -gt "${dbcount}" ]]; do
c=""
if [[ "${intense_optimizations}" -gt 0 ]]; then
c=$(mariadb "${db}" -B -N -q -e "select \`id\` from \`contact\` where \`id\` > ${lastid} and (\`photo\` like \"https:\/\/${url}/avatar/%\" or \`photo\` like \"\") order by id limit ${limit}")
else
c=$(mariadb "${db}" -B -N -q -e "select \`id\` from \`contact\` where \`id\` > ${lastid} and (\`photo\` like \"https:\/\/${url}/avatar/%\" or \`photo\` like \"\") and (id in (select cid from \`user-contact\`) or id in (select \`uid\` from \`user\`) or \`id\` in (select \`contact-id\` from \`group_member\`)) order by id limit ${limit}")
fi
while read -r id; do while read -r id; do
lastid="${id}" lastid="${id}"
loop & loop &
until [[ $(jobs -r -p | wc -l) -lt $(($(getconf _NPROCESSORS_ONLN) * 1)) ]]; do until [[ $(jobs -r -p | wc -l) -lt $(($(getconf _NPROCESSORS_ONLN) * thread_multiplier)) ]]; do
wait -n wait -n
done done
done < <(mariadb "${db}" -B -N -q -e "select \`id\` from \`contact\` where \`id\` > ${lastid} and (\`photo\` like \"https:\/\/${url}/avatar/%\" or \`photo\` like \"\") order by id limit ${limit}") done < <(echo "${c}")
wait wait
done done
rm -rf "${nfile}" "${nlock}" if [[ -f "${nfile}" ]]; then
#mariadb "${db}" -e "alter table contact drop index photo_index" rm -rf "${nfile}"
#printf "\nFixing folders and moving to avatar cache...\n" fi
if [[ -f "${nlock}" ]]; then
rm -rf "${nlock}"
fi
if [[ ! -d "${nfolder}" && $(find "${nfolder}" | wc -l) -eq 0 ]]; then
rm -rf "${nfolder}"
fi
if [[ "${intense_optimizations}" -gt 0 ]]; then
mariadb "${db}" -e "alter table contact drop index photo_index"
fi