Skip to content

Commit

Permalink
gupload: Check for access token in bg to update if required
Browse files Browse the repository at this point in the history
launch a background service to check access token and update it

checks ACCESS_TOKEN_EXPIRY, will update before 3 seconds of expiry

process will be killed when script exits

Fix labbots#113
  • Loading branch information
Akianonymus committed Oct 19, 2020
1 parent 75053f8 commit 424ca4d
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 4 deletions.
33 changes: 32 additions & 1 deletion bash/release/gupload
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,28 @@ _check_credentials() {
fi
}

[[ -z ${ACCESS_TOKEN} || ${ACCESS_TOKEN_EXPIRY} -lt "$(printf "%(%s)T\\n" "-1")" ]] && { _get_access_token_and_update || return 1; }
[[ -z ${ACCESS_TOKEN} || ${ACCESS_TOKEN_EXPIRY:-0} -lt "$(printf "%(%s)T\\n" "-1")" ]] && { _get_access_token_and_update || return 1; }

# launch a background service to check access token and update it
# checks ACCESS_TOKEN_EXPIRY, will update before 3 seconds of expiry
# process will be killed when script exits
{
while :; do
CURRENT_TIME="$(printf "%(%s)T\\n" "-1")"
REMAINING_TOKEN_TIME="$((ACCESS_TOKEN_EXPIRY - CURRENT_TIME))"
if [[ ${REMAINING_TOKEN_TIME} -le 3 ]]; then
_get_access_token_and_update
else
TOKEN_PROCESS_TIME_TO_SLEEP="$(if [[ ${REMAINING_TOKEN_TIME} -le 4 ]]; then
printf "0\n"
else
printf "%s\n" "$((REMAINING_TOKEN_TIME - 3))"
fi)"
sleep "${TOKEN_PROCESS_TIME_TO_SLEEP}"
fi
done
} &
ACCESS_TOKEN_SERVICE_PID="${!}"

