Skip to content

Commit

Permalink
export markdown with html
Browse files Browse the repository at this point in the history
  • Loading branch information
liu599 committed Sep 9, 2024
1 parent 1d25389 commit de5c4aa
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 3 deletions.
5 changes: 5 additions & 0 deletions api/background/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# @Time : 2024/9/9
# @Author : liuboyuan
from framework.inori_scheduler import ScheduledTaskManager
background_manager = ScheduledTaskManager()
12 changes: 9 additions & 3 deletions api/view/trial_sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# @Description :
import os
from datetime import datetime

import markdown2
from api.view.base.view import BaseView
from api.constants.status_code import Codes
from api_entry import rest_api_description as api
Expand Down Expand Up @@ -68,6 +68,9 @@ def post(self):
print(system_prompt)
print(file_content)
print(updates['info']['repo'])
html_report = markdown2.markdown(file_content)
with open(f"static/{os.path.basename(file_path)}.html", 'w', encoding='utf-8') as file:
file.write(html_report)
return self.response_raw(
code=Codes.SUCCESS.code,
msg=Codes.SUCCESS.desc,
Expand All @@ -79,15 +82,18 @@ def post(self):
})
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
filename = f"{updates['info']['repo']}-{days}-{updates['info']['from']}-{timestamp}.md"
content = chat_message_body["content"]
with open(f"static/{filename}", 'w', encoding='utf-8') as file:
file.write(chat_message_body["content"])
file.write(content)
with open(f"static/{filename}.html", 'w', encoding='utf-8') as file:
file.write(markdown2.markdown(content))

return self.response_raw(
code=Codes.SUCCESS.code,
msg=Codes.SUCCESS.desc,
data={
"report_file_path": f"{self.base_url}/static/{os.path.basename(file_path)}",
"gpt_report_file_path": f"{self.base_url}/static/{filename}",
"gpt_report_file_path": f"{self.base_url}/static/{filename}.html",
"metadata": chat_message_body["metadata"]
}
)
3 changes: 3 additions & 0 deletions framework/inori_email/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
# @Time : 2024/9/9
# @Author : liuboyuan
7 changes: 7 additions & 0 deletions framework/inori_email/notifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
# @Time : 2024/9/9
# @Author : liuboyuan

class Notifier:
def send_email(self, report):
pass
115 changes: 115 additions & 0 deletions framework/inori_scheduler/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# -*- coding: utf-8 -*-
# @Time : 2024/7/10
# @Author : liuboyuan
# @description : https://github.com/josiahcarlson/parse-crontab
import datetime
import math
import threading
import time
from framework.inori_utils.log_utils import LOG


def cal_next_job(trigger_method):
from crontab import CronTab
ct = CronTab(trigger_method)
now = datetime.datetime.utcnow()
delay = math.ceil(ct.next(now, default_utc=True))
return delay


class Task:
def __init__(self, name, cron_expression, func, manual=False, **kwargs):
self.name = name
self.trigger_method = cron_expression
self.func = func
self.func_kwargs = kwargs
self.active = True
self.alive = False
self.manual = manual
self.timer = None

def execute(self):
"""执行任务的函数,并在完成后重新调度"""
self.alive = True
try:
self.func(**self.func_kwargs)
finally:
self.alive = False
if self.active and not self.manual:
self.reschedule()

def reschedule(self):
"""重新调度任务"""
if self.timer:
self.timer.cancel()
next_run_time = cal_next_job(self.trigger_method)
# print(next_run_time)
if next_run_time >= 0:
self.timer = threading.Timer(next_run_time, self.execute)
self.timer.start()


class ScheduledTaskManager:
def __init__(self):
self.tasks = []
self.running = False
self.thread = None

def add_task(self, name, cron_expression, func, manual=False, **kwargs):
"""向任务管理器中添加任务"""
task = Task(name, cron_expression, func, manual, **kwargs)
self.tasks.append(task)

def _run(self):
"""内部使用,用于运行所有已添加的任务"""
self.running = True
while self.running:
for task in self.tasks:
if task.active and not task.alive and not task.timer:
if not task.manual:
self.schedule_task(task)
time.sleep(1)

def schedule_task(self, task):
"""首次调度一个任务"""
next_run_time = cal_next_job(task.trigger_method)
if next_run_time >= 0:
task.timer = threading.Timer(next_run_time, task.execute)
task.timer.start()

def start(self):
"""开始执行所有已添加的任务"""
if not self.running:
self.thread = threading.Thread(target=self._run)
self.thread.start()

def stop(self):
"""停止所有正在执行的任务和线程"""
if self.running:
self.running = False
if self.thread:
self.thread.join()
for task in self.tasks:
if task.timer:
task.timer.cancel()
task.timer = None
task.alive = False


# 示例使用
def print_task_name(task_name):
LOG.debug(f"Executing task: {task_name}")


if __name__ == "__main__":
task_manager = ScheduledTaskManager()
task_manager.add_task("Task1", "*/5 * * * * * *", print_task_name)
# task_manager.add_task("Task2", "*/5 * * * * * *", print_task_name, manual=True)
task_manager.start()
time.sleep(10)
task_manager.stop()
print('=== restart ===')
task_manager.add_task("Task2", "*/8 * * * * * *", print_task_name)
task_manager.start()
time.sleep(24)
task_manager.stop()

0 comments on commit de5c4aa

Please sign in to comment.