82 lines
2.6 KiB
TypeScript
82 lines
2.6 KiB
TypeScript
/**
|
|
* WebShare Provider for proxy management with scheduled updates
|
|
*/
|
|
import { getLogger } from '@stock-bot/logger';
|
|
import {
|
|
createJobHandler,
|
|
handlerRegistry,
|
|
type HandlerConfigWithSchedule,
|
|
} from '@stock-bot/queue';
|
|
import { updateProxies } from '@stock-bot/utils';
|
|
import type { ServiceContainer } from '@stock-bot/di';
|
|
|
|
const logger = getLogger('webshare-provider');
|
|
|
|
// Initialize and register the WebShare provider
|
|
export function initializeWebShareProvider(_container: ServiceContainer) {
|
|
logger.debug('Registering WebShare provider with scheduled jobs...');
|
|
|
|
const webShareProviderConfig: HandlerConfigWithSchedule = {
|
|
name: 'webshare',
|
|
|
|
operations: {
|
|
'fetch-proxies': createJobHandler(async () => {
|
|
logger.info('Fetching proxies from WebShare API');
|
|
const { fetchWebShareProxies } = await import('./operations/fetch.operations');
|
|
|
|
try {
|
|
const proxies = await fetchWebShareProxies();
|
|
|
|
if (proxies.length > 0) {
|
|
// Update the centralized proxy manager
|
|
await updateProxies(proxies);
|
|
|
|
logger.info('Updated proxy manager with WebShare proxies', {
|
|
count: proxies.length,
|
|
workingCount: proxies.filter(p => p.isWorking !== false).length,
|
|
});
|
|
|
|
return {
|
|
success: true,
|
|
proxiesUpdated: proxies.length,
|
|
workingProxies: proxies.filter(p => p.isWorking !== false).length,
|
|
};
|
|
} else {
|
|
logger.warn('No proxies fetched from WebShare API');
|
|
return {
|
|
success: false,
|
|
proxiesUpdated: 0,
|
|
error: 'No proxies returned from API',
|
|
};
|
|
}
|
|
} catch (error) {
|
|
logger.error('Failed to fetch and update proxies', { error });
|
|
return {
|
|
success: false,
|
|
proxiesUpdated: 0,
|
|
error: error instanceof Error ? error.message : 'Unknown error',
|
|
};
|
|
}
|
|
}),
|
|
},
|
|
|
|
scheduledJobs: [
|
|
{
|
|
type: 'webshare-fetch',
|
|
operation: 'fetch-proxies',
|
|
cronPattern: '0 */6 * * *', // Every 6 hours
|
|
priority: 3,
|
|
description: 'Fetch fresh proxies from WebShare API',
|
|
immediately: true, // Run on startup
|
|
},
|
|
],
|
|
};
|
|
|
|
handlerRegistry.registerWithSchedule(webShareProviderConfig);
|
|
logger.debug('WebShare provider registered successfully');
|
|
}
|
|
|
|
export const webShareProvider = {
|
|
initialize: (container: ServiceContainer) => initializeWebShareProvider(container),
|
|
};
|
|
|