mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-26 18:38:39 -06:00
Update monitor.service.ts
This commit is contained in:
parent
269c9e4b55
commit
ce358e91f0
@ -7,6 +7,7 @@ import { join } from 'path';
|
|||||||
import { Auth, ConfigService, Database, DelInstance, HttpServer, Redis } from '../../config/env.config';
|
import { Auth, ConfigService, Database, DelInstance, HttpServer, Redis } from '../../config/env.config';
|
||||||
import { Logger } from '../../config/logger.config';
|
import { Logger } from '../../config/logger.config';
|
||||||
import { INSTANCE_DIR, STORE_DIR } from '../../config/path.config';
|
import { INSTANCE_DIR, STORE_DIR } from '../../config/path.config';
|
||||||
|
import { NotFoundException } from '../../exceptions';
|
||||||
import { dbserver } from '../../libs/db.connect';
|
import { dbserver } from '../../libs/db.connect';
|
||||||
import { RedisCache } from '../../libs/redis.client';
|
import { RedisCache } from '../../libs/redis.client';
|
||||||
import {
|
import {
|
||||||
@ -75,57 +76,77 @@ export class WAMonitoringService {
|
|||||||
|
|
||||||
public async instanceInfo(instanceName?: string) {
|
public async instanceInfo(instanceName?: string) {
|
||||||
this.logger.verbose('get instance info');
|
this.logger.verbose('get instance info');
|
||||||
|
if (instanceName && !this.waInstances[instanceName]) {
|
||||||
|
throw new NotFoundException(`Instance "${instanceName}" not found`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const instances: any[] = [];
|
||||||
|
|
||||||
|
for await (const [key, value] of Object.entries(this.waInstances)) {
|
||||||
|
if (value) {
|
||||||
|
this.logger.verbose('get instance info: ' + key);
|
||||||
|
let chatwoot: any;
|
||||||
|
|
||||||
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
||||||
|
|
||||||
const instances: any[] = await Promise.all(
|
const findChatwoot = await this.waInstances[key].findChatwoot();
|
||||||
Object.entries(this.waInstances).map(async ([key, value]) => {
|
|
||||||
const status = value?.connectionStatus?.state || 'unknown';
|
|
||||||
|
|
||||||
if (status === 'unknown') {
|
if (findChatwoot && findChatwoot.enabled) {
|
||||||
return null;
|
chatwoot = {
|
||||||
|
...findChatwoot,
|
||||||
|
webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status === 'open') {
|
if (value.connectionStatus.state === 'open') {
|
||||||
this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
|
this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
|
||||||
}
|
|
||||||
|
|
||||||
const instanceData: any = {
|
const instanceData = {
|
||||||
instance: {
|
instance: {
|
||||||
instanceName: key,
|
instanceName: key,
|
||||||
owner: value.wuid,
|
owner: value.wuid,
|
||||||
profileName: (await value.getProfileName()) || 'not loaded',
|
profileName: (await value.getProfileName()) || 'not loaded',
|
||||||
profilePictureUrl: value.profilePictureUrl,
|
profilePictureUrl: value.profilePictureUrl,
|
||||||
profileStatus: (await value.getProfileStatus()) || '',
|
profileStatus: (await value.getProfileStatus()) || '',
|
||||||
status: status,
|
status: value.connectionStatus.state,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
|
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
|
||||||
instanceData.instance.serverUrl = urlServer;
|
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;
|
||||||
instanceData.instance.apikey = (await this.repository.auth.find(key))?.apikey;
|
|
||||||
|
|
||||||
const findChatwoot = await this.waInstances[key].findChatwoot();
|
instanceData.instance['apikey'] = (await this.repository.auth.find(key)).apikey;
|
||||||
if (findChatwoot && findChatwoot.enabled) {
|
|
||||||
instanceData.instance.chatwoot = {
|
instanceData.instance['chatwoot'] = chatwoot;
|
||||||
...findChatwoot,
|
}
|
||||||
webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`,
|
|
||||||
|
instances.push(instanceData);
|
||||||
|
} else {
|
||||||
|
this.logger.verbose('instance: ' + key + ' - connectionStatus: ' + value.connectionStatus.state);
|
||||||
|
|
||||||
|
const instanceData = {
|
||||||
|
instance: {
|
||||||
|
instanceName: key,
|
||||||
|
status: value.connectionStatus.state,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
|
||||||
|
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;
|
||||||
|
|
||||||
|
instanceData.instance['apikey'] = (await this.repository.auth.find(key)).apikey;
|
||||||
|
|
||||||
|
instanceData.instance['chatwoot'] = chatwoot;
|
||||||
}
|
}
|
||||||
|
|
||||||
return instanceData;
|
instances.push(instanceData);
|
||||||
}),
|
}
|
||||||
).then((results) => results.filter((instance) => instance !== null));
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.logger.verbose('return instance info: ' + instances.length);
|
this.logger.verbose('return instance info: ' + instances.length);
|
||||||
|
|
||||||
if (instanceName) {
|
return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
|
||||||
const instance = instances.find((i) => i.instance.instanceName === instanceName);
|
|
||||||
return instance || [];
|
|
||||||
}
|
|
||||||
|
|
||||||
return instances;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private delInstanceFiles() {
|
private delInstanceFiles() {
|
||||||
@ -140,7 +161,8 @@ export class WAMonitoringService {
|
|||||||
});
|
});
|
||||||
this.logger.verbose('instance files deleted: ' + name);
|
this.logger.verbose('instance files deleted: ' + name);
|
||||||
});
|
});
|
||||||
} else if (!this.redis.ENABLED) {
|
// } else if (this.redis.ENABLED) {
|
||||||
|
} else {
|
||||||
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });
|
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });
|
||||||
for await (const dirent of dir) {
|
for await (const dirent of dir) {
|
||||||
if (dirent.isDirectory()) {
|
if (dirent.isDirectory()) {
|
||||||
@ -198,11 +220,6 @@ export class WAMonitoringService {
|
|||||||
execSync(`rm -rf ${join(STORE_DIR, 'auth', 'apikey', instanceName + '.json')}`);
|
execSync(`rm -rf ${join(STORE_DIR, 'auth', 'apikey', instanceName + '.json')}`);
|
||||||
execSync(`rm -rf ${join(STORE_DIR, 'webhook', instanceName + '.json')}`);
|
execSync(`rm -rf ${join(STORE_DIR, 'webhook', instanceName + '.json')}`);
|
||||||
execSync(`rm -rf ${join(STORE_DIR, 'chatwoot', instanceName + '*')}`);
|
execSync(`rm -rf ${join(STORE_DIR, 'chatwoot', instanceName + '*')}`);
|
||||||
execSync(`rm -rf ${join(STORE_DIR, 'chamaai', instanceName + '*')}`);
|
|
||||||
execSync(`rm -rf ${join(STORE_DIR, 'proxy', instanceName + '*')}`);
|
|
||||||
execSync(`rm -rf ${join(STORE_DIR, 'rabbitmq', instanceName + '*')}`);
|
|
||||||
execSync(`rm -rf ${join(STORE_DIR, 'typebot', instanceName + '*')}`);
|
|
||||||
execSync(`rm -rf ${join(STORE_DIR, 'websocket', instanceName + '*')}`);
|
|
||||||
execSync(`rm -rf ${join(STORE_DIR, 'settings', instanceName + '*')}`);
|
execSync(`rm -rf ${join(STORE_DIR, 'settings', instanceName + '*')}`);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@ -223,83 +240,66 @@ export class WAMonitoringService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async loadInstance() {
|
public async loadInstance() {
|
||||||
this.logger.verbose('Loading instances');
|
this.logger.verbose('load instances');
|
||||||
|
const set = async (name: string) => {
|
||||||
try {
|
|
||||||
if (this.redis.ENABLED) {
|
|
||||||
await this.loadInstancesFromRedis();
|
|
||||||
} else if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
|
|
||||||
await this.loadInstancesFromDatabase();
|
|
||||||
} else {
|
|
||||||
await this.loadInstancesFromFiles();
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
this.logger.error(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async setInstance(name: string) {
|
|
||||||
const instance = new WAStartupService(this.configService, this.eventEmitter, this.repository, this.cache);
|
const instance = new WAStartupService(this.configService, this.eventEmitter, this.repository, this.cache);
|
||||||
instance.instanceName = name;
|
instance.instanceName = name;
|
||||||
this.logger.verbose('Instance loaded: ' + name);
|
this.logger.verbose('instance loaded: ' + name);
|
||||||
|
|
||||||
await instance.connectToWhatsapp();
|
await instance.connectToWhatsapp();
|
||||||
this.logger.verbose('connectToWhatsapp: ' + name);
|
this.logger.verbose('connectToWhatsapp: ' + name);
|
||||||
|
|
||||||
this.waInstances[name] = instance;
|
this.waInstances[name] = instance;
|
||||||
}
|
};
|
||||||
|
|
||||||
private async loadInstancesFromRedis() {
|
try {
|
||||||
this.logger.verbose('Redis enabled');
|
if (this.redis.ENABLED) {
|
||||||
|
this.logger.verbose('redis enabled');
|
||||||
await this.cache.connect(this.redis as Redis);
|
await this.cache.connect(this.redis as Redis);
|
||||||
const keys = await this.cache.instanceKeys();
|
const keys = await this.cache.instanceKeys();
|
||||||
|
|
||||||
if (keys?.length > 0) {
|
if (keys?.length > 0) {
|
||||||
this.logger.verbose('Reading instance keys and setting instances');
|
this.logger.verbose('reading instance keys and setting instances');
|
||||||
await Promise.all(keys.map((k) => this.setInstance(k.split(':')[1])));
|
keys.forEach(async (k) => await set(k.split(':')[1]));
|
||||||
} else {
|
} else {
|
||||||
this.logger.verbose('No instance keys found');
|
this.logger.verbose('no instance keys found');
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async loadInstancesFromDatabase() {
|
if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
|
||||||
this.logger.verbose('Database enabled');
|
this.logger.verbose('database enabled');
|
||||||
await this.repository.dbServer.connect();
|
await this.repository.dbServer.connect();
|
||||||
const collections: any[] = await this.dbInstance.collections();
|
const collections: any[] = await this.dbInstance.collections();
|
||||||
|
|
||||||
if (collections.length > 0) {
|
if (collections.length > 0) {
|
||||||
this.logger.verbose('Reading collections and setting instances');
|
this.logger.verbose('reading collections and setting instances');
|
||||||
await Promise.all(collections.map((coll) => this.setInstance(coll.namespace.replace(/^[\w-]+\./, ''))));
|
collections.forEach(async (coll) => await set(coll.namespace.replace(/^[\w-]+\./, '')));
|
||||||
} else {
|
} else {
|
||||||
this.logger.verbose('No collections found');
|
this.logger.verbose('no collections found');
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async loadInstancesFromFiles() {
|
this.logger.verbose('store in files enabled');
|
||||||
this.logger.verbose('Store in files enabled');
|
|
||||||
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });
|
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });
|
||||||
const instanceDirs = [];
|
|
||||||
|
|
||||||
for await (const dirent of dir) {
|
for await (const dirent of dir) {
|
||||||
if (dirent.isDirectory()) {
|
if (dirent.isDirectory()) {
|
||||||
instanceDirs.push(dirent.name);
|
this.logger.verbose('reading instance files and setting instances');
|
||||||
} else {
|
const files = readdirSync(join(INSTANCE_DIR, dirent.name), {
|
||||||
this.logger.verbose('No instance files found');
|
encoding: 'utf-8',
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
||||||
await Promise.all(
|
|
||||||
instanceDirs.map(async (instanceName) => {
|
|
||||||
this.logger.verbose('Reading instance files and setting instances: ' + instanceName);
|
|
||||||
const files = readdirSync(join(INSTANCE_DIR, instanceName), { encoding: 'utf-8' });
|
|
||||||
|
|
||||||
if (files.length === 0) {
|
if (files.length === 0) {
|
||||||
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
|
rmSync(join(INSTANCE_DIR, dirent.name), { recursive: true, force: true });
|
||||||
} else {
|
break;
|
||||||
await this.setInstance(instanceName);
|
}
|
||||||
|
|
||||||
|
await set(dirent.name);
|
||||||
|
} else {
|
||||||
|
this.logger.verbose('no instance files found');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(error);
|
||||||
}
|
}
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private removeInstance() {
|
private removeInstance() {
|
||||||
|
Loading…
Reference in New Issue
Block a user