From 2a15ca4c4ab0ee884d2d1a03c97762747b3a5702 Mon Sep 17 00:00:00 2001 From: bucolucas Date: Tue, 20 Aug 2024 12:30:05 -0500 Subject: [PATCH] Add PowerShell script to run Python file with restart logic --- run_python_with_restart.ps1 | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 run_python_with_restart.ps1 diff --git a/run_python_with_restart.ps1 b/run_python_with_restart.ps1 new file mode 100644 index 0000000..4ce8653 --- /dev/null +++ b/run_python_with_restart.ps1 @@ -0,0 +1,49 @@ +param( + [Parameter(Mandatory=$true)] + [string]$PythonFile, + [int]$SpecialExitCode = 42, + [int]$MaxAttempts = 20 +) + +function Run-PythonScript { + param($ScriptPath) + $process = Start-Process -FilePath "python" -ArgumentList $ScriptPath -PassThru -Wait -NoNewWindow + return $process.ExitCode +} + +function Git-Pull { + git pull + return $LASTEXITCODE -eq 0 +} + +$attempt = 1 +$waitTime = 15 + +while ($attempt -le $MaxAttempts) { + Write-Host "Attempt $attempt of $MaxAttempts" + + $exitCode = Run-PythonScript -ScriptPath $PythonFile + + if ($exitCode -eq $SpecialExitCode) { + Write-Host "Special exit code detected. Attempting git pull..." + + if (Git-Pull) { + Write-Host "Git pull successful. Restarting Python script..." + $attempt = 1 + $waitTime = 15 + continue + } else { + Write-Host "Git pull failed. Waiting $waitTime seconds before next attempt..." + } + } else { + Write-Host "Python script exited with code $exitCode. Exiting..." + exit $exitCode + } + + Start-Sleep -Seconds $waitTime + $attempt++ + $waitTime *= 2 +} + +Write-Host "Maximum attempts reached. Exiting..." +exit 1 \ No newline at end of file