feat(session): persistent browser context pool with lifecycle management
- SessionContextService: in-memory Map of live Playwright handles, reused per sessionName across exec_code/open_url calls; closed on module destroy - SessionSchedulerService: @Interval closes idle sessions and deletes old closed ones using SESSION_IDLE_TIMEOUT_MINUTES / SESSION_DELETE_CLOSED_DAYS - SessionService: onApplicationBootstrap closes all open sessions on restart; upsert marks status=open and sets lastUsedAt; adds markOpen/markClosed/ touchLastUsed/findExpiredOpen/findOldClosed/findById helpers - SessionEntity: status (open|closed) and lastUsedAt columns added - AuthService: keeps browser alive after login, registers context in pool - BrowserService: named sessions reuse persistent context; anonymous remain ephemeral - AppConfig: all config fields declared with typed defaults; validate wired into ConfigModule so mis-configuration fails fast at startup - ConfigService<AppConfig, true> used everywhere — no more untyped get() calls
This commit is contained in:
1 parent
b2edac062f
commit
9e79cad237
14 files changed
+424
-86
No files matched your search
@@ -6,12 +6,14 @@ import {
|
||||
} from "@nestjs/common";
|
||||
import { TraceLogger } from "../common/trace-logger";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { AppConfig } from "../config/app.config";
|
||||
import { chromium } from "playwright";
|
||||
import type { Page } from "playwright";
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import * as crypto from "crypto";
|
||||
import { SessionService } from "../session/session.service";
|
||||
import { SessionContextService } from "../session/session-context.service";
|
||||
import { EnvironmentService } from "../environment/environment.service";
|
||||
|
||||
interface KeyDescriptor {
|
||||
@@ -26,11 +28,12 @@ export class AuthService {
|
||||
private readonly keysDir: string;
|
||||
|
||||
constructor(
|
||||
private readonly config: ConfigService,
|
||||
private readonly config: ConfigService<AppConfig, true>,
|
||||
private readonly sessionService: SessionService,
|
||||
private readonly sessionContextService: SessionContextService,
|
||||
private readonly environmentService: EnvironmentService,
|
||||
) {
|
||||
this.keysDir = path.resolve(this.config.get<string>("KEYS_DIR", "keys"));
|
||||
this.keysDir = path.resolve(this.config.get("KEYS_DIR"));
|
||||
}
|
||||
|
||||
listKeys(): string[] {
|
||||
@@ -169,11 +172,21 @@ export class AuthService {
|
||||
localStorageData,
|
||||
);
|
||||
|
||||
// Register the live Playwright context — browser stays open for reuse
|
||||
this.sessionContextService.register(
|
||||
resolvedSession,
|
||||
browser,
|
||||
context,
|
||||
page,
|
||||
);
|
||||
|
||||
this.logger.log(
|
||||
`Login successful for key ${keyId}, session: ${resolvedSession}`,
|
||||
);
|
||||
return { token, sessionName: resolvedSession };
|
||||
} catch (err) {
|
||||
// Close browser only on failure — on success it is kept alive in SessionContextService
|
||||
await browser.close().catch(() => {});
|
||||
if (
|
||||
err instanceof BadRequestException ||
|
||||
err instanceof InternalServerErrorException
|
||||
@@ -185,8 +198,6 @@ export class AuthService {
|
||||
`Login automation failed: ${(err as Error).message}`,
|
||||
{ cause: err },
|
||||
);
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user