Dispatch-Chron Jobs in Core-Server verschoben #65
This commit is contained in:
@@ -13,7 +13,6 @@ import { handleConnectDesktop } from "socket-events/connect-desktop";
|
||||
import cookieParser from "cookie-parser";
|
||||
import cors from "cors";
|
||||
import { authMiddleware } from "modules/expressMiddleware";
|
||||
import "modules/chron";
|
||||
|
||||
const app = express();
|
||||
const server = createServer(app);
|
||||
|
||||
@@ -1,155 +0,0 @@
|
||||
import { MissionLog, NotificationPayload, prisma } from "@repo/db";
|
||||
import { io } from "index";
|
||||
import cron from "node-cron";
|
||||
|
||||
const removeClosedMissions = async () => {
|
||||
const oldMissions = await prisma.mission.findMany({
|
||||
where: {
|
||||
state: "running",
|
||||
},
|
||||
});
|
||||
oldMissions.forEach(async (mission) => {
|
||||
const lastAlert = (mission.missionLog as unknown as MissionLog[]).find((l) => {
|
||||
return l.type === "alert-log";
|
||||
});
|
||||
|
||||
const lastAlertTime = lastAlert ? new Date(lastAlert.timeStamp) : null;
|
||||
|
||||
const aircraftsInMission = await prisma.connectedAircraft.findMany({
|
||||
where: {
|
||||
stationId: {
|
||||
in: mission.missionStationIds,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const allConnectedAircraftsInIdleStatus = aircraftsInMission.every((a) =>
|
||||
["1", "2", "6"].includes(a.fmsStatus),
|
||||
);
|
||||
|
||||
const allStationsInMissionChangedFromStatus4to1Or8to1 = mission.missionStationIds.every(
|
||||
(stationId) => {
|
||||
const status4Log = (mission.missionLog as unknown as MissionLog[]).findIndex((l) => {
|
||||
return (
|
||||
l.type === "station-log" &&
|
||||
l.data?.stationId === stationId &&
|
||||
l.data?.newFMSstatus === "4"
|
||||
);
|
||||
});
|
||||
const status8Log = (mission.missionLog as unknown as MissionLog[]).findIndex((l) => {
|
||||
return (
|
||||
l.type === "station-log" &&
|
||||
l.data?.stationId === stationId &&
|
||||
l.data?.newFMSstatus === "8"
|
||||
);
|
||||
});
|
||||
|
||||
const status1Log = (mission.missionLog as unknown as MissionLog[]).findIndex((l) => {
|
||||
return (
|
||||
l.type === "station-log" &&
|
||||
l.data?.stationId === stationId &&
|
||||
l.data?.newFMSstatus === "1"
|
||||
);
|
||||
});
|
||||
const status6Log = (mission.missionLog as unknown as MissionLog[]).findIndex((l) => {
|
||||
return (
|
||||
l.type === "station-log" &&
|
||||
l.data?.stationId === stationId &&
|
||||
l.data?.newFMSstatus === "6"
|
||||
);
|
||||
});
|
||||
|
||||
return (
|
||||
status4Log !== -1 &&
|
||||
(status1Log !== -1 || status6Log !== -1) &&
|
||||
(status4Log < status1Log ||
|
||||
status8Log < status1Log ||
|
||||
status8Log < status6Log ||
|
||||
status1Log < status6Log)
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
const missionHastManualReactivation = (mission.missionLog as unknown as MissionLog[]).some(
|
||||
(l) => l.type === "reopened-log",
|
||||
);
|
||||
|
||||
if (
|
||||
!allConnectedAircraftsInIdleStatus // If some aircrafts are still active, do not close the mission
|
||||
)
|
||||
return;
|
||||
|
||||
const now = new Date();
|
||||
if (!lastAlertTime) return;
|
||||
|
||||
// Case 1: Forgotten Mission, last alert more than 3 Hours ago
|
||||
// Case 2: All stations in mission changed from status 4 to 1 or from status 8 to 1
|
||||
if (
|
||||
!(
|
||||
now.getTime() - lastAlertTime.getTime() > 1000 * 60 * 180 ||
|
||||
allStationsInMissionChangedFromStatus4to1Or8to1
|
||||
) ||
|
||||
missionHastManualReactivation
|
||||
)
|
||||
return;
|
||||
|
||||
const log: MissionLog = {
|
||||
type: "completed-log",
|
||||
auto: true,
|
||||
timeStamp: new Date().toISOString(),
|
||||
data: {},
|
||||
};
|
||||
|
||||
const updatedMission = await prisma.mission.update({
|
||||
where: {
|
||||
id: mission.id,
|
||||
},
|
||||
data: {
|
||||
state: "finished",
|
||||
missionLog: {
|
||||
push: log as any,
|
||||
},
|
||||
},
|
||||
});
|
||||
io.to("dispatchers").emit("new-mission", { updatedMission });
|
||||
io.to("dispatchers").emit("notification", {
|
||||
type: "mission-auto-close",
|
||||
status: "chron",
|
||||
message: `Einsatz ${updatedMission.publicId} wurde aufgrund ${allStationsInMissionChangedFromStatus4to1Or8to1 ? "des Freimeldens aller Stationen" : "von Inaktivität"} geschlossen.`,
|
||||
data: {
|
||||
missionId: updatedMission.id,
|
||||
publicMissionId: updatedMission.publicId,
|
||||
},
|
||||
} as NotificationPayload);
|
||||
console.log(`Mission ${mission.id} closed due to inactivity.`);
|
||||
});
|
||||
};
|
||||
const removeConnectedAircrafts = async () => {
|
||||
const connectedAircrafts = await prisma.connectedAircraft.findMany({
|
||||
where: {
|
||||
logoutTime: null,
|
||||
},
|
||||
});
|
||||
|
||||
connectedAircrafts.forEach(async (aircraft) => {
|
||||
const lastUpdate = new Date(aircraft.lastHeartbeat);
|
||||
const now = new Date();
|
||||
if (now.getTime() - lastUpdate.getTime() > 12 * 60 * 60 * 1000) {
|
||||
await prisma.connectedAircraft.update({
|
||||
where: { id: aircraft.id },
|
||||
data: { logoutTime: now },
|
||||
});
|
||||
|
||||
console.log(`Aircraft ${aircraft.id} disconnected due to inactivity.`);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
cron.schedule("*/1 * * * *", async () => {
|
||||
try {
|
||||
await removeClosedMissions();
|
||||
await removeConnectedAircrafts();
|
||||
} catch (error) {
|
||||
console.error("Error on cron job:", error);
|
||||
}
|
||||
});
|
||||
@@ -7,12 +7,6 @@ export const subClient: RedisClientType = pubClient.duplicate();
|
||||
|
||||
Promise.all([pubClient.connect(), subClient.connect()]).then(() => {
|
||||
console.log("Redis connected");
|
||||
pubClient.keys("dispatchers*").then((keys) => {
|
||||
if (!keys) return;
|
||||
keys.forEach(async (key) => {
|
||||
await pubClient.json.del(key);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
pubClient.on("error", (err) => console.log("Redis Client Error", err));
|
||||
|
||||
Reference in New Issue
Block a user