fix: scheduled clean job
This commit is contained in:
1 parent
0207782a6a
commit
c1d00a7855
4 files changed
+22
-28
No files matched your search
@@ -7,12 +7,11 @@ import {
|
||||
} from "../services";
|
||||
import { shouldSendNotification, sendNotification } from "../services";
|
||||
import { Hono } from "hono";
|
||||
import { Bindings } from "../models/db";
|
||||
import { db } from "../config";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { notificationSettings } from "../db/schema";
|
||||
import { eq,lt } from "drizzle-orm";
|
||||
import { notificationSettings,agentMetrics24h } from "../db/schema";
|
||||
|
||||
const agentTask = new Hono<{ Bindings: Bindings }>();
|
||||
const agentTask = new Hono<{}>();
|
||||
|
||||
interface AgentResult {
|
||||
id: number;
|
||||
@@ -299,9 +298,7 @@ export default {
|
||||
|
||||
if (hour % 6 === 0 && minute === 5) {
|
||||
console.log("定时任务: 正在清理 metrics 24h 表数据...");
|
||||
await env.DB.prepare("DELETE FROM agent_metrics_24h WHERE timestamp < ?")
|
||||
.bind(yesterday)
|
||||
.run();
|
||||
await db.delete(agentMetrics24h).where(lt(agentMetrics24h.timestamp, yesterday));
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
+15
-18
@@ -1,7 +1,10 @@
|
||||
// 导出所有定时任务
|
||||
import monitorTask from "./monitor-task";
|
||||
import agentTask from "./agent-task";
|
||||
import { Bindings } from "../models/db";
|
||||
import { db } from "../config";
|
||||
import * as schema from "../db/schema";
|
||||
import { lt } from "drizzle-orm";
|
||||
|
||||
// 统一的定时任务处理函数
|
||||
export const runScheduledTasks = async (event: any, env: any, ctx: any) => {
|
||||
try {
|
||||
@@ -18,7 +21,7 @@ export const runScheduledTasks = async (event: any, env: any, ctx: any) => {
|
||||
const minute = now.getMinutes();
|
||||
if (dayOfMonth === 1 && hour === 0 && minute === 30) {
|
||||
console.log("每月1号零点30分执行清理任务...");
|
||||
await cleanupOldRecords(env.DB);
|
||||
await cleanupOldRecords();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("定时任务执行出错:", error);
|
||||
@@ -26,27 +29,21 @@ export const runScheduledTasks = async (event: any, env: any, ctx: any) => {
|
||||
};
|
||||
|
||||
// 清理30天以前的历史记录
|
||||
export async function cleanupOldRecords(db: Bindings["DB"]) {
|
||||
export async function cleanupOldRecords() {
|
||||
console.log("开始清理30天以前的历史记录...");
|
||||
|
||||
// 清理30天以前的 monitor_daily_stats
|
||||
await db.prepare(
|
||||
`
|
||||
DELETE FROM monitor_daily_stats
|
||||
WHERE date < datetime('now', '-30 days')
|
||||
`
|
||||
)
|
||||
.run();
|
||||
const now = new Date();
|
||||
now.setDate(now.getDate() - 30);
|
||||
const dateStr = now.toISOString().split("T")[0];
|
||||
await db.delete(schema.monitorDailyStats).where(
|
||||
lt(schema.monitorDailyStats.date, dateStr)
|
||||
)
|
||||
|
||||
// 清理通知历史记录
|
||||
await db
|
||||
.prepare(
|
||||
`
|
||||
DELETE FROM notification_history
|
||||
WHERE sent_at < datetime('now', '-30 days')
|
||||
`
|
||||
)
|
||||
.run();
|
||||
await db.delete(schema.notificationHistory).where(
|
||||
lt(schema.notificationHistory.sent_at, dateStr)
|
||||
)
|
||||
|
||||
return {
|
||||
success: true,
|
||||
|
||||
@@ -29,7 +29,7 @@ async function checkMonitors(c: any) {
|
||||
console.log(`开始检查监控: ${monitor.name} (ID: ${monitor.id})`);
|
||||
const checkResult = await checkMonitor(monitor);
|
||||
// 处理通知
|
||||
console.log(`检查完成,状态: ${checkResult}`);
|
||||
console.log(`检查完成,状态: ${checkResult.status}`);
|
||||
await handleMonitorNotification(c, monitor, checkResult);
|
||||
return checkResult;
|
||||
})
|
||||
|
||||
@@ -88,7 +88,7 @@ export async function checkMonitor(monitor: models.Monitor) {
|
||||
const status = isExpectedStatus ? "up" : "down";
|
||||
|
||||
// 记录状态历史
|
||||
const res = await repositories.insertMonitorStatusHistory(
|
||||
await repositories.insertMonitorStatusHistory(
|
||||
monitor.id,
|
||||
status,
|
||||
responseTime,
|
||||
@@ -96,7 +96,7 @@ export async function checkMonitor(monitor: models.Monitor) {
|
||||
error
|
||||
);
|
||||
|
||||
console.log(`监控 ${monitor.name} (${monitor.url}) 检查完成. ${res}`);
|
||||
console.log(`监控 ${monitor.name} (${monitor.url}) 检查完成.`);
|
||||
|
||||
// 更新监控状态
|
||||
await repositories.updateMonitorStatus(monitor.id, status, responseTime);
|
||||
|
||||
Reference in new issue
Block a user