From 731b3576da429ec554567b2a6a31c242eff9921a Mon Sep 17 00:00:00 2001 From: yancharskaya Date: Tue, 17 Oct 2023 00:47:59 +0300 Subject: [PATCH 1/3] Added scheduler. To be implemented: index update functionality. --- requirements.txt | 3 ++- scheduler.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 scheduler.py diff --git a/requirements.txt b/requirements.txt index 3721664..883673d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -59,4 +59,5 @@ tzdata==2023.3 urllib3==1.26.5 wrapt==1.15.0 yarl==1.9.2 -aiogram==2.11.2 \ No newline at end of file +aiogram==2.11.2 +schedule==1.2.1 \ No newline at end of file diff --git a/scheduler.py b/scheduler.py new file mode 100644 index 0000000..fdbd4b0 --- /dev/null +++ b/scheduler.py @@ -0,0 +1,59 @@ +import os +import time +import schedule +import subprocess + +APP_FILE = "app.py" + +def stop_bot(): + """ + Stopping the bot + """ + # Остановка бота + try: + subprocess.run(["pkill", "-f", "python " + APP_FILE]) + print("Bot has been stopped.") + except Exception as e: + print(f"Error stopping the bot: {e}") + +def update_index(): + """ + Check if new indexes have been added. If they are, stop the bot, add them and restart it + """ + # Открываем файл с url-ами и запоминаем его состояние + with open(r'data/urls_of_channel_videos.txt', 'r') as file: + old_content = file.read() + + # Запускаем пайплайн. Если новые видео будут найдены, в файл добавятся их url-ы + subprocess.run(['python', r'data_pipelines/run_module.py']) + + # Открываем файл с url-ами после изменения + with open(r'data/urls_of_channel_videos.txt', 'r') as file: + new_content = file.read() + + # Если файл с url-ами изменился, останавливаем бота, добавляем новые индексы, перезапускаем бота + if old_content == new_content: + print("No new videos found.") + else: + try: + stop_bot() # Сначала останавливаем бот + + # Тут нужно обновить индексы + # os.system("git pull") # Обновляем код + + subprocess.Popen(["python", APP_FILE]) # Перезапускаем бот + print("Bot has been updated and restarted.") + except Exception as e: + print(f"Error updating and restarting the bot: {e}") + +# Планируем обновление индексов +schedule.every().monday.at("00:00").do(update_index) + +if __name__ == "__main__": + # Запускаем бот + subprocess.Popen(["python", APP_FILE]) + + # Запускаем шедулер + while True: + schedule.run_pending() + time.sleep(1) From 979be146dbc2520eff76101e8e1265887284eb49 Mon Sep 17 00:00:00 2001 From: yancharskaya Date: Tue, 24 Oct 2023 04:35:27 +0300 Subject: [PATCH 2/3] Fixed path and removed unnecessary comments --- scheduler.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/scheduler.py b/scheduler.py index fdbd4b0..a187fa9 100644 --- a/scheduler.py +++ b/scheduler.py @@ -25,7 +25,7 @@ def update_index(): old_content = file.read() # Запускаем пайплайн. Если новые видео будут найдены, в файл добавятся их url-ы - subprocess.run(['python', r'data_pipelines/run_module.py']) + subprocess.run(['python', r'data_pipelines/index_pipeline.py']) # Открываем файл с url-ами после изменения with open(r'data/urls_of_channel_videos.txt', 'r') as file: @@ -36,11 +36,7 @@ def update_index(): print("No new videos found.") else: try: - stop_bot() # Сначала останавливаем бот - - # Тут нужно обновить индексы - # os.system("git pull") # Обновляем код - + stop_bot() # Останавливаем бот subprocess.Popen(["python", APP_FILE]) # Перезапускаем бот print("Bot has been updated and restarted.") except Exception as e: From 8ecf68551d47ea5ff57b434cbe92b3fb88b08d64 Mon Sep 17 00:00:00 2001 From: yancharskaya Date: Tue, 24 Oct 2023 04:50:16 +0300 Subject: [PATCH 3/3] Moved scheduler to the appropriate folder --- scheduler/scheduler.py | 55 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 scheduler/scheduler.py diff --git a/scheduler/scheduler.py b/scheduler/scheduler.py new file mode 100644 index 0000000..8b9f08d --- /dev/null +++ b/scheduler/scheduler.py @@ -0,0 +1,55 @@ +import os +import time +import schedule +import subprocess + +APP_FILE = "app.py" + +def stop_bot(): + """ + Stopping the bot + """ + # Остановка бота + try: + subprocess.run(["pkill", "-f", "python " + APP_FILE]) + print("Bot has been stopped.") + except Exception as e: + print(f"Error stopping the bot: {e}") + +def update_index(): + """ + Check if new indexes have been added. If they are, stop the bot, add them and restart it + """ + # Открываем файл с url-ами и запоминаем его состояние + with open(r'../data/urls_of_channel_videos.txt', 'r') as file: + old_content = file.read() + + # Запускаем пайплайн. Если новые видео будут найдены, в файл добавятся их url-ы + subprocess.run(['python', r'../data_pipelines/index_pipeline.py']) + + # Открываем файл с url-ами после изменения + with open(r'../data/urls_of_channel_videos.txt', 'r') as file: + new_content = file.read() + + # Если файл с url-ами изменился, останавливаем бота, добавляем новые индексы, перезапускаем бота + if old_content == new_content: + print("No new videos found.") + else: + try: + stop_bot() # Останавливаем бот + subprocess.Popen(["python", APP_FILE]) # Перезапускаем бот + print("Bot has been updated and restarted.") + except Exception as e: + print(f"Error updating and restarting the bot: {e}") + +# Планируем обновление индексов +schedule.every().monday.at("00:00").do(update_index) + +if __name__ == "__main__": + # Запускаем бот + subprocess.Popen(["python", APP_FILE]) + + # Запускаем шедулер + while True: + schedule.run_pending() + time.sleep(1)