2024-06-14 11:09:53 +00:00
|
|
|
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";
|
2024-08-07 21:50:32 +00:00
|
|
|
import ScheduledMaintenanceService from "Common/Server/Services/ScheduledMaintenanceService";
|
|
|
|
import ScheduledMaintenanceStateService from "Common/Server/Services/ScheduledMaintenanceStateService";
|
|
|
|
import QueryHelper from "Common/Server/Types/Database/QueryHelper";
|
2024-08-05 19:00:31 +00:00
|
|
|
import ScheduledMaintenance from "Common/Models/DatabaseModels/ScheduledMaintenance";
|
|
|
|
import ScheduledMaintenanceState from "Common/Models/DatabaseModels/ScheduledMaintenanceState";
|
2024-01-11 10:49:55 +00:00
|
|
|
|
|
|
|
RunCron(
|
2024-06-14 11:09:53 +00:00
|
|
|
"ScheduledMaintenance:ChangeStateToEnded",
|
|
|
|
{ schedule: EVERY_MINUTE, runOnStartup: false },
|
|
|
|
async () => {
|
|
|
|
// get all scheduled events of all the projects.
|
|
|
|
const events: Array<ScheduledMaintenance> =
|
|
|
|
await ScheduledMaintenanceService.findBy({
|
|
|
|
query: {
|
|
|
|
currentScheduledMaintenanceState: {
|
|
|
|
isOngoingState: true,
|
|
|
|
} as any,
|
|
|
|
endsAt: QueryHelper.lessThan(OneUptimeDate.getCurrentDate()),
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
limit: LIMIT_MAX,
|
|
|
|
skip: 0,
|
|
|
|
select: {
|
|
|
|
_id: true,
|
|
|
|
projectId: true,
|
|
|
|
changeMonitorStatusToId: true,
|
|
|
|
shouldStatusPageSubscribersBeNotifiedWhenEventChangedToEnded: true,
|
|
|
|
monitors: {
|
|
|
|
_id: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
// change their state to Ongoing.
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
for (const event of events) {
|
|
|
|
const scheduledMaintenanceState: ScheduledMaintenanceState | null =
|
|
|
|
await ScheduledMaintenanceStateService.findOneBy({
|
|
|
|
query: {
|
|
|
|
projectId: event.projectId!,
|
|
|
|
isEndedState: true,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
_id: true,
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
});
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (!scheduledMaintenanceState || !scheduledMaintenanceState.id) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
await ScheduledMaintenanceService.changeScheduledMaintenanceState({
|
|
|
|
projectId: event.projectId!,
|
|
|
|
scheduledMaintenanceId: event.id!,
|
|
|
|
scheduledMaintenanceStateId: scheduledMaintenanceState.id,
|
|
|
|
shouldNotifyStatusPageSubscribers: Boolean(
|
|
|
|
event.shouldStatusPageSubscribersBeNotifiedWhenEventChangedToEnded,
|
|
|
|
),
|
|
|
|
isSubscribersNotified: false,
|
|
|
|
notifyOwners: true,
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
});
|
2024-01-11 10:49:55 +00:00
|
|
|
}
|
2024-06-14 11:09:53 +00:00
|
|
|
},
|
2024-01-11 10:49:55 +00:00
|
|
|
);
|