Skip to content

Commit

Permalink
允许传入自定义配置文件路径
Browse files Browse the repository at this point in the history
  • Loading branch information
Samiya committed Feb 21, 2024
1 parent 4b3972b commit e538a92
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 27 deletions.
24 changes: 11 additions & 13 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
## 现在做
1. 配置文件中增加到期时间
2. 数据库中增加用户列 用于区分多用户数据
3. 加入自定义配置文件路径选择
4. hoyoyo-surugaya测试
5. Alpine的DD脚本/构建Alpine的容器
## 后面再做
1. 配置文件热重载
2. 系统接入telegram bot

使用Alpine基础镜像:考虑使用基于Alpine Linux的Python镜像(如python:3.10-alpine),因为Alpine版本的镜像体积更小,运行时的资源占用也较低。这是因为Alpine使用musl libc和busybox,这些都是轻量级的替代品。


清理不必要的文件:在Dockerfile的构建过程中,确保移除任何不必要的文件,包括临时文件、构建依赖、缓存文件等。例如,使用apk add --no-cache来安装Alpine包,避免缓存文件被添加到镜像中。
### TODO List
- [ ] 配置文件中增加用户到期时间项,以自动停止过期用户任务
- [ ] 数据库中增加用户名项,以区分多用户数据
- [x] 程序运行时允许自定义传入用户配置文件路径 -r -u
- [ ] 测试hoyoyo的surugaya
- [x] 优化docker容器打包后镜像大小及内存占用问题,清理不必要的文件,避免缓存文件被添加到镜像中。
- [x] 以Alpine为底包打包一份docker镜像,镜像体积更小,运行时的资源占用也较低
- [ ] 配置文件热重载问题
- [ ] 将监控系统接入telegram bot,以允许随时添加用户配置文件 或者停止用户任务
- [ ] Alpine的一键DD脚本
- [ ] 对于煤炉推送时间不一样的现象 观察它属于哪个排序?
53 changes: 39 additions & 14 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
from dotenv import load_dotenv
from telebot.async_telebot import AsyncTeleBot
from telebot import asyncio_helper

import argparse
from loguru import logger

from monitor import setup_and_monitor
from common import AsyncHTTPXClient, AsyncAIOHTTPClient

class MonitoringController:
def __init__(self):
def __init__(self, base_path="user", direct_user_path=None):
"""
Initializes the MonitoringController class.
Initializes the MonitoringController class with the ability to accept a custom directory path.
The MonitoringController class is responsible for controlling the monitoring loop,
managing resources such as the http_client and telegram bots, and starting the monitoring process.
Args:
base_path (str, optional): Base directory path to look for user directories. Defaults to "user".
direct_user_path (str, optional): Direct path to a user directory. If provided, only this directory will be monitored.
"""
self.is_running = True # 用于通知监控任务停止
self.base_path = base_path
self.direct_user_path = direct_user_path
self.http_client = None
self.telegram_bots = {}

Expand Down Expand Up @@ -101,24 +104,29 @@ async def close_resources(self):
await bot.close_session()
logger.info(f"telegram bot: {index} has closed")

def fetch_user_directories(self, base_path):
def fetch_user_directories(self):
"""
Retrieve directories for user monitoring.
Retrieve directories for user monitoring based on the provided path.
"""
# 如果提供了直接的用户路径,则仅返回该路径
if self.direct_user_path:
return (
[self.direct_user_path] if os.path.exists(self.direct_user_path) else []
)
# 否则,返回基路径下的所有用户目录
return [
dir_entry.path for dir_entry in os.scandir(base_path) if dir_entry.is_dir()
dir_entry.path
for dir_entry in os.scandir(self.base_path)
if dir_entry.is_dir()
]

async def start_monitoring(self):
"""
Starts the monitoring process.
This method starts the VintageVigil monitoring process by initializing the necessary resources
and setting up and monitoring user directories asynchronously.
Starts the monitoring process, with modifications to accept custom directory paths.
"""
logger.info("Starting VintageVigil...")
await self.initialize_resources(parse_mode="Markdown")
user_directories = self.fetch_user_directories("user")
user_directories = self.fetch_user_directories()

monitor_tasks = [
asyncio.create_task(
Expand Down Expand Up @@ -148,7 +156,24 @@ async def run(self):


if __name__ == "__main__":
controller = MonitoringController()
parser = argparse.ArgumentParser(
description="Monitor user directories for notifications."
)
parser.add_argument(
"-r",
"--base-path",
help="Base directory path to look for user directories.",
default="user",
)
parser.add_argument(
"-u", "--user-path", help="Direct path to a specific user directory to monitor."
)

args = parser.parse_args()

controller = MonitoringController(
base_path=args.base_path, direct_user_path=args.user_path
)
try:
asyncio.run(controller.run())
except KeyboardInterrupt:
Expand Down

0 comments on commit e538a92

Please sign in to comment.