Added auto-restart capability

This commit is contained in:
2024-08-20 13:11:24 -05:00
parent 2e41004aca
commit 5c594c2223
3 changed files with 17 additions and 22 deletions
+3
View File
@@ -22,3 +22,6 @@ Thumbs.db
# Log files
*.log
telegram_bot.db
# Semaphore files
.doreboot
+7 -19
View File
@@ -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
}
+7 -3
View File
@@ -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()
application.run_polling()