This commit is contained in:
2025-03-24 16:06:08 +05:00
parent 630e9c8af9
commit c3b9a2e228
4 changed files with 41 additions and 11 deletions
+3
View File
@@ -11,6 +11,7 @@
"jose": "^6.0.10",
"node-cron": "^3.0.3",
"postgres": "^3.4.5",
"tree-kill": "^1.2.2",
},
"devDependencies": {
"@types/bun": "^1.2.5",
@@ -215,6 +216,8 @@
"split2": ["split2@4.2.0", "", {}, "sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg=="],
"tree-kill": ["tree-kill@1.2.2", "", { "bin": { "tree-kill": "cli.js" } }, "sha512-L0Orpi8qGpRG//Nd+H90vFB+3iHnue1zSSGmNOOCh1GLJ7rUKVwV2HvijphGQS2UmhUZewS9VgvxYIdgr+fG1A=="],
"tsx": ["tsx@4.19.3", "", { "dependencies": { "esbuild": "~0.25.0", "get-tsconfig": "^4.7.5" }, "optionalDependencies": { "fsevents": "~2.3.3" }, "bin": { "tsx": "dist/cli.mjs" } }, "sha512-4H8vUNGNjQ4V2EOoGw005+c+dGuPSnhpPBPHBtsZdGZBk/iJb4kguGlPWaZTZ3q5nMtFOEsY0nRDlh9PJyd6SQ=="],
"type-fest": ["type-fest@4.37.0", "", {}, "sha512-S/5/0kFftkq27FPNye0XM1e2NsnoD/3FS+pBmbjmmtLT6I+i344KoOf7pvXreaFsDamWeaJX55nczA1m5PsBDg=="],
+2 -1
View File
@@ -14,7 +14,8 @@
"got": "^14.4.6",
"jose": "^6.0.10",
"node-cron": "^3.0.3",
"postgres": "^3.4.5"
"postgres": "^3.4.5",
"tree-kill": "^1.2.2"
},
"devDependencies": {
"@types/bun": "^1.2.5",
+2
View File
@@ -4,6 +4,7 @@ import {
uuid,
varchar,
uniqueIndex,
integer,
} from "drizzle-orm/pg-core";
import { companiesTable } from "./companies";
import { usersTable } from "./users";
@@ -22,6 +23,7 @@ export const sessionsTable = pgTable(
})
.notNull()
.default("starting"),
pid: integer("pid"),
appId: uuid("app_id")
.notNull()
.references(() => appsTable.id),
+34 -10
View File
@@ -8,6 +8,7 @@ import { exec, execFile } from "child_process";
import { createHmac } from "crypto";
import got from "got";
import { readdirSync } from "fs";
import treeKill from "tree-kill";
type Session = typeof sessionsTable.$inferSelect & {
app: {
@@ -102,12 +103,12 @@ async function addApps() {
try {
await addApps();
console.log("Apps added");
// console.log("Apps added");
} catch (error) {
// console.error("Error adding apps", error);
}
async function getSessions() {
async function getSession() {
return await db.query.sessionsTable.findFirst({
where: and(
eq(sessionsTable.serverId, serverInfo!.id),
@@ -143,16 +144,23 @@ async function startSession(session: Session) {
const clientPhone = session.client.phone;
const clientEmail = session.client.email || "";
execFile(filePath, [
const process = execFile(filePath, [
`-ClientName=${clientName}`,
`-ClientPhone=${clientPhone}`,
`-ClientEmail=${clientEmail}`,
`-ManagerFullName=Твой любимый манагер`,
]);
// session.pid = process.pid!;
// process.on("close", () => {
// console.log("Session closed");
// endSession(session);
// });
await db
.update(sessionsTable)
.set({ status: "started" })
.set({ status: "started", pid: process.pid })
.where(eq(sessionsTable.id, session.id));
} catch (error) {
console.log("Error starting session", error);
@@ -161,7 +169,9 @@ async function startSession(session: Session) {
async function endSession(session: Session) {
try {
exec(`taskkill /IM ${session.app.fileName}.exe /F`);
if (!session) return;
treeKill(session.pid!, "SIGKILL");
await db
.update(sessionsTable)
@@ -174,7 +184,7 @@ async function endSession(session: Session) {
async function scheduleSession() {
try {
const session = await getSessions();
const session = await getSession();
if (!session) return;
@@ -183,12 +193,10 @@ async function scheduleSession() {
} else if (session.status === "ending") {
endSession(session);
} else if (session.status === "started") {
const result = exec(
`pwsh -c "Get-Process -Name ${session.app.fileName}"`
);
const result = exec(`pwsh -c "Get-Process -Id ${session.pid}"`);
result.stderr?.on("data", async (data) => {
console.log(data.toString());
console.log(data);
try {
await db
@@ -218,3 +226,19 @@ serve({
});
console.log("Server is running at http://localhost:3001");
// let count = 0;
// function test() {
// if (!(count % 100)) {
// console.log(count);
// }
// count++;
// setTimeout(() => {
// test();
// }, 0);
// }
// test();