Skip to content

Commit 66cce79

Browse files
authored
Merge branch 'fhammerl/run-sh-handle-update-containers' of https://github.com/actions/runner into fhammerl/mock-update
2 parents 4768372 + 1bf42ba commit 66cce79

File tree

6 files changed

+125
-71
lines changed

6 files changed

+125
-71
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
@echo off
2+
3+
"%~dp0\bin\Runner.Listener.exe" run %*
4+
5+
rem using `if %ERRORLEVEL% EQU N` insterad of `if ERRORLEVEL N`
6+
rem `if ERRORLEVEL N` means: error level is N or MORE
7+
8+
if %ERRORLEVEL% EQU 0 (
9+
echo "Runner listener exit with 0 return code, stop the service, no retry needed."
10+
exit /b 0
11+
)
12+
13+
if %ERRORLEVEL% EQU 1 (
14+
echo "Runner listener exit with terminated error, stop the service, no retry needed."
15+
exit /b 0
16+
)
17+
18+
if %ERRORLEVEL% EQU 2 (
19+
echo "Runner listener exit with retryable error, re-launch runner in 5 seconds."
20+
timeout /t 5 /nobreak > NUL
21+
exit /b 1
22+
)
23+
24+
if %ERRORLEVEL% EQU 3 (
25+
rem Sleep 5 seconds to wait for the runner update process finish
26+
echo "Runner listener exit because of updating, re-launch runner in 5 seconds"
27+
timeout /t 5 /nobreak > NUL
28+
exit /b 1
29+
)
30+
31+
if %ERRORLEVEL% EQU 4 (
32+
rem Sleep 5 seconds to wait for the ephemeral runner update process finish
33+
echo "Runner listener exit because of updating, re-launch ephemeral runner in 5 seconds"
34+
timeout /t 5 /nobreak > NUL
35+
exit /b 1
36+
)
37+
38+
echo "Exiting after unknown error code: %ERRORLEVEL%"
39+
exit /b 0
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
exit 0
34+
elif [[ $returnCode == 1 ]]; then
35+
echo "Runner listener exit with terminated error, stop the service, no retry needed."
36+
exit 0
37+
elif [[ $returnCode == 2 ]]; then
38+
echo "Runner listener exit with retryable error, re-launch runner in 5 seconds."
39+
safe_sleep
40+
exit 1
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+
safe_sleep
45+
exit 1
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+
safe_sleep
50+
exit 1
51+
else
52+
echo "Exiting with unknown error code: ${returnCode}"
53+
exit 0
54+
fi

src/Misc/layoutroot/run.cmd

