Merge pull request #109 from bucolucas/add-commit-check-reboot

Add functionality to check for new commits and trigger reboot
This commit is contained in:
2024-08-20 14:22:41 -05:00
committed by GitHub
+20
View File
@@ -2,6 +2,8 @@ import os
import logging import logging
import sys import sys
import asyncio import asyncio
import time
import git
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes, CallbackQueryHandler from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes, CallbackQueryHandler
@@ -9,6 +11,8 @@ class TelegramHelper:
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
self.telegram_bot_token = os.getenv('TELEGRAM_BOT_TOKEN') self.telegram_bot_token = os.getenv('TELEGRAM_BOT_TOKEN')
self.repo = git.Repo(os.getcwd())
self.start_time = time.time()
async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await self.bot.start() await self.bot.start()
@@ -89,6 +93,19 @@ class TelegramHelper:
await application.bot.send_message(chat_id=chat_id, text="The application has finished initializing.") await application.bot.send_message(chat_id=chat_id, text="The application has finished initializing.")
os.remove(reboot_file_path) os.remove(reboot_file_path)
async def check_for_new_commits(self):
while True:
try:
self.repo.remotes.origin.fetch()
latest_commit = self.repo.head.commit
commit_time = latest_commit.committed_date
if commit_time > self.start_time:
logging.info("New commit detected. Triggering reboot...")
await self.reboot(None, None)
except Exception as e:
logging.error(f"Error checking for new commits: {str(e)}")
await asyncio.sleep(60) # Check every 60 seconds
def run(self): def run(self):
application = Application.builder().token(self.telegram_bot_token).build() application = Application.builder().token(self.telegram_bot_token).build()
@@ -105,4 +122,7 @@ class TelegramHelper:
# Check for .doreboot file and send message if it exists # Check for .doreboot file and send message if it exists
asyncio.get_event_loop().create_task(self.check_doreboot_file(application)) asyncio.get_event_loop().create_task(self.check_doreboot_file(application))
# Start the commit checking task
asyncio.get_event_loop().create_task(self.check_for_new_commits())
application.run_polling() application.run_polling()