diff --git a/src/Misc/layoutbin/update.cmd.template b/src/Misc/layoutbin/update.cmd.template index 70d86fd60..66267a727 100644 --- a/src/Misc/layoutbin/update.cmd.template +++ b/src/Misc/layoutbin/update.cmd.template @@ -120,6 +120,9 @@ if ERRORLEVEL 1 ( echo [%date% %time%] Update succeed >> "%logfile%" 2>&1 +type nul > update.finished +echo [%date% %time%] update.finished file creation succeed >> "%logfile%" 2>&1 + rem rename the update log file with %logfile%.succeed/.failed/succeedneedrestart rem runner service host can base on the log file name determin the result of the runner update echo [%date% %time%] Rename "%logfile%" to be "%logfile%.succeed" >> "%logfile%" 2>&1 diff --git a/src/Misc/layoutbin/update.sh.template b/src/Misc/layoutbin/update.sh.template index 8c9f508a0..4ca6b00e6 100755 --- a/src/Misc/layoutbin/update.sh.template +++ b/src/Misc/layoutbin/update.sh.template @@ -180,6 +180,9 @@ fi date "+[%F %T-%4N] Update succeed" >> "$logfile" +touch update.finished +date "+[%F %T-%4N] update.finished file creation succeed" >> "$logfile" + # rename the update log file with %logfile%.succeed/.failed/succeedneedrestart # runner service host can base on the log file name determin the result of the runner update date "+[%F %T-%4N] Rename $logfile to be $logfile.succeed" >> "$logfile" 2>&1 diff --git a/src/Misc/layoutroot/run-helper.cmd.template b/src/Misc/layoutroot/run-helper.cmd.template index 562038336..23e42467b 100644 --- a/src/Misc/layoutroot/run-helper.cmd.template +++ b/src/Misc/layoutroot/run-helper.cmd.template @@ -1,5 +1,5 @@ @echo off - +SET UPDATEFILE=update.finished "%~dp0\bin\Runner.Listener.exe" run %* rem using `if %ERRORLEVEL% EQU N` insterad of `if ERRORLEVEL N` @@ -22,16 +22,30 @@ if %ERRORLEVEL% EQU 2 ( ) if %ERRORLEVEL% EQU 3 ( - rem Sleep 5 seconds to wait for the runner update process finish - echo "Runner listener exit because of updating, re-launch runner in 5 seconds" - ping 127.0.0.1 -n 6 -w 1000 >NUL + rem Wait for 30 seconds or for flag file to exists for the ephemeral runner update process finish + echo "Runner listener exit because of updating, re-launch runner after successful update" + FOR /L %%G IN (1,1,30) DO ( + IF EXIST %UPDATEFILE% ( + echo "Update finished successfully." + del %FILE% + exit /b 1 + ) + ping 127.0.0.1 -n 2 -w 1000 >NUL + ) exit /b 1 ) if %ERRORLEVEL% EQU 4 ( - rem Sleep 5 seconds to wait for the ephemeral runner update process finish - echo "Runner listener exit because of updating, re-launch ephemeral runner in 5 seconds" - ping 127.0.0.1 -n 6 -w 1000 >NUL + rem Wait for 30 seconds or for flag file to exists for the runner update process finish + echo "Runner listener exit because of updating, re-launch runner after successful update" + FOR /L %%G IN (1,1,30) DO ( + IF EXIST %UPDATEFILE% ( + echo "Update finished successfully." + del %FILE% + exit /b 1 + ) + ping 127.0.0.1 -n 2 -w 1000 >NUL + ) exit /b 1 ) diff --git a/src/Misc/layoutroot/run-helper.sh.template b/src/Misc/layoutroot/run-helper.sh.template index 831ae3a3f..42f11eb01 100755 --- a/src/Misc/layoutroot/run-helper.sh.template +++ b/src/Misc/layoutroot/run-helper.sh.template @@ -17,6 +17,8 @@ while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symli [[ $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 done DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + +updateFile="update.finished" "$DIR"/bin/Runner.Listener run $* returnCode=$? @@ -31,14 +33,28 @@ elif [[ $returnCode == 2 ]]; then "$DIR"/safe_sleep.sh 5 exit 2 elif [[ $returnCode == 3 ]]; then - # Sleep 5 seconds to wait for the runner update process finish - echo "Runner listener exit because of updating, re-launch runner in 5 seconds" - "$DIR"/safe_sleep.sh 5 + # Wait for 30 seconds or for flag file to exists for the runner update process finish + echo "Runner listener exit because of updating, re-launch runner after successful update" + for i in {0..30} do + if test -f "$updateFile"; then + echo "Update finished successfully." + rm "$updateFile" + break + fi + "$DIR"/safe_sleep.sh 1 + done exit 2 elif [[ $returnCode == 4 ]]; then - # Sleep 5 seconds to wait for the ephemeral runner update process finish - echo "Runner listener exit because of updating, re-launch ephemeral runner in 5 seconds" - "$DIR"/safe_sleep.sh 5 + # Wait for 30 seconds or for flag file to exists for the ephemeral runner update process finish + echo "Runner listener exit because of updating, re-launch runner after successful update" + for i in {0..30} do + if test -f "$updateFile"; then + echo "Update finished successfully." + rm "$updateFile" + break + fi + "$DIR"/safe_sleep.sh 1 + done exit 2 else echo "Exiting with unknown error code: ${returnCode}" diff --git a/src/Runner.Listener/SelfUpdater.cs b/src/Runner.Listener/SelfUpdater.cs index 81e2d1dec..3c8242402 100644 --- a/src/Runner.Listener/SelfUpdater.cs +++ b/src/Runner.Listener/SelfUpdater.cs @@ -131,6 +131,8 @@ namespace GitHub.Runner.Listener // For L0, we will skip execute update script. if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("_GITHUB_ACTION_EXECUTE_UPDATE_SCRIPT"))) { + string flagFile = "update.finished"; + IOUtil.DeleteFile(flagFile); // kick off update script Process invokeScript = new Process(); #if OS_WINDOWS @@ -294,12 +296,12 @@ namespace GitHub.Runner.Listener archiveFile = Path.Combine(HostContext.GetDirectory(WellKnownDirectory.Root), $"runner{targetVersion}.tar.gz"); } - if (File.Exists(archiveFile)) + if (File.Exists(archiveFile)) { _updateTrace.Enqueue($"Mocking update with file: '{archiveFile}' and targetVersion: '{targetVersion}', nothing is downloaded"); _terminal.WriteLine($"Mocking update with file: '{archiveFile}' and targetVersion: '{targetVersion}', nothing is downloaded"); } - else + else { archiveFile = null; _terminal.WriteLine($"Mock runner archive not found at {archiveFile} for target version {targetVersion}, proceeding with download instead");