Initial proxy manager refactor

This commit is contained in:
Boki 2025-06-20 12:20:06 -04:00
parent 84cb14680b
commit da916222c1
6 changed files with 58 additions and 37 deletions

View file

@ -11,6 +11,7 @@ import { getLogger, setLoggerConfig, shutdownLoggers } from '@stock-bot/logger';
import { connectMongoDB } from '@stock-bot/mongodb-client';
import { connectPostgreSQL } from '@stock-bot/postgres-client';
import { QueueManager, type QueueManagerConfig } from '@stock-bot/queue';
import { ProxyManager } from '@stock-bot/utils';
import { Shutdown } from '@stock-bot/shutdown';
// Local imports
import { exchangeRoutes, healthRoutes, queueRoutes } from './routes';
@ -116,8 +117,7 @@ async function initializeServices() {
// Initialize proxy manager
logger.debug('Initializing proxy manager...');
const { proxyManager } = await import('@stock-bot/utils');
await proxyManager.initialize();
await ProxyManager.initialize();
logger.info('Proxy manager initialized');
// Initialize providers (register handlers and scheduled jobs)
@ -257,4 +257,4 @@ startServer().catch(error => {
logger.info('Data service startup initiated');
// Queue manager is available via QueueManager.getInstance() singleton pattern
// ProxyManager class and singleton instance are available via @stock-bot/utils

View file

@ -2,7 +2,7 @@ import { getRandomUserAgent } from '@stock-bot/http';
import { getLogger } from '@stock-bot/logger';
import { getMongoDBClient } from '@stock-bot/mongodb-client';
import { QueueManager } from '@stock-bot/queue';
import { proxyManager } from '@stock-bot/utils';
import { ProxyManager } from '@stock-bot/utils';
// Shared instances (module-scoped, not global)
let isInitialized = false; // Track if resources are initialized
@ -90,7 +90,7 @@ export async function createSessions(): Promise<void> {
while (sessionCache[sessionId].length < 50) {
logger.info(`Creating new session for ${sessionId}`);
const proxyInfo = await proxyManager.getRandomProxy();
const proxyInfo = await ProxyManager.getInstance().getRandomProxy();
if (!proxyInfo) {
logger.error('No proxy available for QM session creation');
break; // Skip session creation if no proxy is available
@ -294,7 +294,7 @@ async function searchAndSpawnJobs(
// API call function to search symbols via QM
async function searchQMSymbolsAPI(query: string): Promise<string[]> {
const proxyInfo = await proxyManager.getRandomProxy();
const proxyInfo = await ProxyManager.getInstance().getRandomProxy();
if (!proxyInfo) {
throw new Error('No proxy available for QM API call');

View file

@ -7,7 +7,7 @@ import {
handlerRegistry,
type HandlerConfigWithSchedule,
} from '@stock-bot/queue';
import { proxyManager } from '@stock-bot/utils';
import { ProxyManager } from '@stock-bot/utils';
const logger = getLogger('webshare-provider');
@ -28,7 +28,7 @@ export function initializeWebShareProvider() {
if (proxies.length > 0) {
// Update the centralized proxy manager
await proxyManager.updateProxies(proxies);
await ProxyManager.getInstance().updateProxies(proxies);
logger.info('Updated proxy manager with WebShare proxies', {
count: proxies.length,
@ -66,7 +66,7 @@ export function initializeWebShareProvider() {
const validationResults = await validateStoredProxies();
// Update proxy manager with validated proxies
await proxyManager.updateProxies(validationResults.workingProxies);
await ProxyManager.getInstance().updateProxies(validationResults.workingProxies);
logger.info('Proxy validation completed', {
totalChecked: validationResults.totalChecked,
@ -87,7 +87,7 @@ export function initializeWebShareProvider() {
}),
'get-stats': createJobHandler(async () => {
const stats = proxyManager.getStats();
const stats = ProxyManager.getInstance().getStats();
logger.info('Proxy manager statistics', stats);
return stats;
}),
@ -128,7 +128,7 @@ export function initializeWebShareProvider() {
// Legacy function for backward compatibility - now uses centralized proxy manager
export async function getProxy(): Promise<string | null> {
const proxy = await proxyManager.getRandomProxy();
const proxy = ProxyManager.getInstance().getRandomProxy();
if (!proxy) {
return null;
}

View file

@ -3,7 +3,7 @@
*/
import { getLogger } from '@stock-bot/logger';
import { HttpClient, type ProxyInfo } from '@stock-bot/http';
import { proxyManager } from '@stock-bot/utils';
import { ProxyManager } from '@stock-bot/utils';
const logger = getLogger('webshare-tasks');
@ -94,7 +94,7 @@ export async function validateStoredProxies(): Promise<{
const testUrl = 'https://httpbin.org/ip'; // Simple IP echo service
// Get all proxies from proxy manager
const allProxies = await proxyManager.getAllProxies();
const allProxies = ProxyManager.getInstance().getAllProxies();
if (allProxies.length === 0) {
logger.warn('No proxies available for validation');