Skip to content

Commit 66dde76

Browse files
committed
Extract most os run.sh logic so we can update it
1 parent edb6483 commit 66dde76

File tree

2 files changed

+60
-51
lines changed

2 files changed

+60
-51
lines changed

src/Misc/layoutbin/run-helper.sh

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/bash
2+
3+
# Validate not sudo
4+
user_id=`id -u`
5+
if [ $user_id -eq 0 -a -z "$RUNNER_ALLOW_RUNASROOT" ]; then
6+
echo "Must not run interactively with sudo"
7+
exit 1
8+
fi
9+
10+
# Run
11+
shopt -s nocasematch
12+
13+
safe_sleep() {
14+
if [ ! -x "$(command -v sleep)" ]; then
15+
if [ ! -x "$(command -v ping)" ]; then
16+
COUNT="0"
17+
while [[ $COUNT != 5000 ]]; do
18+
echo "SLEEP" > /dev/null
19+
COUNT=$[$COUNT+1]
20+
done
21+
else
22+
ping -c 5 127.0.0.1 > /dev/null
23+
fi
24+
else
25+
sleep 5
26+
fi
27+
}
28+
29+
bin/Runner.Listener run $*
30+
returnCode=$?
31+
if [[ $returnCode == 0 ]]; then
32+
echo "Runner listener exit with 0 return code, stop the service, no retry needed."
33+
echo "0 ORIG"
34+
elif [[ $returnCode == 1 ]]; then
35+
echo "Runner listener exit with terminated error, stop the service, no retry needed."
36+
echo "1 ORIG"
37+
elif [[ $returnCode == 2 ]]; then
38+
echo "Runner listener exit with retryable error, re-launch runner in 5 seconds."
39+
echo "2 ORIG"
40+
safe_sleep
41+
elif [[ $returnCode == 3 ]]; then
42+
# Sleep 5 seconds to wait for the runner update process finish
43+
echo "Runner listener exit because of updating, re-launch runner in 5 seconds"
44+
echo "3 ORIG"
45+
safe_sleep
46+
elif [[ $returnCode == 4 ]]; then
47+
# Sleep 5 seconds to wait for the ephemeral runner update process finish
48+
echo "Runner listener exit because of updating, re-launch ephemeral runner in 5 seconds"
49+
echo "4 ORIG"
50+
safe_sleep
51+
else
52+
echo "Exiting with unknown error code: ${returnCode}"
53+
fi
54+
55+
exit $returnCode

src/Misc/layoutroot/run.sh

+5-51
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
#!/bin/bash
22

3-
# Validate not sudo
4-
user_id=`id -u`
5-
if [ $user_id -eq 0 -a -z "$RUNNER_ALLOW_RUNASROOT" ]; then
6-
echo "Must not run interactively with sudo"
7-
exit 1
8-
fi
9-
103
# Change directory to the script root directory
114
# https://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within
125
SOURCE="${BASH_SOURCE[0]}"
@@ -17,54 +10,15 @@ while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symli
1710
done
1811
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
1912

20-
# Do not "cd $DIR". For localRun, the current directory is expected to be the repo location on disk.
21-
22-
# Run
23-
shopt -s nocasematch
24-
25-
safe_sleep() {
26-
if [ ! -x "$(command -v sleep)" ]; then
27-
if [ ! -x "$(command -v ping)" ]; then
28-
COUNT="0"
29-
while [[ $COUNT != 5000 ]]; do
30-
echo "SLEEP" > /dev/null
31-
COUNT=$[$COUNT+1]
32-
done
33-
else
34-
ping -c 5 127.0.0.1 > /dev/null
35-
fi
36-
else
37-
sleep 5
38-
fi
39-
}
40-
13+
# run the helper process which keep the listener alive
4114
while :;
4215
do
43-
if [[ "$1" == "localRun" ]]; then
44-
"$DIR"/bin/Runner.Listener $*
45-
else
46-
"$DIR"/bin/Runner.Listener run $*
47-
fi
48-
16+
"$DIR"/bin/run-helper.sh $*
4917
returnCode=$?
50-
if [[ $returnCode == 0 ]]; then
51-
echo "Runner listener exit with 0 return code, stop the service, no retry needed."
52-
exit $returnCode
53-
elif [[ $returnCode == 1 ]]; then
54-
echo "Runner listener exit with terminated error, stop the service, no retry needed."
55-
exit $returnCode
56-
elif [[ $returnCode == 2 ]]; then
57-
echo "Runner listener exit with retryable error, re-launch runner in 5 seconds."
58-
safe_sleep
59-
elif [[ $returnCode == 3 ]]; then
60-
# Sleep 5 seconds to wait for the runner update process finish
61-
echo "Runner listener exit because of updating, re-launch runner in 5 seconds"
62-
safe_sleep
63-
elif [[ $returnCode == 4 ]]; then
64-
# Sleep 5 seconds to wait for the ephemeral runner update process finish
65-
echo "Runner listener exit because of updating, re-launch ephemeral runner in 5 seconds"
66-
safe_sleep
18+
if [[ $returnCode -ge 2 ]]; then
19+
echo "Restart runner after it exited with return code '${returnCode}'"
6720
else
21+
echo "Exit runner after it exited with return code '${returnCode}'"
6822
exit $returnCode
6923
fi
7024
done

0 commit comments

Comments
 (0)