log verbose in file redis

This commit is contained in:
Davidson Gomes 2023-07-05 12:54:19 -03:00
parent f51c3b6519
commit a2779612be
5 changed files with 69 additions and 6 deletions

2
.gitignore vendored
View File

@ -33,3 +33,5 @@ lerna-debug.log*
!/instances/.gitkeep !/instances/.gitkeep
/test/ /test/
/src/env.yml /src/env.yml
/temp/*

View File

@ -41,9 +41,9 @@
"homepage": "https://github.com/DavidsonGomes/evolution-api#readme", "homepage": "https://github.com/DavidsonGomes/evolution-api#readme",
"dependencies": { "dependencies": {
"@adiwajshing/keyed-db": "^0.2.4", "@adiwajshing/keyed-db": "^0.2.4",
"@whiskeysockets/baileys": "^6.3.0",
"@ffmpeg-installer/ffmpeg": "^1.1.0", "@ffmpeg-installer/ffmpeg": "^1.1.0",
"@hapi/boom": "^10.0.1", "@hapi/boom": "^10.0.1",
"@whiskeysockets/baileys": "^6.3.0",
"axios": "^1.3.5", "axios": "^1.3.5",
"class-validator": "^0.13.2", "class-validator": "^0.13.2",
"compression": "^1.7.4", "compression": "^1.7.4",

View File

@ -67,7 +67,6 @@ export class Logger {
this.configService.get<Log>('LOG').LEVEL.forEach((level) => types.push(Type[level])); this.configService.get<Log>('LOG').LEVEL.forEach((level) => types.push(Type[level]));
const typeValue = typeof value; const typeValue = typeof value;
if (types.includes(type)) { if (types.includes(type)) {
if (configService.get<Log>('LOG').COLOR) { if (configService.get<Log>('LOG').COLOR) {
console.log( console.log(

View File

@ -5,8 +5,11 @@ import { Redis } from '../config/env.config';
export class RedisCache { export class RedisCache {
constructor() { constructor() {
this.logger.verbose('instance created');
process.on('beforeExit', async () => { process.on('beforeExit', async () => {
this.logger.verbose('instance destroyed');
if (this.statusConnection) { if (this.statusConnection) {
this.logger.verbose('instance disconnect');
await this.client.disconnect(); await this.client.disconnect();
} }
}); });
@ -17,11 +20,14 @@ export class RedisCache {
private redisEnv: Redis; private redisEnv: Redis;
public set reference(reference: string) { public set reference(reference: string) {
this.logger.verbose('set reference: ' + reference);
this.instanceName = reference; this.instanceName = reference;
} }
public async connect(redisEnv: Redis) { public async connect(redisEnv: Redis) {
this.logger.verbose('connecting');
this.client = createClient({ url: redisEnv.URI }); this.client = createClient({ url: redisEnv.URI });
this.logger.verbose('connected in ' + redisEnv.URI);
await this.client.connect(); await this.client.connect();
this.statusConnection = true; this.statusConnection = true;
this.redisEnv = redisEnv; this.redisEnv = redisEnv;
@ -32,6 +38,7 @@ export class RedisCache {
public async instanceKeys(): Promise<string[]> { public async instanceKeys(): Promise<string[]> {
try { try {
this.logger.verbose('instance keys: ' + this.redisEnv.PREFIX_KEY + ':*');
return await this.client.sendCommand(['keys', this.redisEnv.PREFIX_KEY + ':*']); return await this.client.sendCommand(['keys', this.redisEnv.PREFIX_KEY + ':*']);
} catch (error) { } catch (error) {
this.logger.error(error); this.logger.error(error);
@ -40,13 +47,16 @@ export class RedisCache {
public async keyExists(key?: string) { public async keyExists(key?: string) {
if (key) { if (key) {
this.logger.verbose('keyExists: ' + key);
return !!(await this.instanceKeys()).find((i) => i === key); return !!(await this.instanceKeys()).find((i) => i === key);
} }
this.logger.verbose('keyExists: ' + this.instanceName);
return !!(await this.instanceKeys()).find((i) => i === this.instanceName); return !!(await this.instanceKeys()).find((i) => i === this.instanceName);
} }
public async writeData(field: string, data: any) { public async writeData(field: string, data: any) {
try { try {
this.logger.verbose('writeData: ' + field);
const json = JSON.stringify(data, BufferJSON.replacer); const json = JSON.stringify(data, BufferJSON.replacer);
return await this.client.hSet( return await this.client.hSet(
@ -61,14 +71,19 @@ export class RedisCache {
public async readData(field: string) { public async readData(field: string) {
try { try {
this.logger.verbose('readData: ' + field);
const data = await this.client.hGet( const data = await this.client.hGet(
this.redisEnv.PREFIX_KEY + ':' + this.instanceName, this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
field, field,
); );
if (data) { if (data) {
this.logger.verbose('readData: ' + field + ' success');
return JSON.parse(data, BufferJSON.reviver); return JSON.parse(data, BufferJSON.reviver);
} }
this.logger.verbose('readData: ' + field + ' not found');
return null;
} catch (error) { } catch (error) {
this.logger.error(error); this.logger.error(error);
} }
@ -76,6 +91,7 @@ export class RedisCache {
public async removeData(field: string) { public async removeData(field: string) {
try { try {
this.logger.verbose('removeData: ' + field);
return await this.client.hDel( return await this.client.hDel(
this.redisEnv.PREFIX_KEY + ':' + this.instanceName, this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
field, field,
@ -87,6 +103,7 @@ export class RedisCache {
public async delAll(hash?: string) { public async delAll(hash?: string) {
try { try {
this.logger.verbose('instance delAll: ' + hash);
return await this.client.del( return await this.client.del(
hash || this.redisEnv.PREFIX_KEY + ':' + this.instanceName, hash || this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
); );

View File

@ -126,6 +126,7 @@ export class WAStartupService {
private readonly repository: RepositoryBroker, private readonly repository: RepositoryBroker,
private readonly cache: RedisCache, private readonly cache: RedisCache,
) { ) {
this.logger.verbose('WAStartupService initialized');
this.cleanStore(); this.cleanStore();
this.instance.qrcode = { count: 0 }; this.instance.qrcode = { count: 0 };
} }
@ -148,6 +149,7 @@ export class WAStartupService {
return; return;
} }
this.instance.name = name; this.instance.name = name;
this.logger.verbose(`Instance '${this.instance.name}' initialized`);
this.sendDataWebhook(Events.STATUS_INSTANCE, { this.sendDataWebhook(Events.STATUS_INSTANCE, {
instance: this.instance.name, instance: this.instance.name,
status: 'created', status: 'created',
@ -163,9 +165,12 @@ export class WAStartupService {
} }
public async getProfileName() { public async getProfileName() {
this.logger.verbose('Getting profile name');
let profileName = this.client.user?.name ?? this.client.user?.verifiedName; let profileName = this.client.user?.name ?? this.client.user?.verifiedName;
if (!profileName) { if (!profileName) {
this.logger.verbose('Profile name not found, trying to get from database');
if (this.configService.get<Database>('DATABASE').ENABLED) { if (this.configService.get<Database>('DATABASE').ENABLED) {
this.logger.verbose('Database enabled, trying to get from database');
const collection = dbserver const collection = dbserver
.getClient() .getClient()
.db( .db(
@ -175,10 +180,12 @@ export class WAStartupService {
.collection(this.instanceName); .collection(this.instanceName);
const data = await collection.findOne({ _id: 'creds' }); const data = await collection.findOne({ _id: 'creds' });
if (data) { if (data) {
this.logger.verbose('Profile name found in database');
const creds = JSON.parse(JSON.stringify(data), BufferJSON.reviver); const creds = JSON.parse(JSON.stringify(data), BufferJSON.reviver);
profileName = creds.me?.name || creds.me?.verifiedName; profileName = creds.me?.name || creds.me?.verifiedName;
} }
} else if (existsSync(join(INSTANCE_DIR, this.instanceName, 'creds.json'))) { } else if (existsSync(join(INSTANCE_DIR, this.instanceName, 'creds.json'))) {
this.logger.verbose('Profile name found in file');
const creds = JSON.parse( const creds = JSON.parse(
readFileSync(join(INSTANCE_DIR, this.instanceName, 'creds.json'), { readFileSync(join(INSTANCE_DIR, this.instanceName, 'creds.json'), {
encoding: 'utf-8', encoding: 'utf-8',
@ -187,20 +194,26 @@ export class WAStartupService {
profileName = creds.me?.name || creds.me?.verifiedName; profileName = creds.me?.name || creds.me?.verifiedName;
} }
} }
this.logger.verbose(`Profile name: ${profileName}`);
return profileName; return profileName;
} }
public async getProfileStatus() { public async getProfileStatus() {
this.logger.verbose('Getting profile status');
const status = await this.client.fetchStatus(this.instance.wuid); const status = await this.client.fetchStatus(this.instance.wuid);
this.logger.verbose(`Profile status: ${status.status}`);
return status.status; return status.status;
} }
public get profilePictureUrl() { public get profilePictureUrl() {
this.logger.verbose('Getting profile picture url');
return this.instance.profilePictureUrl; return this.instance.profilePictureUrl;
} }
public get qrCode(): wa.QrCode { public get qrCode(): wa.QrCode {
this.logger.verbose('Getting qrcode');
return { return {
code: this.instance.qrcode?.code, code: this.instance.qrcode?.code,
base64: this.instance.qrcode?.base64, base64: this.instance.qrcode?.base64,
@ -208,20 +221,44 @@ export class WAStartupService {
} }
private async loadWebhook() { private async loadWebhook() {
this.logger.verbose('Loading webhook');
const data = await this.repository.webhook.find(this.instanceName); const data = await this.repository.webhook.find(this.instanceName);
this.localWebhook.url = data?.url; this.localWebhook.url = data?.url;
this.logger.verbose(`Webhook url: ${this.localWebhook.url}`);
this.localWebhook.enabled = data?.enabled; this.localWebhook.enabled = data?.enabled;
this.logger.verbose(`Webhook enabled: ${this.localWebhook.enabled}`);
this.localWebhook.events = data?.events; this.localWebhook.events = data?.events;
this.logger.verbose(`Webhook events: ${this.localWebhook.events}`);
this.localWebhook.webhook_by_events = data?.webhook_by_events; this.localWebhook.webhook_by_events = data?.webhook_by_events;
this.logger.verbose(`Webhook by events: ${this.localWebhook.webhook_by_events}`);
this.logger.verbose('Webhook loaded');
} }
public async setWebhook(data: WebhookRaw) { public async setWebhook(data: WebhookRaw) {
this.logger.verbose('Setting webhook');
await this.repository.webhook.create(data, this.instanceName); await this.repository.webhook.create(data, this.instanceName);
this.logger.verbose(`Webhook url: ${data.url}`);
this.logger.verbose(`Webhook events: ${data.events}`);
Object.assign(this.localWebhook, data); Object.assign(this.localWebhook, data);
this.logger.verbose('Webhook set');
} }
public async findWebhook() { public async findWebhook() {
return await this.repository.webhook.find(this.instanceName); this.logger.verbose('Finding webhook');
const data = await this.repository.webhook.find(this.instanceName);
if (!data) {
this.logger.verbose('Webhook not found');
throw new NotFoundException('Webhook not found');
}
this.logger.verbose(`Webhook url: ${data.url}`);
this.logger.verbose(`Webhook events: ${data.events}`);
return data;
} }
public async sendDataWebhook<T = any>(event: Events, data: T, local = true) { public async sendDataWebhook<T = any>(event: Events, data: T, local = true) {
@ -474,6 +511,13 @@ export class WAStartupService {
this.instance.wuid, this.instance.wuid,
)}/*.json`, )}/*.json`,
); );
this.logger.verbose(
`Cleaned ${join(
this.storePath,
key.toLowerCase().replace('_', '-'),
this.instance.wuid,
)}/*.json`,
);
} }
} }
} catch (error) {} } catch (error) {}
@ -1189,12 +1233,13 @@ export class WAStartupService {
private async convertToWebP(image: string) { private async convertToWebP(image: string) {
try { try {
let imagePath: string; let imagePath: string;
const outputPath = `${join(process.cwd(), 'temp', 'sticker.webp')}`; const timestamp = new Date().getTime();
const outputPath = `${join(process.cwd(), 'temp', `${timestamp}.webp`)}`;
if (isBase64(image)) { if (isBase64(image)) {
const base64Data = image.replace(/^data:image\/(jpeg|png|gif);base64,/, ''); const base64Data = image.replace(/^data:image\/(jpeg|png|gif);base64,/, '');
const imageBuffer = Buffer.from(base64Data, 'base64'); const imageBuffer = Buffer.from(base64Data, 'base64');
imagePath = `${join(process.cwd(), 'temp', 'temp-sticker.png')}`; imagePath = `${join(process.cwd(), 'temp', `temp-${timestamp}.png`)}`;
await sharp(imageBuffer).toFile(imagePath); await sharp(imageBuffer).toFile(imagePath);
} else { } else {
const timestamp = new Date().getTime(); const timestamp = new Date().getTime();
@ -1202,7 +1247,7 @@ export class WAStartupService {
const response = await axios.get(url, { responseType: 'arraybuffer' }); const response = await axios.get(url, { responseType: 'arraybuffer' });
const imageBuffer = Buffer.from(response.data, 'binary'); const imageBuffer = Buffer.from(response.data, 'binary');
imagePath = `${join(process.cwd(), 'temp', 'temp-sticker.png')}`; imagePath = `${join(process.cwd(), 'temp', `temp-${timestamp}.png`)}`;
await sharp(imageBuffer).toFile(imagePath); await sharp(imageBuffer).toFile(imagePath);
} }