Add ScheduleRecurringEvents job to schedule recurring maintenance events

This commit is contained in:
Simon Larsen 2024-09-09 19:38:19 +01:00
parent 21c9facc02
commit 6736e6c74e
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA
2 changed files with 184 additions and 0 deletions

View File

@ -31,6 +31,7 @@ import "./Jobs/ScheduledMaintenance/ChangeStateToEnded";
// Scheduled Event
import "./Jobs/ScheduledMaintenance/ChangeStateToOngoing";
import "./Jobs/ScheduledMaintenance/SendNotificationToSubscribers";
import "./Jobs/ScheduledMaintenance/ScheduleRecurringEvents";
// Scheduled Event Owners
import "./Jobs/ScheduledMaintenanceOwners/SendCreatedResourceNotification";
import "./Jobs/ScheduledMaintenanceOwners/SendNotePostedNotification";

View File

@ -0,0 +1,183 @@
import RunCron from "../../Utils/Cron";
import LIMIT_MAX from "Common/Types/Database/LimitMax";
import OneUptimeDate from "Common/Types/Date";
import { EVERY_MINUTE } from "Common/Utils/CronTime";
import ScheduledMaintenanceService from "Common/Server/Services/ScheduledMaintenanceService";
import QueryHelper from "Common/Server/Types/Database/QueryHelper";
import ScheduledMaintenanceTemplate from "Common/Models/DatabaseModels/ScheduledMaintenanceTemplate";
import ScheduledMaintenanceTemplateService from "Common/Server/Services/ScheduledMaintenanceTemplateService";
import ScheduledMaintenanceTemplateOwnerUserService from "Common/Server/Services/ScheduledMaintenanceTemplateOwnerUserService";
import ScheduledMaintenanceOwnerUser from "Common/Models/DatabaseModels/ScheduledMaintenanceOwnerUser";
import ScheduledMaintenanceTemplateOwnerUser from "Common/Models/DatabaseModels/ScheduledMaintenanceTemplateOwnerUser";
import ScheduledMaintenanceOwnerTeamService from "Common/Server/Services/ScheduledMaintenanceOwnerTeamService";
import ScheduledMaintenanceTemplateOwnerTeamService from "Common/Server/Services/ScheduledMaintenanceTemplateOwnerTeamService";
import ScheduledMaintenance from "Common/Models/DatabaseModels/ScheduledMaintenance";
import ScheduledMaintenanceOwnerUserService from "Common/Server/Services/ScheduledMaintenanceOwnerUserService";
import ScheduledMaintenanceOwnerTeam from "Common/Models/DatabaseModels/ScheduledMaintenanceOwnerTeam";
RunCron(
"ScheduledMaintenance:ScheduleRecurringEvents",
{ schedule: EVERY_MINUTE, runOnStartup: false },
async () => {
// get all scheduled events of all the projects.
const recurringTemplates: Array<ScheduledMaintenanceTemplate> =
await ScheduledMaintenanceTemplateService.findBy({
query: {
isRecurringEvent: true,
scheduleNextEventAt: QueryHelper.lessThanEqualTo(
OneUptimeDate.getCurrentDate(),
),
},
props: {
isRoot: true,
},
limit: LIMIT_MAX,
skip: 0,
select: {
_id: true,
projectId: true,
changeMonitorStatusToId: true,
shouldStatusPageSubscribersBeNotifiedWhenEventChangedToEnded: true,
shouldStatusPageSubscribersBeNotifiedOnEventCreated: true,
shouldStatusPageSubscribersBeNotifiedWhenEventChangedToOngoing: true,
monitors: true,
statusPages: true,
scheduleNextEventAt: true,
firstEventStartsAt: true,
firstEventEndsAt: true,
firstEventScheduledAt: true,
title: true,
description: true,
labels: true,
isRecurringEvent: true,
recurringInterval: true,
},
});
// change their state to Ongoing.
for (const recurringTemplate of recurringTemplates) {
// get owner users for this template.
const ownerUsers: Array<ScheduledMaintenanceTemplateOwnerUser> =
await ScheduledMaintenanceTemplateOwnerUserService.findBy({
query: {
scheduledMaintenanceTemplateId: recurringTemplate.id!,
},
props: {
isRoot: true,
},
limit: LIMIT_MAX,
skip: 0,
select: {
userId: true,
},
});
// owner teams.
const ownerTeams: Array<ScheduledMaintenanceOwnerTeam> =
await ScheduledMaintenanceTemplateOwnerTeamService.findBy({
query: {
scheduledMaintenanceTemplateId: recurringTemplate.id!,
},
props: {
isRoot: true,
},
limit: LIMIT_MAX,
skip: 0,
select: {
teamId: true,
},
});
// now create a new scheduled maintenance event for this template.
let scheduledMaintenanceEvent = new ScheduledMaintenance();
scheduledMaintenanceEvent.projectId = recurringTemplate.projectId!;
scheduledMaintenanceEvent.changeMonitorStatusToId =
recurringTemplate.changeMonitorStatusToId!;
scheduledMaintenanceEvent.shouldStatusPageSubscribersBeNotifiedWhenEventChangedToEnded =
recurringTemplate.shouldStatusPageSubscribersBeNotifiedWhenEventChangedToEnded!;
scheduledMaintenanceEvent.shouldStatusPageSubscribersBeNotifiedOnEventCreated =
recurringTemplate.shouldStatusPageSubscribersBeNotifiedOnEventCreated!;
scheduledMaintenanceEvent.shouldStatusPageSubscribersBeNotifiedWhenEventChangedToOngoing =
recurringTemplate.shouldStatusPageSubscribersBeNotifiedWhenEventChangedToOngoing!;
scheduledMaintenanceEvent.monitors = recurringTemplate.monitors!;
scheduledMaintenanceEvent.statusPages = recurringTemplate.statusPages!;
scheduledMaintenanceEvent.title = recurringTemplate.title!;
scheduledMaintenanceEvent.description = recurringTemplate.description!;
scheduledMaintenanceEvent.labels = recurringTemplate.labels!;
const eventscheduledTime = recurringTemplate.scheduleNextEventAt!;
const firstScheduledTime = recurringTemplate.firstEventScheduledAt!;
const firstStartTime = recurringTemplate.firstEventStartsAt!;
const firstEndTime = recurringTemplate.firstEventEndsAt!;
const minutesBetwenScheduledAndStartTime =
OneUptimeDate.getMinutesBetweenTwoDates(
eventscheduledTime,
firstStartTime,
);
const minutesBetweenScheduledAndEndTime =
OneUptimeDate.getMinutesBetweenTwoDates(
eventscheduledTime,
firstEndTime,
);
// set the scheduled time for this event.
scheduledMaintenanceEvent.createdAt = eventscheduledTime!;
scheduledMaintenanceEvent.startsAt = OneUptimeDate.addRemoveMinutes(
firstScheduledTime,
minutesBetwenScheduledAndStartTime,
);
scheduledMaintenanceEvent.endsAt = OneUptimeDate.addRemoveMinutes(
firstScheduledTime,
minutesBetweenScheduledAndEndTime,
);
// now create this event.
scheduledMaintenanceEvent = await ScheduledMaintenanceService.create({
data: scheduledMaintenanceEvent,
props: {
isRoot: true,
},
});
// now add owners and teams to this event.
for (const ownerUser of ownerUsers) {
const scheduledMaintenanceOwnerUser =
new ScheduledMaintenanceOwnerUser();
scheduledMaintenanceOwnerUser.scheduledMaintenanceId =
scheduledMaintenanceEvent.id!;
scheduledMaintenanceOwnerUser.projectId =
scheduledMaintenanceEvent.projectId!;
scheduledMaintenanceOwnerUser.userId = ownerUser.userId!;
await ScheduledMaintenanceOwnerUserService.create({
data: scheduledMaintenanceOwnerUser,
props: {
isRoot: true,
},
});
}
// now do the same for owner teams.
for (const ownerTeam of ownerTeams) {
const scheduledMaintenanceOwnerTeam =
new ScheduledMaintenanceOwnerTeam();
scheduledMaintenanceOwnerTeam.scheduledMaintenanceId =
scheduledMaintenanceEvent.id!;
scheduledMaintenanceOwnerTeam.projectId =
scheduledMaintenanceEvent.projectId!;
scheduledMaintenanceOwnerTeam.teamId = ownerTeam.teamId!;
await ScheduledMaintenanceOwnerTeamService.create({
data: scheduledMaintenanceOwnerTeam,
props: {
isRoot: true,
},
});
}
}
},
);