import { proxyService, ProxySource } from './services/proxy.service.js'; import { getLogger, shutdownLoggers } from '@stock-bot/logger'; import { onShutdown, setShutdownTimeout } from '@stock-bot/shutdown'; // Initialize logger for the demo const logger = getLogger('proxy-demo'); /** * Example usage of the ProxyService with enhanced logging */ // async function demonstrateProxyService() { // console.log('๐Ÿš€ Starting Proxy Service Demo...'); // try { // // 1. Start the proxy refresh job (scrapes proxies every 30 minutes) // console.log('๐Ÿ“ฅ Starting proxy refresh job...'); // await proxyService.queueRefreshProxies(30 * 60 * 1000); // 30 minutes // // 2. Start health checks (checks working proxies every 15 minutes) // console.log('๐Ÿ” Starting proxy health checks...'); // await proxyService.startHealthChecks(15 * 60 * 1000); // 15 minutes // // 3. Manually scrape proxies // console.log('๐ŸŒ Manually scraping proxies...'); // const scrapedCount = await proxyService.scrapeProxies(); // console.log(`โœ… Scraped ${scrapedCount} unique proxies`); // // 4. Wait a bit for some validation to complete // await new Promise(resolve => setTimeout(resolve, 5000)); // // 5. Get proxy statistics // console.log('๐Ÿ“Š Getting proxy statistics...'); // const stats = await proxyService.getProxyStats(); // console.log('Stats:', { // total: stats.total, // working: stats.working, // failed: stats.failed, // avgResponseTime: stats.avgResponseTime + 'ms' // }); // // 6. Get a working proxy // console.log('๐ŸŽฏ Getting a working proxy...'); // const workingProxy = await proxyService.getWorkingProxy(); // if (workingProxy) { // console.log('Working proxy found:', { // host: workingProxy.host, // port: workingProxy.port, // protocol: workingProxy.protocol // }); // // 7. Test the proxy // console.log('๐Ÿงช Testing proxy...'); // const testResult = await proxyService.checkProxy(workingProxy); // console.log('Test result:', { // isWorking: testResult.isWorking, // responseTime: testResult.responseTime + 'ms', // error: testResult.error || 'None' // }); // } else { // console.log('โŒ No working proxies available yet'); // } // // 8. Get multiple working proxies // console.log('๐Ÿ“‹ Getting multiple working proxies...'); // const workingProxies = await proxyService.getWorkingProxies(5); // console.log(`Found ${workingProxies.length} working proxies`); // // 9. Example: Using a proxy with HttpClient // if (workingProxies.length > 0) { // console.log('๐Ÿ”„ Example: Using proxy with HttpClient...'); // try { // const { HttpClient } = await import('@stock-bot/http'); // const proxyClient = new HttpClient({ // proxy: workingProxies[0], // timeout: 10000 // }); // const response = await proxyClient.get('http://httpbin.org/ip'); // console.log('โœ… Request through proxy successful:', response.data); // } catch (error) { // console.log('โŒ Request through proxy failed:', error); // } // } // console.log('๐ŸŽ‰ Proxy Service Demo completed!'); // } catch (error) { // console.error('โŒ Demo failed:', error); // } // } /** * Example: Custom proxy source with enhanced logging */ async function demonstrateCustomProxySource() { logger.info('๐Ÿ”ง Demonstrating custom proxy source...'); const customSources : ProxySource[] = [ { url: 'https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt', protocol: 'http' } ]; try { const count = await proxyService.scrapeProxies(customSources); logger.info(`โœ… Scraped ${count} proxies from custom source`, { count, sourceCount: customSources.length }); } catch (error) { logger.error('โŒ Custom source scraping failed',{ error: error as Error, sourceUrls: customSources.map(s => s.url) }); } } // Export functions for use in other files export { // demonstrateProxyService, demonstrateCustomProxySource }; // Execute the demo with enhanced logging and graceful shutdown logger.info('๐Ÿš€ Starting enhanced proxy demo...'); // Configure graceful shutdown setShutdownTimeout(15000); // 15 seconds shutdown timeout // Register shutdown handlers onShutdown(async () => { logger.info('๐Ÿ”ง Cleaning up proxy service...'); try { // Clean up proxy service resources if needed // await proxyService.shutdown(); logger.info('โœ… Proxy service cleanup completed'); } catch (error) { logger.error('โŒ Proxy service cleanup failed', error); } }); onShutdown(async () => { logger.info('๐Ÿ”ง Shutting down loggers...'); try { await shutdownLoggers(); console.log('โœ… Logger shutdown completed'); } catch (error) { console.error('โŒ Logger shutdown failed:', error); } }); onShutdown(async () => { logger.info('๐Ÿ”ง Performing final cleanup...'); // Any additional cleanup can go here await new Promise(resolve => setTimeout(resolve, 1000)); // Simulate cleanup work logger.info('โœ… Final cleanup completed'); }); // Demonstrate graceful shutdown by setting up a timer to shutdown after demo const shutdownTimer = setTimeout(() => { logger.info('โฐ Demo timeout reached, initiating graceful shutdown...'); process.kill(process.pid, 'SIGTERM'); }, 20000); // Shutdown after 20 seconds // Clear timer if demo completes early const clearShutdownTimer = () => { clearTimeout(shutdownTimer); logger.info('โฐ Demo completed, canceling automatic shutdown timer'); }; demonstrateCustomProxySource() .then(() => { logger.info('๐ŸŽ‰ Proxy demo completed successfully!'); clearShutdownTimer(); // Demonstrate manual shutdown after a short delay setTimeout(() => { logger.info('๐Ÿ”„ Demonstrating manual graceful shutdown in 3 seconds...'); setTimeout(() => { process.kill(process.pid, 'SIGTERM'); }, 3000); }, 2000); }) .catch((error) => { logger.error('๐Ÿ’ฅ Proxy demo failed', error); clearShutdownTimer(); setTimeout(() => process.exit(1), 1000); }); // If this file is run directly, execute the demo // if (import.meta.main) { // demonstrateProxyService() // demonstrateCustomProxySource() // .catch(console.error); // }