73 lines
2.2 KiB
TypeScript
73 lines
2.2 KiB
TypeScript
import {
|
|
BaseHandler,
|
|
Handler,
|
|
Operation,
|
|
QueueSchedule,
|
|
type ExecutionContext,
|
|
type IServiceContainer
|
|
} from '@stock-bot/handlers';
|
|
|
|
@Handler('webshare')
|
|
export class WebShareHandler extends BaseHandler {
|
|
constructor(services: IServiceContainer) {
|
|
super(services);
|
|
}
|
|
|
|
@Operation('fetch-proxies')
|
|
@QueueSchedule('0 */6 * * *', { // once a month
|
|
priority: 3,
|
|
immediately: true,
|
|
description: 'Fetch fresh proxies from WebShare API',
|
|
})
|
|
async fetchProxies(_input: unknown, _context: ExecutionContext): Promise<unknown> {
|
|
this.logger.info('Fetching proxies from WebShare API');
|
|
|
|
try {
|
|
const { fetchWebShareProxies } = await import('./operations/fetch.operations');
|
|
const proxies = await fetchWebShareProxies();
|
|
|
|
if (proxies.length > 0) {
|
|
// Update the centralized proxy manager using the injected service
|
|
if (!this.proxy) {
|
|
this.logger.warn('Proxy manager is not initialized, cannot update proxies');
|
|
return {
|
|
success: false,
|
|
proxiesUpdated: 0,
|
|
error: 'Proxy manager not initialized',
|
|
};
|
|
}
|
|
await this.proxy.updateProxies(proxies);
|
|
|
|
this.logger.info('Updated proxy manager with WebShare proxies', {
|
|
count: proxies.length,
|
|
workingCount: proxies.filter(p => p.isWorking !== false).length,
|
|
});
|
|
|
|
// Cache proxy stats for monitoring using handler's cache methods
|
|
await this.cacheSet('proxy-count', proxies.length, 3600);
|
|
await this.cacheSet(
|
|
'working-count',
|
|
proxies.filter(p => p.isWorking !== false).length,
|
|
3600
|
|
);
|
|
await this.cacheSet('last-fetch', new Date().toISOString(), 1800);
|
|
|
|
return {
|
|
success: true,
|
|
proxiesUpdated: proxies.length,
|
|
workingProxies: proxies.filter(p => p.isWorking !== false).length,
|
|
};
|
|
} else {
|
|
this.logger.warn('No proxies fetched from WebShare API');
|
|
return {
|
|
success: false,
|
|
proxiesUpdated: 0,
|
|
error: 'No proxies returned from API',
|
|
};
|
|
}
|
|
} catch (error) {
|
|
this.logger.error('Failed to fetch and update proxies', { error });
|
|
throw error;
|
|
}
|
|
}
|
|
}
|