From e38df2706ebfc59aa0a36294d7a35e4bf9ae1492 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Wed, 3 Jan 2024 01:52:06 +0800 Subject: [PATCH] fix: fix a bug which can not raise monitor with changeActive if monitor never run before the case of init state is stop. (active: false) and add workspace id validate in update logic --- src/server/model/monitor/manager.ts | 12 +++++++++- src/server/trpc/routers/monitor.ts | 37 ++++++++++++++++------------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/server/model/monitor/manager.ts b/src/server/model/monitor/manager.ts index 3f0a1ba..2a077c0 100644 --- a/src/server/model/monitor/manager.ts +++ b/src/server/model/monitor/manager.ts @@ -23,12 +23,13 @@ export class MonitorManager { */ async upsert(data: MonitorUpsertData): Promise { let monitor: MonitorWithNotification; - const { id, notificationIds = [], ...others } = data; + const { id, workspaceId, notificationIds = [], ...others } = data; if (id) { // update monitor = await prisma.monitor.update({ where: { id, + workspaceId, }, data: { ...others, @@ -45,6 +46,7 @@ export class MonitorManager { monitor = await prisma.monitor.create({ data: { ...others, + workspaceId, notifications: { connect: notificationIds.map((id) => ({ id })), }, @@ -124,4 +126,12 @@ export class MonitorManager { getRunner(monitorId: string): MonitorRunner | undefined { return this.monitorRunner[monitorId]; } + + createRunner(monitor: MonitorWithNotification) { + const runner = (this.monitorRunner[monitor.id] = new MonitorRunner( + monitor + )); + + return runner; + } } diff --git a/src/server/trpc/routers/monitor.ts b/src/server/trpc/routers/monitor.ts index 3ce3aa2..be70cee 100644 --- a/src/server/trpc/routers/monitor.ts +++ b/src/server/trpc/routers/monitor.ts @@ -251,7 +251,7 @@ export const monitorRouter = router({ }) ) .output(monitorInfoSchema) - .mutation(async ({ input, ctx }) => { + .mutation(async ({ input }) => { const { workspaceId, monitorId, active } = input; const monitor = await prisma.monitor.update({ @@ -262,22 +262,27 @@ export const monitorRouter = router({ data: { active, }, + include: { + notifications: true, + }, }); - const runner = monitorManager.getRunner(monitorId); - if (runner) { - if (active === true) { - runner.startMonitor(); - runner.createEvent( - 'UP', - `Monitor [${monitor.name}] has been manual start` - ); - } else { - runner.stopMonitor(); - runner.createEvent( - 'DOWN', - `Monitor [${monitor.name}] has been manual stop` - ); - } + let runner = monitorManager.getRunner(monitorId); + if (!runner) { + runner = monitorManager.createRunner(monitor); + } + + if (active === true) { + runner.startMonitor(); + runner.createEvent( + 'UP', + `Monitor [${monitor.name}] has been manual start` + ); + } else { + runner.stopMonitor(); + runner.createEvent( + 'DOWN', + `Monitor [${monitor.name}] has been manual stop` + ); } return monitor;