+17-16
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,22 @@ if defined VERBOSE_ARG (
1313
rem Unblock files in the root of the layout folder. E.g. .cmd files.
1414
powershell.exe -NoLogo -Sta -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command "$VerbosePreference = %VERBOSE_ARG% ; Get-ChildItem -LiteralPath '%~dp0' | ForEach-Object { Write-Verbose ('Unblock: {0}' -f $_.FullName) ; $_ } | Unblock-File | Out-Null"
1515

16-
if /i "%~1" equ "localRun" (
17-
rem ********************************************************************************
18-
rem Local run.
19-
rem ********************************************************************************
20-
"%~dp0bin\Runner.Listener.exe" %*
21-
) else (
22-
rem ********************************************************************************
23-
rem Run.
24-
rem ********************************************************************************
25-
"%~dp0bin\Runner.Listener.exe" run %*
2616

27-
rem Return code 4 means the run once runner received an update message.
28-
rem Sleep 5 seconds to wait for the update process finish and run the runner again.
29-
if ERRORLEVEL 4 (
30-
timeout /t 5 /nobreak > NUL
31-
"%~dp0bin\Runner.Listener.exe" run %*
32-
)
17+
rem ********************************************************************************
18+
rem Run.
19+
rem ********************************************************************************
20+
21+
:launch_helper
22+
copy run-helper.cmd.template run-helper.cmd /Y
23+
call "%~dp0run-helper.cmd" %*
24+
25+
rem using `if %ERRORLEVEL% EQU N` insterad of `if ERRORLEVEL N`
26+
rem `if ERRORLEVEL N` means: error level is N or MORE
27+
28+
if %ERRORLEVEL% EQU 1 (
29+
echo "Restarting runner..."
30+
goto :launch_helper
31+
) else (
32+
echo "Exiting runner..."
33+
exit 0
3334
)

src/Misc/layoutroot/run.sh

+13-53
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,24 @@
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]}"
136
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
14-
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
15-
SOURCE="$(readlink "$SOURCE")"
16-
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
7+
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
8+
SOURCE="$(readlink "$SOURCE")"
9+
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
1710
done
1811
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
19-
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-
if [[ "$1" == "localRun" ]]; then
25-
"$DIR"/bin/Runner.Listener $*
26-
else
27-
"$DIR"/bin/Runner.Listener run $*
28-
29-
# Return code 3 means the run once runner received an update message.
30-
# Sleep 5 seconds to wait for the update process finish
12+
cp -f run-helper.sh.template run-helper.sh
13+
# run the helper process which keep the listener alive
14+
while :;
15+
do
16+
"$DIR"/run-helper.sh $*
3117
returnCode=$?
32-
if [[ $returnCode == 3 ]]; then
33-
if [ ! -x "$(command -v sleep)" ]; then
34-
if [ ! -x "$(command -v ping)" ]; then
35-
COUNT="0"
36-
while [[ $COUNT != 5000 ]]; do
37-
echo "SLEEP" > /dev/null
38-
COUNT=$[$COUNT+1]
39-
done
40-
else
41-
ping -c 5 127.0.0.1 > /dev/null
42-
fi
43-
else
44-
sleep 5
45-
fi
46-
elif [[ $returnCode == 4 ]]; then
47-
if [ ! -x "$(command -v sleep)" ]; then
48-
if [ ! -x "$(command -v ping)" ]; then
49-
COUNT="0"
50-
while [[ $COUNT != 5000 ]]; do
51-
echo "SLEEP" > /dev/null
52-
COUNT=$[$COUNT+1]
53-
done
54-
else
55-
ping -c 5 127.0.0.1 > /dev/null
56-
fi
57-
else
58-
sleep 5
59-
fi
60-
"$DIR"/bin/Runner.Listener run $*
18+
if [[ $returnCode == 1 ]]; then
19+
echo "Restarting runner..."
6120
else
62-
exit $returnCode
21+
echo "Exiting runner..."
22+
exit 0
6323
fi
64-
fi
24+
done

src/Runner.Listener/Configuration/ConfigurationManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public RunnerSettings LoadSettings()
5454
Trace.Info(nameof(LoadSettings));
5555
if (!IsConfigured())
5656
{
57-
throw new InvalidOperationException("Not configured. Run config.(sh/cmd) to configure the runner.");
57+
throw new NonRetryableException("Not configured. Run config.(sh/cmd) to configure the runner.");
5858
}
5959

6060
RunnerSettings settings = _store.GetSettings();

src/Runner.Listener/Runner.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ private async Task<int> RunAsync(RunnerSettings settings, bool runOnce = false)
428428
}
429429
}
430430
var selfUpdater = HostContext.GetService<ISelfUpdater>();
431-
selfUpdateTask = selfUpdater.SelfUpdate(runnerUpdateMessage, jobDispatcher, !runOnce && HostContext.StartupType != StartupType.Service, HostContext.RunnerShutdownToken);
431+
selfUpdateTask = selfUpdater.SelfUpdate(runnerUpdateMessage, jobDispatcher, false, HostContext.RunnerShutdownToken);
432432
Trace.Info("Refresh message received, kick-off selfupdate background process.");
433433
}
434434
else

0 commit comments

Comments
 (0)