fixed proxy handler
This commit is contained in:
parent
4d7c7df909
commit
cdc2f44e86
3 changed files with 79 additions and 91 deletions
|
|
@ -9,6 +9,7 @@ import { getLogger } from '@stock-bot/logger';
|
||||||
// Import handlers for bundling (ensures they're included in the build)
|
// Import handlers for bundling (ensures they're included in the build)
|
||||||
import './ceo/ceo.handler';
|
import './ceo/ceo.handler';
|
||||||
import './ib/ib.handler';
|
import './ib/ib.handler';
|
||||||
|
import './proxy/proxy.handler';
|
||||||
import './qm/qm.handler';
|
import './qm/qm.handler';
|
||||||
import './webshare/webshare.handler';
|
import './webshare/webshare.handler';
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,10 +8,6 @@ import { fetch } from '@stock-bot/utils';
|
||||||
|
|
||||||
import { PROXY_CONFIG } from '../shared/config';
|
import { PROXY_CONFIG } from '../shared/config';
|
||||||
import type { ProxySource } from '../shared/types';
|
import type { ProxySource } from '../shared/types';
|
||||||
httpClient = new HttpClient({ timeout: 10000 }, ctx.logger);
|
|
||||||
}
|
|
||||||
return httpClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function fetchProxiesFromSources(): Promise<ProxyInfo[]> {
|
export async function fetchProxiesFromSources(): Promise<ProxyInfo[]> {
|
||||||
const ctx = {
|
const ctx = {
|
||||||
|
|
|
||||||
|
|
@ -1,95 +1,86 @@
|
||||||
/**
|
|
||||||
* Proxy Provider for new queue system
|
|
||||||
*/
|
|
||||||
import type { ServiceContainer } from '@stock-bot/di';
|
|
||||||
import { getLogger } from '@stock-bot/logger';
|
|
||||||
import type { ProxyInfo } from '@stock-bot/proxy';
|
|
||||||
import {
|
import {
|
||||||
createJobHandler,
|
BaseHandler,
|
||||||
handlerRegistry,
|
Handler,
|
||||||
type HandlerConfigWithSchedule,
|
Operation,
|
||||||
} from '@stock-bot/queue';
|
ScheduledOperation,
|
||||||
|
type IServiceContainer,
|
||||||
|
} from '@stock-bot/handlers';
|
||||||
|
import type { ProxyInfo } from '@stock-bot/proxy';
|
||||||
|
import { processItems } from '@stock-bot/queue';
|
||||||
|
import { fetchProxiesFromSources } from './operations/fetch.operations';
|
||||||
|
import { checkProxy } from './operations/check.operations';
|
||||||
|
|
||||||
const handlerLogger = getLogger('proxy-handler');
|
@Handler('proxy')
|
||||||
|
export class ProxyHandler extends BaseHandler {
|
||||||
|
constructor(services: IServiceContainer) {
|
||||||
|
super(services);
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize and register the Proxy provider
|
@Operation('fetch-from-sources')
|
||||||
export function initializeProxyProvider(_container: ServiceContainer) {
|
@ScheduledOperation('proxy-fetch-and-check', '0 0 * * 0', {
|
||||||
handlerLogger.debug('Registering proxy provider with scheduled jobs...');
|
priority: 0,
|
||||||
|
description: 'Fetch and validate proxy list from sources',
|
||||||
|
// immediately: true, // Don't run immediately during startup to avoid conflicts
|
||||||
|
})
|
||||||
|
async fetchFromSources(): Promise<{
|
||||||
|
processed: number;
|
||||||
|
jobsCreated: number;
|
||||||
|
batchesCreated?: number;
|
||||||
|
mode: string;
|
||||||
|
}> {
|
||||||
|
// Fetch proxies from all configured sources
|
||||||
|
this.logger.info('Processing fetch proxies from sources request');
|
||||||
|
|
||||||
|
const proxies = await fetchProxiesFromSources();
|
||||||
|
this.logger.info('Fetched proxies from sources', { count: proxies.length });
|
||||||
|
|
||||||
const proxyProviderConfig: HandlerConfigWithSchedule = {
|
if (proxies.length === 0) {
|
||||||
name: 'proxy',
|
this.logger.warn('No proxies fetched from sources');
|
||||||
|
return { processed: 0, jobsCreated: 0, mode: 'direct' };
|
||||||
|
}
|
||||||
|
|
||||||
operations: {
|
// Get QueueManager from service container
|
||||||
'fetch-from-sources': createJobHandler(async () => {
|
const queueManager = this.queue;
|
||||||
// Fetch proxies from all configured sources
|
if (!queueManager) {
|
||||||
handlerLogger.info('Processing fetch proxies from sources request');
|
throw new Error('Queue manager not available');
|
||||||
const { fetchProxiesFromSources } = await import('./operations/fetch.operations');
|
}
|
||||||
const { processItems } = await import('@stock-bot/queue');
|
|
||||||
|
// Batch process the proxies through check-proxy operation
|
||||||
|
const batchResult = await processItems(proxies, 'proxy', {
|
||||||
|
handler: 'proxy',
|
||||||
|
operation: 'check-proxy',
|
||||||
|
totalDelayHours: 0.083, // 5 minutes (5/60 hours)
|
||||||
|
batchSize: 50, // Process 50 proxies per batch
|
||||||
|
priority: 3,
|
||||||
|
useBatching: true,
|
||||||
|
retries: 1,
|
||||||
|
ttl: 30000, // 30 second timeout per proxy check
|
||||||
|
removeOnComplete: 5,
|
||||||
|
removeOnFail: 3,
|
||||||
|
}, queueManager);
|
||||||
|
|
||||||
// Fetch all proxies from sources
|
this.logger.info('Batch proxy validation completed', {
|
||||||
const proxies = await fetchProxiesFromSources();
|
totalProxies: proxies.length,
|
||||||
handlerLogger.info('Fetched proxies from sources', { count: proxies.length });
|
jobsCreated: batchResult.jobsCreated,
|
||||||
|
mode: batchResult.mode,
|
||||||
|
batchesCreated: batchResult.batchesCreated,
|
||||||
|
duration: `${batchResult.duration}ms`,
|
||||||
|
});
|
||||||
|
|
||||||
if (proxies.length === 0) {
|
return {
|
||||||
handlerLogger.warn('No proxies fetched from sources');
|
processed: proxies.length,
|
||||||
return { processed: 0, successful: 0 };
|
jobsCreated: batchResult.jobsCreated,
|
||||||
}
|
batchesCreated: batchResult.batchesCreated,
|
||||||
|
mode: batchResult.mode,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Get QueueManager instance - we have to use getInstance for now until handlers get container access
|
@Operation('check-proxy')
|
||||||
const { QueueManager } = await import('@stock-bot/queue');
|
async checkProxyOperation(payload: ProxyInfo): Promise<unknown> {
|
||||||
const queueManager = QueueManager.getInstance();
|
// payload is now the raw proxy info object
|
||||||
|
this.logger.debug('Processing proxy check request', {
|
||||||
// Batch process the proxies through check-proxy operation
|
proxy: `${payload.host}:${payload.port}`,
|
||||||
const batchResult = await processItems(proxies, 'proxy', {
|
});
|
||||||
handler: 'proxy',
|
return checkProxy(payload);
|
||||||
operation: 'check-proxy',
|
}
|
||||||
totalDelayHours: 0.083, // 5 minutes (5/60 hours)
|
}
|
||||||
batchSize: 50, // Process 50 proxies per batch
|
|
||||||
priority: 3,
|
|
||||||
useBatching: true,
|
|
||||||
retries: 1,
|
|
||||||
ttl: 30000, // 30 second timeout per proxy check
|
|
||||||
removeOnComplete: 5,
|
|
||||||
removeOnFail: 3,
|
|
||||||
}, queueManager);
|
|
||||||
|
|
||||||
handlerLogger.info('Batch proxy validation completed', {
|
|
||||||
totalProxies: proxies.length,
|
|
||||||
jobsCreated: batchResult.jobsCreated,
|
|
||||||
mode: batchResult.mode,
|
|
||||||
batchesCreated: batchResult.batchesCreated,
|
|
||||||
duration: `${batchResult.duration}ms`,
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
processed: proxies.length,
|
|
||||||
jobsCreated: batchResult.jobsCreated,
|
|
||||||
batchesCreated: batchResult.batchesCreated,
|
|
||||||
mode: batchResult.mode,
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
|
|
||||||
'check-proxy': createJobHandler(async (payload: ProxyInfo) => {
|
|
||||||
// payload is now the raw proxy info object
|
|
||||||
handlerLogger.debug('Processing proxy check request', {
|
|
||||||
proxy: `${payload.host}:${payload.port}`,
|
|
||||||
});
|
|
||||||
const { checkProxy } = await import('./operations/check.operations');
|
|
||||||
return checkProxy(payload);
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
scheduledJobs: [
|
|
||||||
{
|
|
||||||
type: 'proxy-fetch-and-check',
|
|
||||||
operation: 'fetch-from-sources',
|
|
||||||
cronPattern: '0 0 * * 0', // Every week at midnight on Sunday
|
|
||||||
priority: 0,
|
|
||||||
description: 'Fetch and validate proxy list from sources',
|
|
||||||
// immediately: true, // Don't run immediately during startup to avoid conflicts
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
handlerRegistry.registerWithSchedule(proxyProviderConfig);
|
|
||||||
handlerLogger.debug('Proxy provider registered successfully with scheduled jobs');
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue