fixed proxy started working on new qm

This commit is contained in:
Boki 2025-06-26 21:47:27 -04:00
parent d989c0c814
commit e5f505335c
12 changed files with 375 additions and 115 deletions

View file

@ -8,3 +8,19 @@ export {
Disabled,
} from './decorators/decorators';
export { createJobHandler } from './utils/create-job-handler';
// Re-export commonly used types from @stock-bot/types for convenience
export type {
ExecutionContext,
IHandler,
JobHandler,
HandlerConfig,
HandlerConfigWithSchedule,
HandlerMetadata,
OperationMetadata,
ScheduledJob,
TypedJobHandler,
} from '@stock-bot/types';
// Re-export JobScheduleOptions from BaseHandler
export type { JobScheduleOptions } from './base/BaseHandler';

View file

@ -262,29 +262,27 @@ export interface Page {
// Proxy Manager types
export interface ProxyManager {
getProxy(key?: string): Promise<ProxyInfo | null>;
getProxy(): string | null;
getProxyInfo(): ProxyInfo | null;
getProxies(count: number, key?: string): Promise<ProxyInfo[]>;
releaseProxy(proxy: ProxyInfo | string): Promise<void>;
markProxyFailed(proxy: ProxyInfo | string, reason?: string): Promise<void>;
getStats(): Promise<ProxyStats>;
getStats(): ProxyStats;
resetProxy(proxy: ProxyInfo | string): Promise<void>;
blacklistProxy(proxy: ProxyInfo | string, duration?: number): Promise<void>;
isBlacklisted(proxy: ProxyInfo | string): Promise<boolean>;
refreshProxies(): Promise<void>;
}
// ProxyInfo should be imported from @stock-bot/proxy package
// to avoid duplication. Using minimal definition here for type compatibility
export interface ProxyInfo {
id: string;
host: string;
port: number;
protocol: 'http' | 'https';
username?: string;
password?: string;
protocol?: string;
country?: string;
lastUsed?: Date;
failureCount?: number;
successCount?: number;
averageResponseTime?: number;
[key: string]: any; // Allow additional properties from proxy package
}
export interface ProxyStats {

View file

@ -73,53 +73,20 @@ export class ProxyManager {
return proxyUrl;
}
/**
* Get a random working proxy from the available pool (synchronous)
* Get proxy info for the current proxy in rotation (synchronous)
*/
getRandomProxy(): ProxyInfo | null {
// Ensure initialized
if (!this.isInitialized) {
throw new Error('ProxyManager not initialized');
}
// Return null if no proxies available
getProxyInfo(): ProxyInfo | null {
if (this.proxies.length === 0) {
this.logger.warn('No proxies available in memory');
return null;
}
// Filter for working proxies (not explicitly marked as non-working)
const workingProxies = this.proxies.filter(proxy => proxy.isWorking !== false);
if (workingProxies.length === 0) {
this.logger.warn('No working proxies available');
return null;
}
// Return random proxy with preference for recently successful ones
const sortedProxies = workingProxies.sort((a, b) => {
// Prefer proxies with better success rates
const aRate = a.successRate || 0;
const bRate = b.successRate || 0;
return bRate - aRate;
});
// Take from top 50% of best performing proxies
const topProxies = sortedProxies.slice(0, Math.max(1, Math.floor(sortedProxies.length * 0.5)));
const selectedProxy = topProxies[Math.floor(Math.random() * topProxies.length)];
if (!selectedProxy) {
this.logger.warn('No proxy selected from available pool');
return null;
}
this.logger.debug('Selected proxy', {
host: selectedProxy.host,
port: selectedProxy.port,
successRate: selectedProxy.successRate,
totalAvailable: workingProxies.length,
});
return selectedProxy;
// Use same rotation logic as getProxy() to ensure consistency
// Note: We don't increment the index here since getProxy() already does that
const currentIndex = this.proxyIndex > 0 ? this.proxyIndex - 1 : this.proxies.length - 1;
const proxyInfo = this.proxies[currentIndex];
return proxyInfo || null;
}
/**