onirii
Version:
Universal queue SDK
247 lines (246 loc) • 9.89 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RabbitManagerService = void 0;
const tslib_1 = require("tslib");
const log_factory_1 = require("../../factory/log-factory");
const api_request_util_1 = require("../../util/api-request-util");
const rabbit_manager_response_util_1 = require("../../util/api-response/rabbit-manager-response-util");
const env_loader_util_1 = require("../../util/env-loader-util");
class RabbitManagerService {
/**
* Constructor
*
* @param {string} name identify name
* @param {string} managerApi manager api
* @param {string} auth manager auth
*/
constructor(name, managerApi, auth) {
this.instanceName = name;
this.logger = log_factory_1.LogFactory.create(`${this.instanceName}-manager-service`);
this.managerApi = managerApi;
if (!this.managerApi) {
this.managerApi = env_loader_util_1.EnvLoaderUtil.getInstance().getPublicConfig().managerApiUrl;
}
let currentAuth = auth;
if (!currentAuth) {
currentAuth = env_loader_util_1.EnvLoaderUtil.getInstance().getPublicConfig().managerAuth;
}
if (!this.managerApi || !currentAuth) {
throw new Error('Must Specific Manager ApiUrl And Auth, You Can Defined At Env Or Params');
}
this.apiRequester = new api_request_util_1.ApiRequestUtil(new rabbit_manager_response_util_1.RabbitManagerResponseUtil(this.logger), {
'Authorization': `Basic ${Buffer.from(currentAuth).toString('base64')}`,
});
}
/**
* Check target manager service status, this method is not a necessary init method just for check
*
* @return {Promise<void>}
*/
ready() {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
return (yield this.checkAlarms()) !== false;
});
}
/**
* Get all system alarms
*
* @return {Promise<any>} if cant connect to server specific return [false]
*/
checkAlarms() {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
return this.apiRequester.getRequest(`${this.managerApi}/health/checks/alarms`);
});
}
/**
* Check Certificate
*
* @param {number} leftTime left expire time
* @param {"days" | "weeks" | "months" | "years"} unit time unit
* @return {Promise<any>} if cant connect to server specific return [false]
*/
checkCertificate(leftTime, unit) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
return this.apiRequester.getRequest(`${this.managerApi}/health/checks/certificate-expiration/${leftTime}/${unit}`);
});
}
/**
* Check all local alarms
*
* @return {Promise<any>} if cant connect to server specific return [false]
*/
checkLocalAlarms() {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
return this.apiRequester.getRequest(`${this.managerApi}/health/checks/local-alarms`);
});
}
/**
* Check node mirror sync status
*
* @return {Promise<HealthCheckBaseRs | boolean>} if cant connect to server specific return [false]
*/
checkNodeMirrorSyncCritical() {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
return this.apiRequester.getRequest(`${this.managerApi}/health/checks/node-is-mirror-sync-critical`);
});
}
/**
* Check node quorum status
*
* @return {Promise<HealthCheckBaseRs | boolean>} if cant connect to server specific return [false]
*/
checkNodeQuorumCritical() {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
return this.apiRequester.getRequest(`${this.managerApi}/health/checks/node-is-quorum-critical`);
});
}
/**
* Check port health
*
* @param {number} port target port
* @return {Promise<HealthCheckPortRs | boolean>} if cant connect to server specific return [false]
*/
checkPortHealth(port) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
return this.apiRequester.getRequest(`${this.managerApi}/health/checks/port-listener/${port}`);
});
}
/**
* check server protocol status
*
* @param {"amqp091" | "amqp10" | "mqtt" | "stomp" | "web-mqtt" | "web-stomp"} name target protocol
* @return {Promise<HealthCheckProtocolRs | boolean>} if cant connect to server specific return [false]
*/
checkProtocol(name) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
return this.apiRequester.getRequest(`${this.managerApi}/health/checks/protocol-listener/${name}`);
});
}
/**
* Check all vhost status
*
* @return {Promise<any>} if cant connect to server specific return [false]
*/
checkVirtualHosts() {
return this.apiRequester.getRequest(`${this.managerApi}/health/checks/virtual-hosts`);
}
/**
* Create a new vhost
*
* @param {string} name vhost name
* @param {CreateVhostParams} params create options
* @return {Promise<boolean>}
*/
createVhost(name, params) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
const rs = yield this.apiRequester.putRequest(`${this.managerApi}/vhosts/${name}`, params);
if (!rs) {
this.logger.warn(`Target VHost Already Exist:${name}`);
}
return rs;
});
}
/**
* Delete vhost
*
* Note::If checkImmediately = false this method will always return true
*
* @param {string} name target vhost name
* @param {boolean} checkImmediately check deleted?
* @param {CreateVhostParams} params delete options
* @return {Promise<boolean>}
*/
deleteVhost(name, checkImmediately = false, params) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
const rs = yield this.apiRequester.deleteRequest(`${this.managerApi}/vhosts/${name}`, params);
if (checkImmediately) {
return (yield this.getVhost(name)) === false;
}
return rs;
});
}
getVhost(name) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
const currentName = name ? `/${name}` : '';
return this.apiRequester.getRequest(`${this.managerApi}/vhosts${currentName}`);
});
}
/**
* Get vhost all connection list
*
* @param {string} name vhost name
* @return {Promise<RMConnect.VhostConnectionsStatus[] | boolean>} if cant connect to server or vhost not exist specific return [false]
*/
getVhostConnection(name) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
return this.apiRequester.getRequest(`${this.managerApi}/vhosts/${name}/connections`);
});
}
/**
* Get vhost all opening channel list
*
* @param {string} name vhost name
* @param {RabbitManager.PaginationParams} options paginationParams
* @return {Promise<RMChannel.VhostChannelStatus[] | boolean>} if cant connect to server or vhost not exist specific return [false]
*/
getVhostOpenChannels(name, options) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
return this.apiRequester.getRequest(`${this.managerApi}/vhosts/${name}/channels`, options);
});
}
/**
* Start rabbit node
* @param {string} vhostName target vhost name
* @param {string} nodeName target node name
* @return {Promise<any>}
*/
startNode(vhostName, nodeName) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
return this.apiRequester.postRequest(`${this.managerApi}/vhosts/${vhostName}/start/${nodeName}`);
});
}
/**
* Create exchange
*
* @param {string} vhost target vhost
* @param {string} name new exchange name
* @param {RMExchange.CreateOptions} options create exchange options
* @return {Promise<boolean>} if already exist return false and print warn
*/
createExchange(vhost, name, options) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
return this.apiRequester.putRequest(`${this.managerApi}/exchanges/${vhost}/${name}`, options);
});
}
/**
* Delete Exchange
*
* Note:: If checkImmediately = false this method will always return true
*
* @param {string} vhost target vhost name
* @param {string} name target exchange name
* @param {boolean} checkImmediately check deleted?
* @param {RMExchange.DeleteOptions} options delete options
* @return {Promise<boolean>}
*/
deleteExchange(vhost, name, checkImmediately = false, options) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
const rs = yield this.apiRequester.deleteRequest(`${this.managerApi}/exchanges/${vhost}/${name}`, options);
if (checkImmediately) {
return (yield this.getExchange(vhost, name)) === false;
}
return rs;
});
}
getExchange(vhost, name) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
if (!vhost && name) {
throw new Error('Must Specific Vhost When Getting Current Exchange');
}
const currentVhost = vhost ? `/${vhost}` : '';
const currentName = name ? `/${name}` : '';
return this.apiRequester.getRequest(`${this.managerApi}/exchanges${currentVhost}${currentName}`);
});
}
}
exports.RabbitManagerService = RabbitManagerService;