fixed queue and finished initial qm work
This commit is contained in:
parent
52436c69b2
commit
00fbd31364
4 changed files with 45 additions and 56 deletions
|
|
@ -1,34 +0,0 @@
|
||||||
/**
|
|
||||||
* QM Exchanges Operations - Simple exchange data fetching
|
|
||||||
*/
|
|
||||||
|
|
||||||
import type { IServiceContainer } from '@stock-bot/handlers';
|
|
||||||
|
|
||||||
interface QMExchange {
|
|
||||||
_id?: string;
|
|
||||||
code: string;
|
|
||||||
name: string;
|
|
||||||
country: string;
|
|
||||||
currency: string;
|
|
||||||
timezone?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function fetchExchanges(services: IServiceContainer): Promise<QMExchange[]> {
|
|
||||||
// Get exchanges from MongoDB
|
|
||||||
const exchanges = await services.mongodb
|
|
||||||
.collection<QMExchange>('qm_exchanges')
|
|
||||||
.find({})
|
|
||||||
.toArray();
|
|
||||||
|
|
||||||
return exchanges;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getExchangeByCode(
|
|
||||||
services: IServiceContainer,
|
|
||||||
code: string
|
|
||||||
): Promise<QMExchange | null> {
|
|
||||||
// Get specific exchange by code
|
|
||||||
const exchange = await services.mongodb.collection<QMExchange>('qm_exchanges').findOne({ code });
|
|
||||||
|
|
||||||
return exchange;
|
|
||||||
}
|
|
||||||
|
|
@ -43,7 +43,7 @@ export async function checkSessions(
|
||||||
const neededSessions = SESSION_CONFIG.MAX_SESSIONS - currentCount;
|
const neededSessions = SESSION_CONFIG.MAX_SESSIONS - currentCount;
|
||||||
|
|
||||||
// Queue up to 10 at a time to avoid overwhelming the system
|
// Queue up to 10 at a time to avoid overwhelming the system
|
||||||
const toQueue = Math.min(neededSessions, 10);
|
const toQueue = Math.min(neededSessions, 20);
|
||||||
|
|
||||||
for (let i = 0; i < toQueue; i++) {
|
for (let i = 0; i < toQueue; i++) {
|
||||||
await this.scheduleOperation('create-session', { sessionId, sessionType }, {
|
await this.scheduleOperation('create-session', { sessionId, sessionType }, {
|
||||||
|
|
|
||||||
|
|
@ -443,12 +443,12 @@ export class QueueManager {
|
||||||
const queueShutdownPromises = Array.from(this.queues.values()).map(async queue => {
|
const queueShutdownPromises = Array.from(this.queues.values()).map(async queue => {
|
||||||
try {
|
try {
|
||||||
// Add timeout to queue.close() to prevent hanging
|
// Add timeout to queue.close() to prevent hanging
|
||||||
await queue.close();
|
const closePromise = queue.close();
|
||||||
// const timeoutPromise = new Promise<never>((_, reject) =>
|
const timeoutPromise = new Promise<never>((_, reject) =>
|
||||||
// setTimeout(() => reject(new Error('Queue close timeout')), 100)
|
setTimeout(() => reject(new Error('Queue close timeout')), 5000) // 5 second timeout per queue
|
||||||
// );
|
);
|
||||||
|
|
||||||
// await Promise.race([closePromise, timeoutPromise]);
|
await Promise.race([closePromise, timeoutPromise]);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.warn('Error closing queue', { error: (error as Error).message });
|
this.logger.warn('Error closing queue', { error: (error as Error).message });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -264,26 +264,49 @@ export class Queue {
|
||||||
*/
|
*/
|
||||||
async close(): Promise<void> {
|
async close(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
// Close the queue itself
|
// Close workers first with timeout
|
||||||
await this.bullQueue.close();
|
|
||||||
this.logger.info('Queue closed', { queueName: this.queueName });
|
|
||||||
|
|
||||||
// Close queue events
|
|
||||||
if (this.queueEvents) {
|
|
||||||
await this.queueEvents.close();
|
|
||||||
this.logger.debug('Queue events closed', { queueName: this.queueName });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close workers first
|
|
||||||
if (this.workers.length > 0) {
|
if (this.workers.length > 0) {
|
||||||
await Promise.all(
|
const workerClosePromises = this.workers.map(async (worker, idx) => {
|
||||||
this.workers.map(async worker => {
|
try {
|
||||||
return await worker.close();
|
const closePromise = worker.close();
|
||||||
})
|
const timeoutPromise = new Promise<void>((resolve) => {
|
||||||
);
|
setTimeout(() => {
|
||||||
|
this.logger.warn('Worker close timeout, forcing closure', {
|
||||||
|
queueName: this.queueName,
|
||||||
|
workerId: idx
|
||||||
|
});
|
||||||
|
resolve();
|
||||||
|
}, 2000); // 2 second timeout per worker
|
||||||
|
});
|
||||||
|
|
||||||
|
await Promise.race([closePromise, timeoutPromise]);
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.warn('Error closing worker', {
|
||||||
|
queueName: this.queueName,
|
||||||
|
workerId: idx,
|
||||||
|
error
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await Promise.all(workerClosePromises);
|
||||||
this.workers = [];
|
this.workers = [];
|
||||||
this.logger.debug('Workers closed', { queueName: this.queueName });
|
this.logger.debug('Workers closed', { queueName: this.queueName });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close queue events
|
||||||
|
if (this.queueEvents) {
|
||||||
|
try {
|
||||||
|
await this.queueEvents.close();
|
||||||
|
this.logger.debug('Queue events closed', { queueName: this.queueName });
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.warn('Error closing queue events', { queueName: this.queueName, error });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the queue itself
|
||||||
|
await this.bullQueue.close();
|
||||||
|
this.logger.info('Queue closed', { queueName: this.queueName });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error('Error closing queue', { queueName: this.queueName, error });
|
this.logger.error('Error closing queue', { queueName: this.queueName, error });
|
||||||
throw error;
|
throw error;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue