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
This commit is contained in:
moonrailgun 2024-01-03 01:52:06 +08:00
parent df01dcb857
commit e38df2706e
2 changed files with 32 additions and 17 deletions

View File

@ -23,12 +23,13 @@ export class MonitorManager {
*/ */
async upsert(data: MonitorUpsertData): Promise<MonitorWithNotification> { async upsert(data: MonitorUpsertData): Promise<MonitorWithNotification> {
let monitor: MonitorWithNotification; let monitor: MonitorWithNotification;
const { id, notificationIds = [], ...others } = data; const { id, workspaceId, notificationIds = [], ...others } = data;
if (id) { if (id) {
// update // update
monitor = await prisma.monitor.update({ monitor = await prisma.monitor.update({
where: { where: {
id, id,
workspaceId,
}, },
data: { data: {
...others, ...others,
@ -45,6 +46,7 @@ export class MonitorManager {
monitor = await prisma.monitor.create({ monitor = await prisma.monitor.create({
data: { data: {
...others, ...others,
workspaceId,
notifications: { notifications: {
connect: notificationIds.map((id) => ({ id })), connect: notificationIds.map((id) => ({ id })),
}, },
@ -124,4 +126,12 @@ export class MonitorManager {
getRunner(monitorId: string): MonitorRunner | undefined { getRunner(monitorId: string): MonitorRunner | undefined {
return this.monitorRunner[monitorId]; return this.monitorRunner[monitorId];
} }
createRunner(monitor: MonitorWithNotification) {
const runner = (this.monitorRunner[monitor.id] = new MonitorRunner(
monitor
));
return runner;
}
} }

View File

@ -251,7 +251,7 @@ export const monitorRouter = router({
}) })
) )
.output(monitorInfoSchema) .output(monitorInfoSchema)
.mutation(async ({ input, ctx }) => { .mutation(async ({ input }) => {
const { workspaceId, monitorId, active } = input; const { workspaceId, monitorId, active } = input;
const monitor = await prisma.monitor.update({ const monitor = await prisma.monitor.update({
@ -262,22 +262,27 @@ export const monitorRouter = router({
data: { data: {
active, active,
}, },
include: {
notifications: true,
},
}); });
const runner = monitorManager.getRunner(monitorId); let runner = monitorManager.getRunner(monitorId);
if (runner) { if (!runner) {
if (active === true) { runner = monitorManager.createRunner(monitor);
runner.startMonitor(); }
runner.createEvent(
'UP', if (active === true) {
`Monitor [${monitor.name}] has been manual start` runner.startMonitor();
); runner.createEvent(
} else { 'UP',
runner.stopMonitor(); `Monitor [${monitor.name}] has been manual start`
runner.createEvent( );
'DOWN', } else {
`Monitor [${monitor.name}] has been manual stop` runner.stopMonitor();
); runner.createEvent(
} 'DOWN',
`Monitor [${monitor.name}] has been manual stop`
);
} }
return monitor; return monitor;