working on fixing logger
This commit is contained in:
parent
5b8c91f31d
commit
4709887fef
3 changed files with 16 additions and 45 deletions
|
|
@ -1,7 +1,6 @@
|
||||||
import { createLogger } from '@stock-bot/logger';
|
import { Logger } from '@stock-bot/logger';
|
||||||
import createCache, { type CacheProvider } from '@stock-bot/cache';
|
import createCache, { type CacheProvider } from '@stock-bot/cache';
|
||||||
import { HttpClient, HttpClientConfig, ProxyConfig , RequestConfig } from '@stock-bot/http-client';
|
import { HttpClient, HttpClientConfig, ProxyConfig , RequestConfig } from '@stock-bot/http-client';
|
||||||
import type { Logger as PinoLogger } from 'pino';
|
|
||||||
|
|
||||||
export interface ProxySource {
|
export interface ProxySource {
|
||||||
url: string;
|
url: string;
|
||||||
|
|
@ -33,7 +32,7 @@ export interface ProxyData extends ProxyConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ProxyService {
|
export class ProxyService {
|
||||||
private logger: PinoLogger;
|
private logger;
|
||||||
private cache: CacheProvider;
|
private cache: CacheProvider;
|
||||||
private httpClient: HttpClient;
|
private httpClient: HttpClient;
|
||||||
private readonly CACHE_PREFIX = 'proxy:';
|
private readonly CACHE_PREFIX = 'proxy:';
|
||||||
|
|
@ -73,7 +72,7 @@ export class ProxyService {
|
||||||
];
|
];
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.logger = createLogger('proxy-service');
|
this.logger = new Logger('proxy-service');
|
||||||
|
|
||||||
this.cache = createCache('hybrid');
|
this.cache = createCache('hybrid');
|
||||||
|
|
||||||
|
|
@ -142,7 +141,6 @@ export class ProxyService {
|
||||||
|
|
||||||
// Start validation of new proxies
|
// Start validation of new proxies
|
||||||
this.validateProxiesInBackground(uniqueProxies);
|
this.validateProxiesInBackground(uniqueProxies);
|
||||||
|
|
||||||
return uniqueProxies.length;
|
return uniqueProxies.length;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|
@ -231,33 +229,6 @@ export class ProxyService {
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* Store proxies in cache
|
|
||||||
*/
|
|
||||||
private async storeProxies(proxies: ProxyConfig[]): Promise<void> {
|
|
||||||
try {
|
|
||||||
for (const proxy of proxies) {
|
|
||||||
const key = this.getProxyKey(proxy);
|
|
||||||
const data: ProxyData = {
|
|
||||||
protocol: proxy.protocol,
|
|
||||||
host: proxy.host,
|
|
||||||
port: proxy.port,
|
|
||||||
username: proxy.username,
|
|
||||||
password: proxy.password,
|
|
||||||
addedAt: new Date(),
|
|
||||||
lastChecked: null,
|
|
||||||
isWorking: null,
|
|
||||||
responseTime: null
|
|
||||||
};
|
|
||||||
|
|
||||||
await this.cache.set(key, data, 86400); // 24 hours TTL
|
|
||||||
}
|
|
||||||
|
|
||||||
this.logger.info('Stored proxies in cache', { count: proxies.length });
|
|
||||||
} catch (error) {
|
|
||||||
this.logger.error('Error storing proxies', { error });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if a proxy is working
|
* Check if a proxy is working
|
||||||
|
|
@ -265,16 +236,14 @@ export class ProxyService {
|
||||||
async checkProxy(proxy: ProxyConfig, checkUrl: string = this.DEFAULT_CHECK_URL): Promise<ProxyCheckResult> {
|
async checkProxy(proxy: ProxyConfig, checkUrl: string = this.DEFAULT_CHECK_URL): Promise<ProxyCheckResult> {
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
try {
|
try {
|
||||||
|
this.logger.debug('Proxy check initiate request', {
|
||||||
// Create a new HttpClient instance with the proxy
|
proxy: proxy.host + ':' + proxy.port,
|
||||||
const proxyClient = new HttpClient({
|
|
||||||
timeout: this.CHECK_TIMEOUT,
|
|
||||||
proxy: proxy
|
|
||||||
});
|
});
|
||||||
|
const response = await this.httpClient.get(checkUrl, {proxy: proxy, timeout: this.CHECK_TIMEOUT});
|
||||||
const response = await proxyClient.get(checkUrl);
|
|
||||||
const responseTime = Date.now() - startTime;
|
const responseTime = Date.now() - startTime;
|
||||||
|
this.logger.debug('Proxy check response', {
|
||||||
|
proxy: proxy.host + ':' + proxy.port,
|
||||||
|
});
|
||||||
if (response.status >= 200 && response.status < 300 && !response.data.contains(this.DEFAULT_IP_ADDRESS)) {
|
if (response.status >= 200 && response.status < 300 && !response.data.contains(this.DEFAULT_IP_ADDRESS)) {
|
||||||
const result: ProxyCheckResult = {
|
const result: ProxyCheckResult = {
|
||||||
proxy,
|
proxy,
|
||||||
|
|
@ -471,7 +440,7 @@ export class ProxyService {
|
||||||
*/
|
*/
|
||||||
private async validateProxiesInBackground(proxies: ProxyConfig[]): Promise<void> {
|
private async validateProxiesInBackground(proxies: ProxyConfig[]): Promise<void> {
|
||||||
this.logger.info('Starting background proxy validation', { count: proxies.length });
|
this.logger.info('Starting background proxy validation', { count: proxies.length });
|
||||||
|
this.logger.error('Background validation is not implemented yet, this is a placeholder function', { error: { message: 'Background validation not implemented' } });
|
||||||
const concurrency = 50; // Process 50 proxies concurrently
|
const concurrency = 50; // Process 50 proxies concurrently
|
||||||
const chunks = this.chunkArray(proxies, concurrency);
|
const chunks = this.chunkArray(proxies, concurrency);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ export interface RequestConfig {
|
||||||
headers?: Record<string, string>;
|
headers?: Record<string, string>;
|
||||||
body?: any;
|
body?: any;
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
|
proxy?: ProxyConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface HttpResponse<T = any> {
|
export interface HttpResponse<T = any> {
|
||||||
|
|
|
||||||
|
|
@ -41,11 +41,13 @@ function createTransports(serviceName: string, options?: {
|
||||||
target: 'pino-pretty',
|
target: 'pino-pretty',
|
||||||
level: loggingConfig.LOG_LEVEL,
|
level: loggingConfig.LOG_LEVEL,
|
||||||
options: {
|
options: {
|
||||||
minimumLevel: 'info',
|
|
||||||
colorize: true,
|
colorize: true,
|
||||||
translateTime: 'yyyy-mm-dd HH:MM:ss.l',
|
translateTime: 'yyyy-mm-dd HH:MM:ss.l',
|
||||||
ignore: 'pid,hostname,service,environment,version',
|
ignore: 'pid,hostname',
|
||||||
messageFormat: '[{service}] {msg}',
|
// messageFormat: '[{service}] {msg}',
|
||||||
|
// errorLikeObjectKeys: ['err', 'error'], // Tell pino-pretty these are error objects
|
||||||
|
// errorProps: 'message,stack,name,code', // Which error properties to show
|
||||||
|
singleLine: true,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -192,7 +194,6 @@ export class Logger {
|
||||||
const logData = {
|
const logData = {
|
||||||
...this.context,
|
...this.context,
|
||||||
...metadata,
|
...metadata,
|
||||||
timestamp: new Date().toISOString()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (typeof message === 'string') {
|
if (typeof message === 'string') {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue