Skip to content

Commit

Permalink
install_packages: add basic 5-time retry for Other Apt Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Botspot committed Sep 2, 2023
1 parent 54af706 commit ad4b235
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions api
Original file line number Diff line number Diff line change
Expand Up @@ -518,19 +518,6 @@ Package: $package_name" > ~/$package_name/DEBIAN/control
echo "$package_name is already installed and no changes would be made. Skipping..."
else

#Before apt update, check if local repo still exists
if [ "$using_local_packages" == 1 ] && [ ! -f /tmp/pi-apps-local-packages/Packages ];then
error "User error: Uh-oh, the /tmp/pi-apps-local-packages folder went missing while installing packages.\nThis usually happens if you try to install several apps at the same time in multiple terminals."
fi

#run an apt update
apt_update "${apt_flags[@]}" || exit 1

#After apt update, check again if local repo still exists
if [ "$using_local_packages" == 1 ] && [ ! -f /tmp/pi-apps-local-packages/Packages ];then
error "User error: Uh-oh, the /tmp/pi-apps-local-packages folder went missing while installing packages.\nThis usually happens if you try to install several apps at the same time in multiple terminals."
fi

#Build .deb file for the dummy package
local output="$(dpkg-deb --build ~/$package_name 2>&1)"
if [ $? != 0 ] || [ ! -f ~/$package_name.deb ];then
Expand All @@ -539,12 +526,32 @@ Package: $package_name" > ~/$package_name/DEBIAN/control
error "install_packages(): failed to create dummy deb ${package_name}!"
fi

#install dummy deb
status "Installing the $package_name package..."
#Before apt update, check if local repo still exists
if [ "$using_local_packages" == 1 ] && [ ! -f /tmp/pi-apps-local-packages/Packages ];then
error "User error: Uh-oh, the /tmp/pi-apps-local-packages folder went missing while installing packages.\nThis usually happens if you try to install several apps at the same time in multiple terminals."
fi

apt_lock_wait
local output="$(sudo -E apt install -fy --no-install-recommends --allow-downgrades "${apt_flags[@]}" ~/$package_name.deb 2>&1 | less_apt | tee /dev/stderr)"
status "Apt finished."
local i=0
while [ $i -le 5 ];do #retry loop ; run apt update and apt install again if apt's records of the local repo goes missing
#run an apt update
apt_update "${apt_flags[@]}" || exit 1

#install dummy deb
status "Installing the $package_name package..."

apt_lock_wait
local output="$(sudo -E apt install -fy --no-install-recommends --allow-downgrades "${apt_flags[@]}" ~/$package_name.deb 2>&1 | less_apt | tee /dev/stderr)"
status "Apt finished."

if [ "$using_local_packages" == 1 ] && [ ! -f /var/lib/apt/lists/_tmp_pi-apps-local-packages_._Packages ];then
#another apt update process deleted apt's knowledge of the pi-apps local repo. Warn the user and try again, up to 5 tries.
i=$((i+1))
warning "Local packages failed to install because another apt update process erased apt's knowledge of the pi-apps local repository.\nTrying again... (attempt $i of 5)"

This comment has been minimized.

Copy link
@theofficialgman

theofficialgman Sep 2, 2023

Collaborator

actually this will run 6 times. see stripped down version of what is here:

i=0
while [ $i -le 5 ];do
i=$((i+1))
echo "(attempt $i of 5)"
done

check the code again. there are multiple ways to fix

This comment has been minimized.

Copy link
@Botspot

Botspot Sep 2, 2023

Author Owner

Ah yes, the off-by-one error. Thanks for noticing.
This will actually retry 5 times as intended, but on the 5th time it will tell the user it will try a 6th time, even though it will not. Will fix.

else
#apt may have succeeded or failed, but exit retry loop for Other Apt Update
break
fi
done

errors="$(echo "$output" | grep '^[(E)|(Err]:')"
if [ ! -z "$errors" ];then
Expand Down

0 comments on commit ad4b235

Please sign in to comment.