68 lines
2 KiB
TypeScript
68 lines
2 KiB
TypeScript
import {
|
|
BaseHandler,
|
|
Handler,
|
|
Operation,
|
|
QueueSchedule,
|
|
type ExecutionContext,
|
|
} from '@stock-bot/handlers';
|
|
import type { IServiceContainer } from '@stock-bot/services';
|
|
import { resolveIngestionServices } from '../../types';
|
|
|
|
@Handler('webshare')
|
|
export class WebShareHandler extends BaseHandler {
|
|
constructor(services: IServiceContainer) {
|
|
super(resolveIngestionServices(services, 'webshare'));
|
|
}
|
|
|
|
@Operation('fetch-proxies')
|
|
@QueueSchedule('0 */6 * * *', {
|
|
// every 6 hours
|
|
priority: 3,
|
|
immediately: false, // Don't run immediately since ProxyManager fetches on startup
|
|
description: 'Refresh proxies from WebShare API',
|
|
})
|
|
async fetchProxies(_input: unknown, _context: ExecutionContext): Promise<unknown> {
|
|
this.logger.info('Refreshing proxies from WebShare API');
|
|
|
|
try {
|
|
// Check if proxy manager is available
|
|
if (!this.proxy) {
|
|
this.logger.warn('Proxy manager is not initialized, cannot refresh proxies');
|
|
return {
|
|
success: false,
|
|
error: 'Proxy manager not initialized',
|
|
};
|
|
}
|
|
|
|
// Use the proxy manager's refresh method
|
|
await this.proxy.refreshProxies();
|
|
|
|
// Get stats after refresh
|
|
const stats = this.proxy.getStats();
|
|
const lastFetchTime = this.proxy.getLastFetchTime();
|
|
|
|
this.logger.info('Successfully refreshed proxies', {
|
|
total: stats.total,
|
|
working: stats.working,
|
|
failed: stats.failed,
|
|
lastFetchTime,
|
|
});
|
|
|
|
// Cache proxy stats for monitoring using handler's cache methods
|
|
await this.cacheSet('proxy-count', stats.total, 3600);
|
|
await this.cacheSet('working-count', stats.working, 3600);
|
|
await this.cacheSet('last-fetch', lastFetchTime?.toISOString() || 'unknown', 1800);
|
|
|
|
return {
|
|
success: true,
|
|
proxiesUpdated: stats.total,
|
|
workingProxies: stats.working,
|
|
failedProxies: stats.failed,
|
|
lastFetchTime,
|
|
};
|
|
} catch (error) {
|
|
this.logger.error('Failed to refresh proxies', { error });
|
|
throw error;
|
|
}
|
|
}
|
|
}
|