Skip to content

Commit 9e0b241

Browse files
authored
Merge pull request #13 from DjangoPeng/v0.5
feat(v0.5): add email notifications, schedule and daemon service management.
2 parents 7f76b6e + a638217 commit 9e0b241

18 files changed

+873
-186
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,5 @@ cython_debug/
164164
# User-defined
165165
daily_progress/*
166166
src/jupyter/*.md
167-
src/jupyter/daily_progress/*
167+
src/jupyter/daily_progress/*
168+
run/*

README-EN.md

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,52 +24,90 @@ pip install -r requirements.txt
2424

2525
### 2. Configure the Application
2626

27-
Edit the `config.json` file to set up your GitHub token, notification settings, subscription file, and update interval:
27+
Edit the `config.json` file to set up your GitHub token, Email settings(e.g.Tencent Exmail), subscription file, and update settings:
28+
2829

2930
```json
3031
{
3132
"github_token": "your_github_token",
32-
"notification_settings": {
33-
"email": "[email protected]",
34-
"slack_webhook_url": "your_slack_webhook_url"
33+
"email": {
34+
"smtp_server": "smtp.exmail.qq.com",
35+
"smtp_port": 465,
36+
"from": "[email protected]",
37+
"password": "your_email_password",
38+
3539
},
40+
"slack_webhook_url": "your_slack_webhook_url",
3641
"subscriptions_file": "subscriptions.json",
37-
"update_interval": 86400
42+
"github_progress_frequency_days": 1,
43+
"github_progress_execution_time":"08:00"
3844
}
45+
46+
```
47+
**For security reasons:** It is recommended to configure the GitHub Token and Email Password using environment variables to avoid storing sensitive information in plain text, as shown below:
48+
49+
```shell
50+
# GitHub
51+
export GITHUB_TOKEN="github_pat_xxx"
52+
# Email
53+
export EMAIL_PASSWORD="password"
3954
```
4055

4156
### 3. How to Run
4257

43-
GitHub Sentinel supports three different ways to run the application:
58+
GitHub Sentinel supports the following three modes of operation:
4459

4560
#### A. Run as a Command-Line Tool
4661

47-
You can run the application interactively from the command line:
62+
You can interactively run the application from the command line:
4863

4964
```sh
5065
python src/command_tool.py
5166
```
5267

53-
In this mode, you can manually input commands to manage subscriptions, retrieve updates, and generate reports.
68+
In this mode, you can manually enter commands to manage subscriptions, retrieve updates, and generate reports.
5469

55-
#### B. Run as a Daemon Process with Scheduler
70+
#### B. Run as a Background Service
5671

57-
To run the application as a background service (daemon) that regularly checks for updates:
72+
To run the application as a background service (daemon), it will automatically update according to the configured schedule.
5873

59-
1. Ensure you have the `python-daemon` package installed:
74+
You can use the daemon management script [daemon_control.sh](daemon_control.sh) to start, check the status, stop, and restart:
75+
76+
1. Start the service:
6077

6178
```sh
62-
pip install python-daemon
79+
$ ./daemon_control.sh start
80+
Starting DaemonProcess...
81+
DaemonProcess started.
6382
```
6483

65-
2. Launch the daemon process:
84+
- This will launch [./src/daemon_process.py], generating reports periodically as set in `config.json`, and sending emails.
85+
- Service logs will be saved to `logs/DaemonProcess.log`, with historical logs also appended to `logs/app.log`.
86+
87+
2. Check the service status:
6688

6789
```sh
68-
nohup python3 src/daemon_process.py > logs/daemon_process.log 2>&1 &
90+
$ ./daemon_control.sh status
91+
DaemonProcess is running.
6992
```
7093

71-
- This will start the scheduler in the background, checking for updates at the interval specified in your `config.json`.
72-
- Logs will be saved to the `logs/daemon_process.log` file.
94+
3. Stop the service:
95+
96+
```sh
97+
$ ./daemon_control.sh stop
98+
Stopping DaemonProcess...
99+
DaemonProcess stopped.
100+
```
101+
102+
4. Restart the service:
103+
104+
```sh
105+
$ ./daemon_control.sh restart
106+
Stopping DaemonProcess...
107+
DaemonProcess stopped.
108+
Starting DaemonProcess...
109+
DaemonProcess started.
110+
```
73111

74112
#### C. Run as a Gradio Server
75113

README.md

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,32 @@ pip install -r requirements.txt
2424

2525
### 2. 配置应用
2626

27-
编辑 `config.json` 文件,以设置您的 GitHub 令牌、通知设置、订阅文件和更新间隔
27+
编辑 `config.json` 文件,以设置您的 GitHub Token、Email 设置(以腾讯企微邮箱为例)、订阅文件和更新设置
2828

2929
```json
3030
{
3131
"github_token": "your_github_token",
32-
"notification_settings": {
33-
"email": "[email protected]",
34-
"slack_webhook_url": "your_slack_webhook_url"
32+
"email": {
33+
"smtp_server": "smtp.exmail.qq.com",
34+
"smtp_port": 465,
35+
"from": "[email protected]",
36+
"password": "your_email_password",
37+
3538
},
39+
"slack_webhook_url": "your_slack_webhook_url",
3640
"subscriptions_file": "subscriptions.json",
37-
"update_interval": 86400
41+
"github_progress_frequency_days": 1,
42+
"github_progress_execution_time":"08:00"
3843
}
3944
```
45+
**出于安全考虑:** GitHub Token 和 Email Password 的设置均支持使用环境变量进行配置,以避免明文配置重要信息,如下所示:
46+
47+
```shell
48+
# Github
49+
export GITHUB_TOKEN="github_pat_xxx"
50+
# Email
51+
export EMAIL_PASSWORD="password"
52+
```
4053

4154
### 3. 如何运行
4255

@@ -52,25 +65,48 @@ python src/command_tool.py
5265

5366
在此模式下,您可以手动输入命令来管理订阅、检索更新和生成报告。
5467

55-
#### B. 作为后台进程运行(带调度器)
68+
#### B. 作为后台服务运行
69+
70+
要将该应用作为后台服务(守护进程)运行,它将根据相关配置定期自动更新。
71+
72+
您可以直接使用守护进程管理脚本 [daemon_control.sh](daemon_control.sh) 来启动、查询状态、关闭和重启:
73+
74+
1. 启动服务:
5675

57-
要将该应用作为后台服务(守护进程)运行,它将定期检查更新:
76+
```sh
77+
$ ./daemon_control.sh start
78+
Starting DaemonProcess...
79+
DaemonProcess started.
80+
```
81+
82+
- 这将启动[./src/daemon_process.py],按照 `config.json` 中设置的更新频率和时间点定期生成报告,并发送邮件。
83+
- 本次服务日志将保存到 `logs/DaemonProcess.log` 文件中。同时,历史累计日志也将同步追加到 `logs/app.log` 日志文件中。
5884

59-
1. 确保您已安装 `python-daemon`
85+
2. 查询服务状态
6086

6187
```sh
62-
pip install python-daemon
88+
$ ./daemon_control.sh status
89+
DaemonProcess is running.
6390
```
6491

65-
2. 启动后台进程
92+
3. 关闭服务
6693

6794
```sh
68-
nohup python3 src/daemon_process.py > logs/daemon_process.log 2>&1 &
95+
$ ./daemon_control.sh stop
96+
Stopping DaemonProcess...
97+
DaemonProcess stopped.
6998
```
7099

71-
- 这将启动后台调度器,按照 `config.json` 中指定的间隔定期检查更新。
72-
- 日志将保存到 `logs/daemon_process.log` 文件中。
100+
4. 重启服务:
73101

102+
```sh
103+
$ ./daemon_control.sh restart
104+
Stopping DaemonProcess...
105+
DaemonProcess stopped.
106+
Starting DaemonProcess...
107+
DaemonProcess started.
108+
```
109+
74110
#### C. 作为 Gradio 服务器运行
75111

76112
要使用 Gradio 界面运行应用,允许用户通过 Web 界面与该工具交互:

config.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
{
22
"github_token": "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",
3-
"notification_settings": {
4-
"email": "[email protected]",
5-
"slack_webhook_url": "your_slack_webhook_url"
3+
"email": {
4+
"smtp_server": "smtp.exmail.qq.com",
5+
"smtp_port": 465,
6+
"from": "[email protected]",
7+
"password": "",
8+
69
},
10+
"slack_webhook_url": "your_slack_webhook_url",
711
"subscriptions_file": "subscriptions.json",
8-
"update_interval": 86400
12+
"github_progress_frequency_days": 1,
13+
"github_progress_execution_time":"08:00"
914
}

daemon_control.sh

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/bash
2+
# 守护进程控制脚本
3+
4+
# 定义守护进程 Python 脚本的路径
5+
DAEMON_PATH="./src/daemon_process.py"
6+
# 定义守护进程的名称
7+
DAEMON_NAME="DaemonProcess"
8+
# 定义日志文件的路径
9+
LOG_FILE="./logs/$DAEMON_NAME.log"
10+
# 定义守护进程的 PID 文件路径,用于存储进程号
11+
PID_FILE="./run/$DAEMON_NAME.pid"
12+
13+
# 启动守护进程的函数
14+
start() {
15+
echo "Starting $DAEMON_NAME..."
16+
# 使用 nohup 命令在后台运行 Python 脚本,并将输出重定向到日志文件
17+
nohup python3 $DAEMON_PATH > $LOG_FILE 2>&1 &
18+
# 将守护进程的 PID 写入文件
19+
echo $! > $PID_FILE
20+
echo "$DAEMON_NAME started."
21+
}
22+
23+
# 停止守护进程的函数
24+
stop() {
25+
if [ -f $PID_FILE ]; then
26+
# 如果 PID 文件存在,读取 PID
27+
PID=$(cat $PID_FILE)
28+
echo "Stopping $DAEMON_NAME..."
29+
# 使用 kill 命令停止进程
30+
kill $PID
31+
echo "$DAEMON_NAME stopped."
32+
# 删除 PID 文件
33+
rm $PID_FILE
34+
else
35+
echo "$DAEMON_NAME is not running."
36+
fi
37+
}
38+
39+
# 检查守护进程状态的函数
40+
status() {
41+
if [ -f $PID_FILE ]; then
42+
PID=$(cat $PID_FILE)
43+
# 检查进程是否在运行
44+
if ps -p $PID > /dev/null
45+
then
46+
echo "$DAEMON_NAME is running."
47+
else
48+
echo "$DAEMON_NAME is not running."
49+
fi
50+
else
51+
echo "$DAEMON_NAME is not running."
52+
fi
53+
}
54+
55+
# 根据输入参数选择执行哪个函数
56+
case "$1" in
57+
start)
58+
start
59+
;;
60+
stop)
61+
stop
62+
;;
63+
status)
64+
status
65+
;;
66+
restart)
67+
# 重启守护进程
68+
stop
69+
start
70+
;;
71+
*)
72+
# 如果参数不符合预期,显示用法
73+
echo "Usage: $0 {start|stop|status|restart}"
74+
exit 1
75+
esac

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ requests
22
openai
33
gradio
44
loguru
5-
python-daemon
5+
schedule
6+
markdown2

src/command_tool.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from config import Config # 从config模块导入Config类,用于配置管理
44
from github_client import GitHubClient # 从github_client模块导入GitHubClient类,用于GitHub API操作
5-
from notifier import Notifier # 从notifier模块导入Notifier类,用于通知功能
65
from report_generator import ReportGenerator # 从report_generator模块导入ReportGenerator类,用于报告生成
76
from llm import LLM # 从llm模块导入LLM类,可能用于语言模型相关操作
87
from subscription_manager import SubscriptionManager # 从subscription_manager模块导入SubscriptionManager类,管理订阅
@@ -12,7 +11,6 @@
1211
def main():
1312
config = Config() # 创建配置实例
1413
github_client = GitHubClient(config.github_token) # 创建GitHub客户端实例
15-
notifier = Notifier(config.notification_settings) # 创建通知器实例
1614
llm = LLM() # 创建语言模型实例
1715
report_generator = ReportGenerator(llm) # 创建报告生成器实例
1816
subscription_manager = SubscriptionManager(config.subscriptions_file) # 创建订阅管理器实例

src/config.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@ def __init__(self):
66
self.load_config()
77

88
def load_config(self):
9-
# 从环境变量获取GitHub Token
10-
self.github_token = os.getenv('GITHUB_TOKEN')
11-
9+
# 尝试从环境变量获取配置或使用 config.json 文件中的配置作为回退
1210
with open('config.json', 'r') as f:
1311
config = json.load(f)
1412

15-
# 如果环境变量中没有GitHub Token,则从配置文件中读取
16-
if not self.github_token:
17-
self.github_token = config.get('github_token')
18-
19-
self.notification_settings = config.get('notification_settings')
13+
# 使用环境变量或配置文件的 GitHub Token
14+
self.github_token = os.getenv('GITHUB_TOKEN', config.get('github_token'))
15+
16+
# 初始化电子邮件设置
17+
self.email = config.get('email', {})
18+
# 使用环境变量或配置文件中的电子邮件密码
19+
self.email['password'] = os.getenv('EMAIL_PASSWORD', self.email.get('password', ''))
20+
2021
self.subscriptions_file = config.get('subscriptions_file')
21-
self.update_interval = config.get('update_interval', 24 * 60 * 60) # 默认24小时
22+
# 默认每天执行
23+
self.freq_days = config.get('github_progress_frequency_days', 1)
24+
# 默认早上8点更新 (操作系统默认时区是 UTC +0,08点刚好对应北京时间凌晨12点)
25+
self.exec_time = config.get('github_progress_execution_time', "08:00")

0 commit comments

Comments
 (0)