Update tasks.py

This commit is contained in:
hetao 2024-01-04 14:32:38 +08:00
parent f3cb462b37
commit d5c03029ac
1 changed files with 29 additions and 6 deletions

View File

@ -1,11 +1,16 @@
#!/usr/bin/python
import sys
sys.path.append('..')
import datetime
import pymysql
from apscheduler.schedulers.blocking import BlockingScheduler
# 定时任务处理与调度
# 假设的数据库连接信息(请替换为您的实际数据库信息)
DB_HOST = 'localhost'
DB_USER = 'user'
DB_PASSWORD = 'password'
DB_NAME = 'database_name'
# 定时任务处理与调度
# 测试程序
def func():
@ -16,12 +21,28 @@ def func():
# 扫描数据库获取未完成的到期pr执行相关操作
def check_pr():
# 查询数据库获取未完成的到期pr
# 数据库连接
connection = pymysql.connect(host=DB_HOST, user=DB_USER, password=DB_PASSWORD, db=DB_NAME)
# 循环遍历调用接口查询pr状态是否新增评论或者状态为关闭
try:
with connection.cursor() as cursor:
# 查询数据库获取未完成的到期pr
sql = "SELECT id, status FROM pr_table WHERE status = 'open' AND due_date < NOW()"
cursor.execute(sql)
result = cursor.fetchall()
# 对不活跃的pr调用接口发表评论提醒处理
for pr in result:
pr_id, status = pr
# 调用接口查询pr状态是否新增评论或者状态为关闭
# 此处需要您根据实际情况编写代码例如调用GitHub API等
# 对不活跃的pr调用接口发表评论提醒处理
# 伪代码post_comment_to_pr(pr_id)
print(f"Processed PR: {pr_id}")
finally:
connection.close()
return
@ -31,8 +52,10 @@ def check_pr():
def do_timer_job(func, time):
# 创建调度器BlockingScheduler
scheduler = BlockingScheduler()
# 添加任务,时间间隔5S
# 添加任务,时间间隔
scheduler.add_job(func, 'interval', seconds=time, id='timer_job1')
# 添加检查PR的任务例如每30分钟执行一次
scheduler.add_job(check_pr, 'interval', minutes=30, id='check_pr_job')
scheduler.start()