From 5c594c22238aa57711296836d748254a55691f7a Mon Sep 17 00:00:00 2001 From: Jonathan Lucas Date: Tue, 20 Aug 2024 13:11:24 -0500 Subject: [PATCH] Added auto-restart capability --- .gitignore | 3 +++ run_python_with_restart.ps1 | 26 +++++++------------------- telegram_helper.py | 10 +++++++--- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 9e1f108..bf1837f 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,6 @@ Thumbs.db # Log files *.log telegram_bot.db + +# Semaphore files +.doreboot \ No newline at end of file diff --git a/run_python_with_restart.ps1 b/run_python_with_restart.ps1 index a5a3825..253542b 100644 --- a/run_python_with_restart.ps1 +++ b/run_python_with_restart.ps1 @@ -1,14 +1,11 @@ param( [Parameter(Mandatory=$true)] - [string]$PythonFile, - [int]$SpecialExitCode = 42, - [int]$MaxAttempts = 20 + [string]$PythonFile ) function Run-PythonScript { param($ScriptPath) $process = Start-Process -FilePath "python" -ArgumentList $ScriptPath -PassThru -Wait -NoNewWindow - Write-Host "Process details: $($process | Format-List | Out-String)" # Added line return $process.ExitCode } @@ -17,34 +14,25 @@ function Git-Pull { return $LASTEXITCODE -eq 0 } -$attempt = 1 $waitTime = 15 - -while ($attempt -le $MaxAttempts) { - Write-Host "Attempt $attempt of $MaxAttempts" - +while ($true) { $exitCode = Run-PythonScript -ScriptPath $PythonFile - if ($exitCode -eq $SpecialExitCode) { - Write-Host "Special exit code detected. Attempting git pull..." + if (Test-Path -Path ".\.doreboot") { + + Write-Host "Special filename 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 + exit 1 } Start-Sleep -Seconds $waitTime - $attempt++ $waitTime *= 2 -} - -Write-Host "Maximum attempts reached. Exiting..." -exit 1 \ No newline at end of file +} \ No newline at end of file diff --git a/telegram_helper.py b/telegram_helper.py index 1164ff4..99f4d7e 100644 --- a/telegram_helper.py +++ b/telegram_helper.py @@ -73,8 +73,11 @@ class TelegramHelper: async def reboot(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: await update.message.reply_text("Rebooting the bot...") - logging.info("Received reboot command. Exiting with status code 355.") - sys.exit(355) + logging.info("Received reboot command. Exiting process...") + reboot_file_path = "./.doreboot" + if not os.path.exists(reboot_file_path): + open(reboot_file_path, 'w').close() + sys.exit(0) def run(self): application = Application.builder().token(self.telegram_bot_token).build() @@ -88,4 +91,5 @@ class TelegramHelper: application.add_handler(CallbackQueryHandler(self.abort_processing, pattern='^abort$')) logging.info("Bot is running...") - application.run_polling() \ No newline at end of file + application.run_polling() + \ No newline at end of file