2024-05-29 14:25:09 +00:00
|
|
|
import RunCron from '../../Utils/Cron';
|
|
|
|
import LIMIT_MAX from 'Common/Types/Database/LimitMax';
|
|
|
|
import OneUptimeDate from 'Common/Types/Date';
|
2024-01-11 10:49:55 +00:00
|
|
|
import { EVERY_MINUTE } from 'Common/Utils/CronTime';
|
|
|
|
import ScheduledMaintenanceService from 'CommonServer/Services/ScheduledMaintenanceService';
|
2024-05-29 14:25:09 +00:00
|
|
|
import ScheduledMaintenanceStateService from 'CommonServer/Services/ScheduledMaintenanceStateService';
|
2024-01-11 10:49:55 +00:00
|
|
|
import QueryHelper from 'CommonServer/Types/Database/QueryHelper';
|
|
|
|
import ScheduledMaintenance from 'Model/Models/ScheduledMaintenance';
|
|
|
|
import ScheduledMaintenanceState from 'Model/Models/ScheduledMaintenanceState';
|
|
|
|
|
|
|
|
RunCron(
|
|
|
|
'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,
|
2024-01-30 10:30:03 +00:00
|
|
|
shouldStatusPageSubscribersBeNotifiedWhenEventChangedToEnded:
|
|
|
|
true,
|
2024-01-11 10:49:55 +00:00
|
|
|
monitors: {
|
|
|
|
_id: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// change their state to Ongoing.
|
|
|
|
|
|
|
|
for (const event of events) {
|
|
|
|
const scheduledMaintenanceState: ScheduledMaintenanceState | null =
|
|
|
|
await ScheduledMaintenanceStateService.findOneBy({
|
|
|
|
query: {
|
|
|
|
projectId: event.projectId!,
|
|
|
|
isEndedState: true,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
_id: true,
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!scheduledMaintenanceState || !scheduledMaintenanceState.id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-01-30 10:14:51 +00:00
|
|
|
await ScheduledMaintenanceService.changeScheduledMaintenanceState({
|
|
|
|
projectId: event.projectId!,
|
|
|
|
scheduledMaintenanceId: event.id!,
|
|
|
|
scheduledMaintenanceStateId: scheduledMaintenanceState.id,
|
2024-01-30 10:30:03 +00:00
|
|
|
shouldNotifyStatusPageSubscribers: Boolean(
|
|
|
|
event.shouldStatusPageSubscribersBeNotifiedWhenEventChangedToEnded
|
|
|
|
),
|
2024-01-30 10:14:51 +00:00
|
|
|
isSubscribersNotified: false,
|
|
|
|
notifyOwners: true,
|
|
|
|
props: {
|
2024-01-11 10:49:55 +00:00
|
|
|
isRoot: true,
|
2024-01-30 10:14:51 +00:00
|
|
|
},
|
|
|
|
});
|
2024-01-11 10:49:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|