feat(lstv2 move): moved lstv2 into this app to keep them combined and easier to maintain
This commit is contained in:
34
lstV2/server/services/logistics/utils/excelDateStuff.ts
Normal file
34
lstV2/server/services/logistics/utils/excelDateStuff.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { getJsDateFromExcel } from "excel-date-to-js";
|
||||
|
||||
export const excelDateStuff = (serial: number, time: any = 0) => {
|
||||
// console.log(serial);
|
||||
// add 5 hours or the offset to utc
|
||||
|
||||
// get the local timezone
|
||||
const localoffset = new Date().getTimezoneOffset() / 60; // then divide by 60 to get the true number;
|
||||
|
||||
if (serial % 1 === 0) {
|
||||
time = 800;
|
||||
}
|
||||
|
||||
const addHours = serial + localoffset / 24;
|
||||
//console.log(getJsDateFromExcel(addHours));
|
||||
if (typeof serial !== "number" || serial <= 0) {
|
||||
return "invalid Date";
|
||||
}
|
||||
|
||||
const date = getJsDateFromExcel(addHours); // base date from Excel serial
|
||||
|
||||
if (time != 0) {
|
||||
// convert the time over to hour and min
|
||||
const hours = Math.floor(time / 100);
|
||||
const minutes = time % 100;
|
||||
date.setHours(hours);
|
||||
date.setMinutes(minutes);
|
||||
}
|
||||
//console.log(date.toLocaleString("en-US"), getJsDateFromExcel(addHours));
|
||||
|
||||
//console.log(serial);
|
||||
//console.log(date.toISOString());
|
||||
return date.toISOString(); //.toLocaleString("en-US"); // or .toISOString() if preferred
|
||||
};
|
||||
66
lstV2/server/services/logistics/utils/logisticsIntervals.ts
Normal file
66
lstV2/server/services/logistics/utils/logisticsIntervals.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { Cron } from "croner";
|
||||
import type { JobInfo } from "../../../types/JobInfo.js";
|
||||
import { createLog } from "../../logger/logger.js";
|
||||
|
||||
export let runningLogisticsCrons: Record<string, Cron> = {};
|
||||
|
||||
export const createLogisticsJob = (
|
||||
id: string, // this is just the name of the job running
|
||||
schedule: string, // `*/30 * * * *`; // default to be every 30 min
|
||||
timezone: string,
|
||||
task: () => Promise<void>
|
||||
) => {
|
||||
// Destroy existing job if it exists
|
||||
if (runningLogisticsCrons[id]) {
|
||||
runningLogisticsCrons[id].stop(); // Croner uses .stop() instead of .destroy()
|
||||
}
|
||||
|
||||
// Create new job with Croner
|
||||
|
||||
// console.log(timezone);
|
||||
// console.log(
|
||||
// new Date().toLocaleString("en-US", { timeZone: "America/Chicago" })
|
||||
// );
|
||||
runningLogisticsCrons[id] = new Cron(
|
||||
schedule,
|
||||
{
|
||||
timezone: timezone,
|
||||
catch: true, // Prevents unhandled rejections
|
||||
},
|
||||
task
|
||||
);
|
||||
|
||||
createLog(
|
||||
"info",
|
||||
"lst",
|
||||
"logistics",
|
||||
`Cron setup for ${id}, trigger time: ${schedule}`
|
||||
);
|
||||
|
||||
// Optional: Add error handling (Croner emits 'error' events)
|
||||
// runningNotifications[id].on("error", (err) => {
|
||||
// console.error(`Job ${id} failed:`, err);
|
||||
// });
|
||||
};
|
||||
|
||||
export const getAllLogisticsJobs = (): JobInfo[] => {
|
||||
return Object.entries(runningLogisticsCrons).map(([id, job]) => ({
|
||||
id,
|
||||
schedule: job.getPattern() || "invalid",
|
||||
nextRun: job.nextRun() || null,
|
||||
lastRun: job.previousRun() || null,
|
||||
isRunning: job ? !job.isStopped() : false,
|
||||
}));
|
||||
};
|
||||
|
||||
// const removeNotification = (id: any) => {
|
||||
// if (runningLogisticsCrons[id]) {
|
||||
// runningLogisticsCrons[id].stop();
|
||||
// delete runningLogisticsCrons[id];
|
||||
// }
|
||||
// };
|
||||
|
||||
export const stopAllLogisticsJobs = () => {
|
||||
Object.values(runningLogisticsCrons).forEach((job: any) => job.stop());
|
||||
runningLogisticsCrons = {}; // Clear the object
|
||||
};
|
||||
Reference in New Issue
Block a user