Added auto-restart capability
This commit is contained in:
@@ -22,3 +22,6 @@ Thumbs.db
|
|||||||
# Log files
|
# Log files
|
||||||
*.log
|
*.log
|
||||||
telegram_bot.db
|
telegram_bot.db
|
||||||
|
|
||||||
|
# Semaphore files
|
||||||
|
.doreboot
|
||||||
@@ -1,14 +1,11 @@
|
|||||||
param(
|
param(
|
||||||
[Parameter(Mandatory=$true)]
|
[Parameter(Mandatory=$true)]
|
||||||
[string]$PythonFile,
|
[string]$PythonFile
|
||||||
[int]$SpecialExitCode = 42,
|
|
||||||
[int]$MaxAttempts = 20
|
|
||||||
)
|
)
|
||||||
|
|
||||||
function Run-PythonScript {
|
function Run-PythonScript {
|
||||||
param($ScriptPath)
|
param($ScriptPath)
|
||||||
$process = Start-Process -FilePath "python" -ArgumentList $ScriptPath -PassThru -Wait -NoNewWindow
|
$process = Start-Process -FilePath "python" -ArgumentList $ScriptPath -PassThru -Wait -NoNewWindow
|
||||||
Write-Host "Process details: $($process | Format-List | Out-String)" # Added line
|
|
||||||
return $process.ExitCode
|
return $process.ExitCode
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,34 +14,25 @@ function Git-Pull {
|
|||||||
return $LASTEXITCODE -eq 0
|
return $LASTEXITCODE -eq 0
|
||||||
}
|
}
|
||||||
|
|
||||||
$attempt = 1
|
|
||||||
$waitTime = 15
|
$waitTime = 15
|
||||||
|
while ($true) {
|
||||||
while ($attempt -le $MaxAttempts) {
|
|
||||||
Write-Host "Attempt $attempt of $MaxAttempts"
|
|
||||||
|
|
||||||
$exitCode = Run-PythonScript -ScriptPath $PythonFile
|
$exitCode = Run-PythonScript -ScriptPath $PythonFile
|
||||||
|
|
||||||
if ($exitCode -eq $SpecialExitCode) {
|
if (Test-Path -Path ".\.doreboot") {
|
||||||
Write-Host "Special exit code detected. Attempting git pull..."
|
|
||||||
|
Write-Host "Special filename detected. Attempting git pull..."
|
||||||
|
|
||||||
if (Git-Pull) {
|
if (Git-Pull) {
|
||||||
Write-Host "Git pull successful. Restarting Python script..."
|
Write-Host "Git pull successful. Restarting Python script..."
|
||||||
$attempt = 1
|
|
||||||
$waitTime = 15
|
$waitTime = 15
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
Write-Host "Git pull failed. Waiting $waitTime seconds before next attempt..."
|
Write-Host "Git pull failed. Waiting $waitTime seconds before next attempt..."
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Write-Host "Python script exited with code $exitCode. Exiting..."
|
exit 1
|
||||||
exit $exitCode
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Start-Sleep -Seconds $waitTime
|
Start-Sleep -Seconds $waitTime
|
||||||
$attempt++
|
|
||||||
$waitTime *= 2
|
$waitTime *= 2
|
||||||
}
|
}
|
||||||
|
|
||||||
Write-Host "Maximum attempts reached. Exiting..."
|
|
||||||
exit 1
|
|
||||||
+7
-3
@@ -73,8 +73,11 @@ class TelegramHelper:
|
|||||||
|
|
||||||
async def reboot(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
async def reboot(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
await update.message.reply_text("Rebooting the bot...")
|
await update.message.reply_text("Rebooting the bot...")
|
||||||
logging.info("Received reboot command. Exiting with status code 355.")
|
logging.info("Received reboot command. Exiting process...")
|
||||||
sys.exit(355)
|
reboot_file_path = "./.doreboot"
|
||||||
|
if not os.path.exists(reboot_file_path):
|
||||||
|
open(reboot_file_path, 'w').close()
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
application = Application.builder().token(self.telegram_bot_token).build()
|
application = Application.builder().token(self.telegram_bot_token).build()
|
||||||
@@ -88,4 +91,5 @@ class TelegramHelper:
|
|||||||
application.add_handler(CallbackQueryHandler(self.abort_processing, pattern='^abort$'))
|
application.add_handler(CallbackQueryHandler(self.abort_processing, pattern='^abort$'))
|
||||||
|
|
||||||
logging.info("Bot is running...")
|
logging.info("Bot is running...")
|
||||||
application.run_polling()
|
application.run_polling()
|
||||||
|
|
||||||
Reference in New Issue
Block a user