diff --git a/bun.lock b/bun.lock index 48eee59..97935b4 100644 --- a/bun.lock +++ b/bun.lock @@ -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=="], diff --git a/package.json b/package.json index 9852d71..334c768 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/db/schema/sessions.ts b/src/db/schema/sessions.ts index 04e151b..785931b 100644 --- a/src/db/schema/sessions.ts +++ b/src/db/schema/sessions.ts @@ -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), diff --git a/src/index.ts b/src/index.ts index dc223e4..9354d86 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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();