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, try to update before 5 mins of expiry, a fresh token gets 60 mins ( 3600 seconds )

process will be killed when script exits

Fix labbots#113
  • Loading branch information
Akianonymus committed Oct 20, 2020
1 parent 774d45f commit 621b51e
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ This repo contains two types of scripts, posix compatible and bash compatible.
| sed | Miscellaneous |
| mktemp | To generate temporary files ( optional ) |
| sleep | Self explanatory |
| ps | To manage different processes |

<strong>If BASH is not available or BASH is available but version is less tham 4.x, then below programs are also required:</strong>

Expand All @@ -124,11 +125,10 @@ This repo contains two types of scripts, posix compatible and bash compatible.
| cat | Miscellaneous |
| stty or zsh or tput | To determine column size ( optional ) |

<strong>These programs are needed for synchronisation script:</strong>
<strong>These are the additional programs needed for synchronisation script:</strong>

| Program | Role In Script |
| ------------- | ------------------------- |
| ps | To manage background jobs |
| tail | To show indefinite logs |

### Installation
Expand Down
35 changes: 34 additions & 1 deletion bash/release/gupload
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,30 @@ _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, try to update before 5 mins of expiry, a fresh token gets 60 mins
# 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 300 ]]; then
# timeout after 30 seconds, it shouldn't take too long anyway
_timeout 30 _get_access_token_and_update || :
else
TOKEN_PROCESS_TIME_TO_SLEEP="$(if [[ ${REMAINING_TOKEN_TIME} -le 301 ]]; then
printf "0\n"
else
printf "%s\n" "$((REMAINING_TOKEN_TIME - 300))"
fi)"
sleep "${TOKEN_PROCESS_TIME_TO_SLEEP}"
fi
sleep 1
done
} &
ACCESS_TOKEN_SERVICE_PID="${!}"

return 0
}
Expand Down Expand Up @@ -1566,6 +1589,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
35 changes: 34 additions & 1 deletion bash/upload.bash
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,30 @@ _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, try to update before 5 mins of expiry, a fresh token gets 60 mins
# 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 300 ]]; then
# timeout after 30 seconds, it shouldn't take too long anyway
_timeout 30 _get_access_token_and_update || :
else
TOKEN_PROCESS_TIME_TO_SLEEP="$(if [[ ${REMAINING_TOKEN_TIME} -le 301 ]]; then
printf "0\n"
else
printf "%s\n" "$((REMAINING_TOKEN_TIME - 300))"
fi)"
sleep "${TOKEN_PROCESS_TIME_TO_SLEEP}"
fi
sleep 1
done
} &
ACCESS_TOKEN_SERVICE_PID="${!}"

return 0
}
Expand Down Expand Up @@ -640,6 +663,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
35 changes: 34 additions & 1 deletion sh/release/gupload
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,30 @@ _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, try to update before 5 mins of expiry, a fresh token gets 60 mins
# 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 300 ]; then
# timeout after 30 seconds, it shouldn't take too long anyway
_timeout 30 _get_access_token_and_update || :
else
TOKEN_PROCESS_TIME_TO_SLEEP="$(if [ "${REMAINING_TOKEN_TIME}" -le 301 ]; then
printf "0\n"
else
printf "%s\n" "$((REMAINING_TOKEN_TIME - 300))"
fi)"
sleep "${TOKEN_PROCESS_TIME_TO_SLEEP}"
fi
sleep 1
done
} &
ACCESS_TOKEN_SERVICE_PID="${!}"
return 0
}
Expand Down Expand Up @@ -1575,6 +1598,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
35 changes: 34 additions & 1 deletion sh/upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,30 @@ _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, try to update before 5 mins of expiry, a fresh token gets 60 mins
# 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 300 ]; then
# timeout after 30 seconds, it shouldn't take too long anyway
_timeout 30 _get_access_token_and_update || :
else
TOKEN_PROCESS_TIME_TO_SLEEP="$(if [ "${REMAINING_TOKEN_TIME}" -le 301 ]; then
printf "0\n"
else
printf "%s\n" "$((REMAINING_TOKEN_TIME - 300))"
fi)"
sleep "${TOKEN_PROCESS_TIME_TO_SLEEP}"
fi
sleep 1
done
} &
ACCESS_TOKEN_SERVICE_PID="${!}"
return 0
}
Expand Down Expand Up @@ -666,6 +689,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 621b51e

Please sign in to comment.