return 0
}
Expand Down Expand Up @@ -1566,6 +1587,16 @@ main() {
_cleanup() {
{
[[ -n ${PARALLEL_UPLOAD} ]] && rm -f "${TMPFILE:?}"*

# grab all chidren processes of access token service
# https://askubuntu.com/a/512872
token_service_pids="$(ps --ppid="${ACCESS_TOKEN_SERVICE_PID}" -o pid=)"
# first kill parent id, then children processes
kill "${ACCESS_TOKEN_SERVICE_PID}"
for pid in ${token_service_pids}; do
kill "${pid}"
done

export abnormal_exit && if [[ -n ${abnormal_exit} ]]; then
printf "\n\n%s\n" "Script exited manually."
kill -- -$$ &
Expand Down
33 changes: 32 additions & 1 deletion bash/upload.bash
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,28 @@ _check_credentials() {
fi
}

[[ -z ${ACCESS_TOKEN} || ${ACCESS_TOKEN_EXPIRY} -lt "$(printf "%(%s)T\\n" "-1")" ]] && { _get_access_token_and_update || return 1; }
[[ -z ${ACCESS_TOKEN} || ${ACCESS_TOKEN_EXPIRY:-0} -lt "$(printf "%(%s)T\\n" "-1")" ]] && { _get_access_token_and_update || return 1; }

# launch a background service to check access token and update it
# checks ACCESS_TOKEN_EXPIRY, will update before 3 seconds of expiry
# process will be killed when script exits
{
while :; do
CURRENT_TIME="$(printf "%(%s)T\\n" "-1")"
REMAINING_TOKEN_TIME="$((ACCESS_TOKEN_EXPIRY - CURRENT_TIME))"
if [[ ${REMAINING_TOKEN_TIME} -le 3 ]]; then
_get_access_token_and_update
else
TOKEN_PROCESS_TIME_TO_SLEEP="$(if [[ ${REMAINING_TOKEN_TIME} -le 4 ]]; then
printf "0\n"
else
printf "%s\n" "$((REMAINING_TOKEN_TIME - 3))"
fi)"
sleep "${TOKEN_PROCESS_TIME_TO_SLEEP}"
fi
done
} &
ACCESS_TOKEN_SERVICE_PID="${!}"

return 0
}
Expand Down Expand Up @@ -640,6 +661,16 @@ main() {
_cleanup() {
{
[[ -n ${PARALLEL_UPLOAD} ]] && rm -f "${TMPFILE:?}"*

# grab all chidren processes of access token service
# https://askubuntu.com/a/512872
token_service_pids="$(ps --ppid="${ACCESS_TOKEN_SERVICE_PID}" -o pid=)"
# first kill parent id, then children processes
kill "${ACCESS_TOKEN_SERVICE_PID}"
for pid in ${token_service_pids}; do
kill "${pid}"
done

export abnormal_exit && if [[ -n ${abnormal_exit} ]]; then
printf "\n\n%s\n" "Script exited manually."
kill -- -$$ &
Expand Down
33 changes: 32 additions & 1 deletion sh/release/gupload
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,28 @@ _check_credentials() {
fi
}
[ -z "${ACCESS_TOKEN}" ] || [ "${ACCESS_TOKEN_EXPIRY}" -lt "$(date +'%s')" ] && { _get_access_token_and_update || return 1; }
[ -z "${ACCESS_TOKEN}" ] || [ "${ACCESS_TOKEN_EXPIRY:-0}" -lt "$(date +'%s')" ] && { _get_access_token_and_update || return 1; }
# launch a background service to check access token and update it
# checks ACCESS_TOKEN_EXPIRY, will update before 3 seconds of expiry
# process will be killed when script exits
{
while :; do
CURRENT_TIME="$(date +'%s')"
REMAINING_TOKEN_TIME="$((CURRENT_TIME - ACCESS_TOKEN_EXPIRY))"
if [ "${REMAINING_TOKEN_TIME}" -le 3 ]; then
_get_access_token_and_update
else
TOKEN_PROCESS_TIME_TO_SLEEP="$(if [ "${REMAINING_TOKEN_TIME}" -le 4 ]; then
printf "0\n"
else
printf "%s\n" "$((REMAINING_TOKEN_TIME - 3))"
fi)"
sleep "${TOKEN_PROCESS_TIME_TO_SLEEP}"
fi
done
} &
ACCESS_TOKEN_SERVICE_PID="${!}"
return 0
}
Expand Down Expand Up @@ -1575,6 +1596,16 @@ main() {
_cleanup() {
{
[ -n "${PARALLEL_UPLOAD}" ] && rm -f "${TMPFILE:?}"*
# grab all chidren processes of access token service
# https://askubuntu.com/a/512872
token_service_pids="$(ps --ppid="${ACCESS_TOKEN_SERVICE_PID}" -o pid=)"
# first kill parent id, then children processes
kill "${ACCESS_TOKEN_SERVICE_PID}"
for pid in ${token_service_pids}; do
kill "${pid}"
done
export abnormal_exit && if [ -n "${abnormal_exit}" ]; then
printf "\n\n%s\n" "Script exited manually."
kill -9 -$$ &
Expand Down
33 changes: 32 additions & 1 deletion sh/upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,28 @@ _check_credentials() {
fi
}
[ -z "${ACCESS_TOKEN}" ] || [ "${ACCESS_TOKEN_EXPIRY}" -lt "$(date +'%s')" ] && { _get_access_token_and_update || return 1; }
[ -z "${ACCESS_TOKEN}" ] || [ "${ACCESS_TOKEN_EXPIRY:-0}" -lt "$(date +'%s')" ] && { _get_access_token_and_update || return 1; }
# launch a background service to check access token and update it
# checks ACCESS_TOKEN_EXPIRY, will update before 3 seconds of expiry
# process will be killed when script exits
{
while :; do
CURRENT_TIME="$(date +'%s')"
REMAINING_TOKEN_TIME="$((CURRENT_TIME - ACCESS_TOKEN_EXPIRY))"
if [ "${REMAINING_TOKEN_TIME}" -le 3 ]; then
_get_access_token_and_update
else
TOKEN_PROCESS_TIME_TO_SLEEP="$(if [ "${REMAINING_TOKEN_TIME}" -le 4 ]; then
printf "0\n"
else
printf "%s\n" "$((REMAINING_TOKEN_TIME - 3))"
fi)"
sleep "${TOKEN_PROCESS_TIME_TO_SLEEP}"
fi
done
} &
ACCESS_TOKEN_SERVICE_PID="${!}"
return 0
}
Expand Down Expand Up @@ -666,6 +687,16 @@ main() {
_cleanup() {
{
[ -n "${PARALLEL_UPLOAD}" ] && rm -f "${TMPFILE:?}"*
# grab all chidren processes of access token service
# https://askubuntu.com/a/512872
token_service_pids="$(ps --ppid="${ACCESS_TOKEN_SERVICE_PID}" -o pid=)"
# first kill parent id, then children processes
kill "${ACCESS_TOKEN_SERVICE_PID}"
for pid in ${token_service_pids}; do
kill "${pid}"
done
export abnormal_exit && if [ -n "${abnormal_exit}" ]; then
printf "\n\n%s\n" "Script exited manually."
kill -9 -$$ &
Expand Down

0 comments on commit 424ca4d

Please sign in to comment.