Skip to content

Commit e38df27

Browse files
committed
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
1 parent df01dcb commit e38df27

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

src/server/model/monitor/manager.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ export class MonitorManager {
2323
*/
2424
async upsert(data: MonitorUpsertData): Promise<MonitorWithNotification> {
2525
let monitor: MonitorWithNotification;
26-
const { id, notificationIds = [], ...others } = data;
26+
const { id, workspaceId, notificationIds = [], ...others } = data;
2727
if (id) {
2828
// update
2929
monitor = await prisma.monitor.update({
3030
where: {
3131
id,
32+
workspaceId,
3233
},
3334
data: {
3435
...others,
@@ -45,6 +46,7 @@ export class MonitorManager {
4546
monitor = await prisma.monitor.create({
4647
data: {
4748
...others,
49+
workspaceId,
4850
notifications: {
4951
connect: notificationIds.map((id) => ({ id })),
5052
},
@@ -124,4 +126,12 @@ export class MonitorManager {
124126
getRunner(monitorId: string): MonitorRunner | undefined {
125127
return this.monitorRunner[monitorId];
126128
}
129+
130+
createRunner(monitor: MonitorWithNotification) {
131+
const runner = (this.monitorRunner[monitor.id] = new MonitorRunner(
132+
monitor
133+
));
134+
135+
return runner;
136+
}
127137
}

src/server/trpc/routers/monitor.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export const monitorRouter = router({
251251
})
252252
)
253253
.output(monitorInfoSchema)
254-
.mutation(async ({ input, ctx }) => {
254+
.mutation(async ({ input }) => {
255255
const { workspaceId, monitorId, active } = input;
256256

257257
const monitor = await prisma.monitor.update({
@@ -262,22 +262,27 @@ export const monitorRouter = router({
262262
data: {
263263
active,
264264
},
265+
include: {
266+
notifications: true,
267+
},
265268
});
266-
const runner = monitorManager.getRunner(monitorId);
267-
if (runner) {
268-
if (active === true) {
269-
runner.startMonitor();
270-
runner.createEvent(
271-
'UP',
272-
`Monitor [${monitor.name}] has been manual start`
273-
);
274-
} else {
275-
runner.stopMonitor();
276-
runner.createEvent(
277-
'DOWN',
278-
`Monitor [${monitor.name}] has been manual stop`
279-
);
280-
}
269+
let runner = monitorManager.getRunner(monitorId);
270+
if (!runner) {
271+
runner = monitorManager.createRunner(monitor);
272+
}
273+
274+
if (active === true) {
275+
runner.startMonitor();
276+
runner.createEvent(
277+
'UP',
278+
`Monitor [${monitor.name}] has been manual start`
279+
);
280+
} else {
281+
runner.stopMonitor();
282+
runner.createEvent(
283+
'DOWN',
284+
`Monitor [${monitor.name}] has been manual stop`
285+
);
281286
}
282287

283288
return monitor;

0 commit comments

Comments
 (0)