This commit is contained in:
Boki 2025-06-21 23:13:07 -04:00
parent 0c77449584
commit ca1f658be6
9 changed files with 170 additions and 766 deletions

View file

@ -0,0 +1,81 @@
/**
* QM Session Actions - Session management and creation
*/
import type { IServiceContainer } from '@stock-bot/handlers';
import { QM_SESSION_IDS, SESSION_CONFIG } from '../shared/config';
import { QMSessionManager } from '../shared/session-manager';
/**
* Check existing sessions and queue creation jobs for needed sessions
*/
export async function checkSessions(services: IServiceContainer): Promise<{
cleaned: number;
queued: number;
message: string;
}> {
const sessionManager = QMSessionManager.getInstance();
const cleanedCount = sessionManager.cleanupFailedSessions();
// Check which session IDs need more sessions and queue creation jobs
let queuedCount = 0;
for (const sessionId of Object.values(QM_SESSION_IDS)) {
if (sessionManager.needsMoreSessions(sessionId)) {
const currentCount = sessionManager.getSessions(sessionId).length;
const neededSessions = SESSION_CONFIG.MAX_SESSIONS - currentCount;
for (let i = 0; i < neededSessions; i++) {
await services.queue.getQueue('qm').add('create-session', { sessionId });
services.logger.log(`Queued job to create session for ${sessionId}`);
queuedCount++;
}
}
}
return {
cleaned: cleanedCount,
queued: queuedCount,
message: `Session check completed: cleaned ${cleanedCount}, queued ${queuedCount}`
};
}
/**
* Create a single session for a specific session ID
*/
export async function createSingleSession(
services: IServiceContainer,
input: any
): Promise<{ sessionId: string; status: string; sessionType: string }> {
const { sessionId: sessionType = 'default' } = input || {};
const sessionManager = QMSessionManager.getInstance();
// Check if we're at capacity for this session type
if (sessionManager.isAtCapacity(sessionType)) {
return {
sessionId: '',
status: 'skipped',
sessionType,
};
}
// TODO: Get actual proxy and headers from proxy service
const session = {
proxy: 'http://proxy:8080', // Placeholder
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; QMBot/1.0)',
'Accept': 'application/json'
},
successfulCalls: 0,
failedCalls: 0,
lastUsed: new Date()
};
// Add session to manager
sessionManager.addSession(sessionType, session);
return {
sessionId: sessionType,
status: 'created',
sessionType
};
}