Add more debug statements to broker
Upgrade packages
This commit is contained in:
parent
d781458cee
commit
0dac59b426
1
.eslintignore
Normal file
1
.eslintignore
Normal file
@ -0,0 +1 @@
|
||||
tests
|
@ -12,11 +12,19 @@
|
||||
"plugins": [ "@typescript-eslint" ],
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2022,
|
||||
"sourceType": "module"
|
||||
"sourceType": "module",
|
||||
"project": "./tsconfig.json"
|
||||
},
|
||||
"rules": {
|
||||
"@typescript-eslint/no-unused-vars": "off",
|
||||
"@typescript-eslint/no-non-null-assertion":"off",
|
||||
"@typescript-eslint/no-non-null-assertion": "off",
|
||||
"@typescript-eslint/no-misused-promises": ["error", {
|
||||
"checksVoidReturn": {
|
||||
"arguments": false
|
||||
}
|
||||
}],
|
||||
"@typescript-eslint/no-shadow": "error",
|
||||
"@typescript-eslint/no-use-before-define": "error",
|
||||
"accessor-pairs": "warn",
|
||||
"array-callback-return": "warn",
|
||||
"array-bracket-newline": [
|
||||
@ -74,7 +82,7 @@
|
||||
"warn",
|
||||
"never"
|
||||
],
|
||||
"eqeqeq": "warn",
|
||||
"eqeqeq": "error",
|
||||
"func-call-spacing": "warn",
|
||||
"func-name-matching": "warn",
|
||||
"func-names": "warn",
|
||||
@ -87,18 +95,38 @@
|
||||
"id-blacklist": "warn",
|
||||
"id-match": "warn",
|
||||
"implicit-arrow-linebreak": "warn",
|
||||
"indent": "warn",
|
||||
"init-declarations": "warn",
|
||||
"quotes": ["error" , "single"],
|
||||
"jsx-quotes": [
|
||||
"indent": [
|
||||
"warn",
|
||||
4,
|
||||
{
|
||||
"SwitchCase": 1,
|
||||
"VariableDeclarator": "first",
|
||||
"FunctionDeclaration": {
|
||||
"parameters": "first"
|
||||
},
|
||||
"CallExpression": {
|
||||
"arguments": "first"
|
||||
},
|
||||
"ArrayExpression": "first",
|
||||
"ObjectExpression": "first",
|
||||
"ImportDeclaration": "first"
|
||||
}
|
||||
],
|
||||
"init-declarations": "warn",
|
||||
"quotes": [
|
||||
"error",
|
||||
"single"
|
||||
],
|
||||
"jsx-quotes": [
|
||||
"error",
|
||||
"prefer-single"
|
||||
],
|
||||
"key-spacing": [
|
||||
"warn",
|
||||
{
|
||||
"beforeColon": false,
|
||||
"afterColon": true
|
||||
"afterColon": true,
|
||||
"align": "value"
|
||||
}
|
||||
],
|
||||
"keyword-spacing": [
|
||||
@ -128,7 +156,7 @@
|
||||
],
|
||||
"max-lines-per-function": [
|
||||
"warn",
|
||||
100
|
||||
140
|
||||
],
|
||||
"max-depth": [
|
||||
"warn",
|
||||
@ -137,16 +165,16 @@
|
||||
"new-parens": "warn",
|
||||
"no-alert": "warn",
|
||||
"no-array-constructor": "warn",
|
||||
// "no-bitwise": "warn",
|
||||
"no-buffer-constructor": "warn",
|
||||
"no-caller": "warn",
|
||||
"no-console": "warn",
|
||||
"no-constant-binary-expression": "error",
|
||||
"no-div-regex": "warn",
|
||||
"no-dupe-else-if": "warn",
|
||||
"no-duplicate-imports": "warn",
|
||||
"no-else-return": "warn",
|
||||
"no-empty-function": "warn",
|
||||
"no-eq-null": "warn",
|
||||
"no-eq-null": "off",
|
||||
"no-eval": "warn",
|
||||
"no-extend-native": "warn",
|
||||
"no-extra-bind": "warn",
|
||||
@ -164,7 +192,15 @@
|
||||
"no-loop-func": "warn",
|
||||
"no-mixed-requires": "warn",
|
||||
"no-multi-assign": "warn",
|
||||
"no-multi-spaces": "warn",
|
||||
"no-multi-spaces": [
|
||||
"warn",
|
||||
{
|
||||
"ignoreEOLComments": true,
|
||||
"exceptions": {
|
||||
"ImportDeclaration": true
|
||||
}
|
||||
}
|
||||
],
|
||||
"no-multi-str": "warn",
|
||||
"no-multiple-empty-lines": "warn",
|
||||
"no-native-reassign": "warn",
|
||||
@ -185,23 +221,25 @@
|
||||
"no-restricted-modules": "warn",
|
||||
"no-restricted-properties": "warn",
|
||||
"no-restricted-syntax": "warn",
|
||||
"no-return-assign": "warn",
|
||||
"no-return-assign": [
|
||||
"warn",
|
||||
"except-parens"
|
||||
],
|
||||
"no-return-await": "warn",
|
||||
"no-script-url": "warn",
|
||||
"no-self-compare": "warn",
|
||||
"no-sequences": "warn",
|
||||
"no-setter-return": "warn",
|
||||
"no-spaced-func": "warn",
|
||||
"@typescript-eslint/no-shadow": "error",
|
||||
"no-tabs": "warn",
|
||||
"no-tabs": "error",
|
||||
"no-template-curly-in-string": "error",
|
||||
"no-throw-literal": "warn",
|
||||
"no-trailing-spaces": "warn",
|
||||
"no-undef-init": "error",
|
||||
"no-undefined": "error",
|
||||
"no-unmodified-loop-condition": "warn",
|
||||
"no-unneeded-ternary": "error",
|
||||
"no-unused-expressions": "warn",
|
||||
"@typescript-eslint/no-use-before-define": "error",
|
||||
"no-useless-call": "warn",
|
||||
"no-useless-computed-key": "warn",
|
||||
"no-useless-concat": "warn",
|
||||
@ -209,7 +247,6 @@
|
||||
"no-useless-rename": "warn",
|
||||
"no-useless-return": "warn",
|
||||
"no-var": "warn",
|
||||
// "no-void": "warn",
|
||||
"no-whitespace-before-property": "error",
|
||||
"nonblock-statement-body-position": [
|
||||
"warn",
|
||||
@ -293,6 +330,16 @@
|
||||
"yoda": [
|
||||
"warn",
|
||||
"never"
|
||||
],
|
||||
"no-warning-comments": [
|
||||
1,
|
||||
{
|
||||
"terms": [
|
||||
"todo",
|
||||
"fixme"
|
||||
],
|
||||
"location": "anywhere"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
893
.yarn/releases/yarn-4.1.1.cjs
vendored
893
.yarn/releases/yarn-4.1.1.cjs
vendored
File diff suppressed because one or more lines are too long
925
.yarn/releases/yarn-4.5.0.cjs
vendored
Executable file
925
.yarn/releases/yarn-4.5.0.cjs
vendored
Executable file
File diff suppressed because one or more lines are too long
@ -1,5 +1,5 @@
|
||||
nodeLinker: node-modules
|
||||
|
||||
yarnPath: .yarn/releases/yarn-4.1.1.cjs
|
||||
npmRegistryServer: "https://registry.corgi.wtf"
|
||||
|
||||
npmRegistryServer: "https://registry.corgi.wtf"
|
||||
yarnPath: .yarn/releases/yarn-4.5.0.cjs
|
||||
|
@ -54,5 +54,5 @@
|
||||
"optional": true
|
||||
}
|
||||
},
|
||||
"packageManager": "yarn@4.1.1"
|
||||
"packageManager": "yarn@4.5.0"
|
||||
}
|
||||
|
166
src/MariaDB.ts
166
src/MariaDB.ts
@ -6,7 +6,7 @@ import { ILogger, IServer } from './interfaces/index.js';
|
||||
|
||||
const SAFE_TO_RETRY = [ 'ER_LOCK_DEADLOCK', 'PROTOCOL_CONNECTION_LOST' ];
|
||||
|
||||
type Credentials = {
|
||||
interface Credentials {
|
||||
user: string,
|
||||
password: string,
|
||||
host: string,
|
||||
@ -18,7 +18,7 @@ type ExtendedCredentials = {
|
||||
node: string
|
||||
} & Credentials
|
||||
|
||||
export type MariaOptions = {
|
||||
export interface MariaOptions {
|
||||
load?: boolean,
|
||||
cluster?: PoolClusterConfig,
|
||||
client?: PoolConfig,
|
||||
@ -40,7 +40,7 @@ type MariaError = {
|
||||
|
||||
type Value = (string | number | null | boolean | object)
|
||||
type Values = (Value | Value[] | Value[][])[];
|
||||
type QueryOptions = {
|
||||
interface QueryOptions {
|
||||
node?: string,
|
||||
timeout?: number,
|
||||
errorIfNodeUnavailable?: boolean
|
||||
@ -54,14 +54,14 @@ type StatusString =
|
||||
| 'donor'
|
||||
| 'joiner'
|
||||
| 'joined'
|
||||
type Node = {
|
||||
interface Node {
|
||||
name: string,
|
||||
host: string,
|
||||
uuid?: string,
|
||||
status?: StatusString
|
||||
}
|
||||
|
||||
type StatusUpdate = {
|
||||
interface StatusUpdate {
|
||||
status: StatusString,
|
||||
uuid: string,
|
||||
primary: string,
|
||||
@ -69,7 +69,7 @@ type StatusUpdate = {
|
||||
members: string
|
||||
}
|
||||
|
||||
const isOkPacket = (obj: object): obj is OkPacket =>
|
||||
const isOkPacket = (obj: object): obj is OkPacket =>
|
||||
{
|
||||
if ('fieldCount' in obj
|
||||
&& 'affectedRows' in obj
|
||||
@ -82,7 +82,7 @@ const isOkPacket = (obj: object): obj is OkPacket =>
|
||||
// Designed to work in a single instance or cluster (galera) configuration
|
||||
// Will need some work for primary/replica configuration (e.g. not treating all nodes as read-write nodes)
|
||||
|
||||
class MariaDB
|
||||
class MariaDB
|
||||
{
|
||||
|
||||
#_activeQueries: number;
|
||||
@ -113,7 +113,7 @@ class MariaDB
|
||||
const hosts = host.split(',');
|
||||
this.#credentials = [];
|
||||
this.#nodes = [];
|
||||
for (const remote of hosts)
|
||||
for (const remote of hosts)
|
||||
{
|
||||
const [ node ] = remote.split('.');
|
||||
if (typeof node === 'undefined' || !(/(([a-zA-Z]\w*[0-9]?)|([0-9]?\w*[a-zA-Z]))/u).test(node))
|
||||
@ -135,48 +135,48 @@ class MariaDB
|
||||
this.#server = server;
|
||||
}
|
||||
|
||||
get ready ()
|
||||
get ready ()
|
||||
{
|
||||
return this.#_ready;
|
||||
}
|
||||
|
||||
get activeQueries ()
|
||||
get activeQueries ()
|
||||
{
|
||||
return this.#_activeQueries;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line max-lines-per-function
|
||||
async init ()
|
||||
|
||||
async init ()
|
||||
{
|
||||
if (!this.#load)
|
||||
if (!this.#load)
|
||||
return this.#logger?.info('Not loading MariaDB');
|
||||
|
||||
this.#logger?.status(`Creating${this.#cluster ? ' cluster' : ''} connection pool`);
|
||||
this.#pool = mysql.createPoolCluster(this.#config.cluster);
|
||||
for (const creds of this.#credentials)
|
||||
for (const creds of this.#credentials)
|
||||
{
|
||||
const name = creds.node;
|
||||
this.#pool.add(name, { ...this.#config.client, ...creds });
|
||||
this.#logger?.info(`Added node ${name} to pool cluster`);
|
||||
}
|
||||
|
||||
this.#pool.on('connection', (connection) =>
|
||||
this.#pool.on('connection', (connection) =>
|
||||
{
|
||||
this.#logger?.debug(`New connection: ${connection?.threadId || null}`);
|
||||
});
|
||||
|
||||
this.#pool.on('acquire', (connection) =>
|
||||
this.#pool.on('acquire', (connection) =>
|
||||
{
|
||||
this.#_activeQueries++;
|
||||
this.#logger?.debug(`Connection acquired: ${connection?.threadId || null}`);
|
||||
});
|
||||
|
||||
this.#pool.on('enqueue', () =>
|
||||
this.#pool.on('enqueue', () =>
|
||||
{
|
||||
this.#logger?.debug('Query enqueued for connection');
|
||||
});
|
||||
|
||||
this.#pool.on('release', (connection) =>
|
||||
this.#pool.on('release', (connection) =>
|
||||
{
|
||||
this.#_activeQueries--;
|
||||
|
||||
@ -186,7 +186,7 @@ class MariaDB
|
||||
this.#logger?.debug(`Connection released: ${connection?.threadId || null}`);
|
||||
});
|
||||
|
||||
this.#pool.on('remove', (nodeId) =>
|
||||
this.#pool.on('remove', (nodeId) =>
|
||||
{
|
||||
this.#logger?.status(`Node ${nodeId} was removed from pool`);
|
||||
const index = this.#nodes.findIndex(n => n === nodeId);
|
||||
@ -194,45 +194,49 @@ class MariaDB
|
||||
});
|
||||
|
||||
this.#logger?.info('Testing MariaDB connection');
|
||||
await new Promise<void>((resolve, reject) =>
|
||||
await new Promise<void>((resolve, reject) =>
|
||||
{
|
||||
if (!this.#pool)
|
||||
return reject(new Error('Missing connection pool'));
|
||||
this.#pool.getConnection((err, conn) =>
|
||||
if (!this.#pool)
|
||||
{
|
||||
reject(new Error('Missing connection pool')); return;
|
||||
}
|
||||
this.#pool.getConnection((err, conn) =>
|
||||
{
|
||||
if (err)
|
||||
return reject(err);
|
||||
{
|
||||
reject(err); return;
|
||||
}
|
||||
conn.release();
|
||||
return resolve();
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
this.#logger?.status('Database connected');
|
||||
|
||||
if (this.#cluster)
|
||||
{ // Resolve the UUID for each node to enable status updates
|
||||
this.#logger?.status('Resolving cluster node UUIDs for status updates');
|
||||
for (const node of this.#nodes)
|
||||
for (const node of this.#nodes)
|
||||
{
|
||||
// UUIDs are not always enough, we also need the node's IP for cases where the UUID changes due to restarts
|
||||
const dnsResult = await dns.lookup(node.host, 4);
|
||||
node.host = dnsResult.address;
|
||||
const response = await this.#_query('SHOW STATUS WHERE `Variable_name` = "wsrep_gcomm_uuid" OR `Variable_name` = "wsrep_local_state_comment"', [], { node: node.name }).catch(() => null) as { Variable_name: string, Value: string }[];
|
||||
// Error gives us null
|
||||
if (!response)
|
||||
if (!response)
|
||||
{
|
||||
this.#logger?.info(`Could not resolve UUID for ${node.name}, presumably offline, setting status to disconnected`);
|
||||
node.status = 'disconnected';
|
||||
continue;
|
||||
}
|
||||
// if we for some reason get a response that is empty, we got a problem
|
||||
if (!response.length)
|
||||
if (!response.length)
|
||||
throw new Error(`Failed to acquire UUID for node ${node.name}`);
|
||||
const uuid = response.find(entry => entry.Variable_name === 'wsrep_gcomm_uuid')?.Value;
|
||||
const status = response.find(entry => entry.Variable_name === 'wsrep_local_state_comment')?.Value.toLowerCase();
|
||||
node.uuid = uuid;
|
||||
node.status = status as StatusString;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
@ -246,9 +250,9 @@ class MariaDB
|
||||
{
|
||||
this.#logger?.info('Setting up metric recording');
|
||||
MariaDB.#queryHistogram = new Prometheus.Histogram({
|
||||
name: 'sql_queries',
|
||||
help: 'Tracks query duration in seconds and frequency',
|
||||
buckets: this.#config.metricsBuckets ?? Prometheus?.exponentialBuckets(0.005, 2, 10),
|
||||
name: 'sql_queries',
|
||||
help: 'Tracks query duration in seconds and frequency',
|
||||
buckets: this.#config.metricsBuckets ?? Prometheus?.exponentialBuckets(0.005, 2, 10),
|
||||
labelNames: [ 'type' ] as const
|
||||
});
|
||||
if (this.#server.registerMetric)
|
||||
@ -259,23 +263,25 @@ class MariaDB
|
||||
return this;
|
||||
}
|
||||
|
||||
async close ()
|
||||
async close ()
|
||||
{
|
||||
this.#logger?.status('Shutting down database connections');
|
||||
if (!this.ready)
|
||||
if (!this.ready)
|
||||
return Promise.resolve();
|
||||
this.#_ready = false;
|
||||
if (this.#_activeQueries)
|
||||
if (this.#_activeQueries)
|
||||
{
|
||||
this.#logger?.info(`${this.#_activeQueries} active queries still running, letting them finish`);
|
||||
await this.finishQueries();
|
||||
this.#logger?.info('Queries finished, shutting down');
|
||||
}
|
||||
return new Promise<void>(resolve =>
|
||||
return new Promise<void>(resolve =>
|
||||
{
|
||||
if (!this.#pool)
|
||||
return resolve();
|
||||
this.#pool.end(() =>
|
||||
if (!this.#pool)
|
||||
{
|
||||
resolve(); return;
|
||||
}
|
||||
this.#pool.end(() =>
|
||||
{
|
||||
this.#pool?.removeAllListeners();
|
||||
resolve();
|
||||
@ -284,7 +290,7 @@ class MariaDB
|
||||
});
|
||||
}
|
||||
|
||||
get statusUpdateListener ()
|
||||
get statusUpdateListener ()
|
||||
{
|
||||
return this.#statusUpdate.bind(this);
|
||||
}
|
||||
@ -297,29 +303,29 @@ class MariaDB
|
||||
if (!update.members.length)
|
||||
{
|
||||
if (update.status === 'disconnected')
|
||||
this.#nodes.forEach(node =>
|
||||
this.#nodes.forEach(node =>
|
||||
{
|
||||
if (node.status === 'disconnecting')
|
||||
{
|
||||
const oldStatus = node.status;
|
||||
node.status = 'disconnected';
|
||||
this.#logger?.status(`Cluster node ${node.name} changed status from ${oldStatus} to ${node.status}`, { broadcast: true });
|
||||
node.status = 'disconnected';
|
||||
this.#logger?.status(`Cluster node ${node.name} changed status from ${oldStatus} to ${node.status}`, { broadcast: true });
|
||||
}
|
||||
});
|
||||
else if (update.status === 'synced')
|
||||
else if (update.status === 'synced')
|
||||
{ // Sometimes we'll get a status update without the member info, probably safe to assume if we get a synced status the recently joined members will be synced
|
||||
|
||||
this.#nodes.forEach(node =>
|
||||
this.#nodes.forEach(node =>
|
||||
{
|
||||
if (node.status === 'joined')
|
||||
{
|
||||
const oldStatus = node.status;
|
||||
node.status = 'synced';
|
||||
this.#logger?.status(`Cluster node ${node.name} changed status from ${oldStatus} to ${node.status}`, { broadcast: true });
|
||||
node.status = 'synced';
|
||||
this.#logger?.status(`Cluster node ${node.name} changed status from ${oldStatus} to ${node.status}`, { broadcast: true });
|
||||
}
|
||||
});
|
||||
}
|
||||
return;
|
||||
return;
|
||||
}
|
||||
const members = update.members.split(',');
|
||||
const index = parseInt(update.index);
|
||||
@ -337,29 +343,31 @@ class MariaDB
|
||||
return this.#logger?.warn('Received status update for node that is not present in config', { broadcast: true });
|
||||
node.uuid = id;
|
||||
}
|
||||
|
||||
|
||||
if (node.status !== update.status)
|
||||
{
|
||||
const oldStatus = node.status;
|
||||
node.status = update.status;
|
||||
this.#logger?.status(`Cluster node ${node.name} changed status from ${oldStatus} to ${node.status}`, { broadcast: true });
|
||||
this.#logger?.status(`Cluster node ${node.name} changed status from ${oldStatus} to ${node.status}`, { broadcast: true });
|
||||
}
|
||||
}
|
||||
|
||||
finishQueries ()
|
||||
finishQueries ()
|
||||
{
|
||||
return new Promise<void>(resolve =>
|
||||
return new Promise<void>(resolve =>
|
||||
{
|
||||
this.#afterLastQuery = resolve;
|
||||
});
|
||||
}
|
||||
|
||||
getConnection (nodeName: string | null, throwError: boolean): Promise<PoolConnection>
|
||||
getConnection (nodeName: string | null, throwError: boolean): Promise<PoolConnection>
|
||||
{
|
||||
return new Promise((resolve, reject) =>
|
||||
return new Promise((resolve, reject) =>
|
||||
{
|
||||
if (!this.#pool)
|
||||
return reject(new Error('Pool closed'));
|
||||
if (!this.#pool)
|
||||
{
|
||||
reject(new Error('Pool closed')); return;
|
||||
}
|
||||
// Get node by name
|
||||
const pool = this.#pool;
|
||||
if (nodeName)
|
||||
@ -371,54 +379,56 @@ class MariaDB
|
||||
if (!node)
|
||||
{
|
||||
const str = `Node ${nodeName} is not available in pool, falling back to arbitrary node`;
|
||||
if (throwError)
|
||||
if (throwError)
|
||||
throw new Error(str);
|
||||
this.#logger?.warn(str);
|
||||
nodeName = '*';
|
||||
this.#logger?.warn(str);
|
||||
nodeName = '*';
|
||||
}
|
||||
else if (node.status && node.status !== 'synced' && !(this.#canQueryDonor && node.status === 'donor'))
|
||||
{
|
||||
const str = `Node ${nodeName} is currently not synced with the pool and thus unqueryable`;
|
||||
if (throwError)
|
||||
if (throwError)
|
||||
throw new Error(str);
|
||||
this.#logger?.warn(str);
|
||||
nodeName = '*';
|
||||
nodeName = '*';
|
||||
}
|
||||
}
|
||||
this.#logger?.debug(`Selected node ${nodeName} for query`);
|
||||
return pool.of(nodeName ?? '*').getConnection((err, conn) =>
|
||||
pool.of(nodeName ?? '*').getConnection((err, conn) =>
|
||||
{
|
||||
if (err)
|
||||
return reject(err);
|
||||
return resolve(conn);
|
||||
{
|
||||
reject(err); return;
|
||||
}
|
||||
resolve(conn);
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Retry certain queries that are safe to retry, e.g. deadlock
|
||||
* @throws {MariaError}
|
||||
* @private
|
||||
* @private
|
||||
* */
|
||||
async #_query<T> (query: string, values: Values, { timeout, node, errorIfNodeUnavailable }: QueryOptions = {}, attempts = 0):
|
||||
Promise<T[] | FieldInfo[] | [OkPacket]>
|
||||
Promise<T[] | FieldInfo[] | [OkPacket]>
|
||||
{
|
||||
const connection = await this.getConnection(node ?? null, errorIfNodeUnavailable ?? false);
|
||||
try
|
||||
{
|
||||
const result = await new Promise<T[] | FieldInfo[] | [OkPacket]| undefined>((resolve, reject) =>
|
||||
const result = await new Promise<T[] | FieldInfo[] | [OkPacket]| undefined>((resolve, reject) =>
|
||||
{
|
||||
const endTimer = MariaDB.#queryHistogram?.startTimer({ type: [ 'SELECT', 'UPDATE', 'INSERT', 'DELETE' ].find(entry => query.toUpperCase().includes(entry)) ?? 'OTHER' });
|
||||
const q = connection.query({ timeout, sql: query }, values, (err, results, fields) =>
|
||||
const q = connection.query({ timeout, sql: query }, values, (err, results, fields) =>
|
||||
{
|
||||
if (err)
|
||||
if (err)
|
||||
reject(err);
|
||||
else if (isOkPacket(results))
|
||||
resolve([ results ]);
|
||||
else if (results)
|
||||
resolve(results);
|
||||
else
|
||||
else
|
||||
resolve(fields);
|
||||
connection.release();
|
||||
if (endTimer)
|
||||
@ -428,24 +438,24 @@ class MariaDB
|
||||
});
|
||||
return Promise.resolve(result || []);
|
||||
}
|
||||
catch (err)
|
||||
catch (err)
|
||||
{
|
||||
const error = err as MariaError;
|
||||
// Retry safe errors // (Galera) Instance not ready for query
|
||||
if ((SAFE_TO_RETRY.includes(error.code) || error.errno === 1047) && attempts < 5) //
|
||||
if ((SAFE_TO_RETRY.includes(error.code) || error.errno === 1047) && attempts < 5) //
|
||||
return this.#_query(query, values, { timeout, node }, ++attempts);
|
||||
return Promise.reject(error);
|
||||
}
|
||||
}
|
||||
|
||||
async query<T> (query: string, values?: Values | QueryOptions, opts?: QueryOptions): Promise<T[] | FieldInfo[] | [OkPacket]>
|
||||
async query<T> (query: string, values?: Values | QueryOptions, opts?: QueryOptions): Promise<T[] | FieldInfo[] | [OkPacket]>
|
||||
{
|
||||
if (values && !(values instanceof Array))
|
||||
{
|
||||
opts = values;
|
||||
opts = values;
|
||||
values = [];
|
||||
}
|
||||
|
||||
|
||||
if (!this.ready)
|
||||
return Promise.reject(new Error('MariaDB not ready'));
|
||||
|
||||
@ -460,7 +470,7 @@ class MariaDB
|
||||
|
||||
}
|
||||
|
||||
q<T> (query: string, values?: Values | QueryOptions, opts?: QueryOptions)
|
||||
q<T> (query: string, values?: Values | QueryOptions, opts?: QueryOptions)
|
||||
{
|
||||
return this.query<T>(query, values, opts);
|
||||
}
|
||||
|
@ -2,15 +2,15 @@ import { ILogger, IServer } from './interfaces/index.js';
|
||||
import amqp, { AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager';
|
||||
import { Channel, ConfirmChannel, ConsumeMessage, Options } from 'amqplib';
|
||||
|
||||
type ExchangeDef = {
|
||||
interface ExchangeDef {
|
||||
durable?: boolean,
|
||||
internal?: boolean,
|
||||
autoDelete?: boolean,
|
||||
type?: 'direct' | 'topic' | 'headers' | 'fanout' | 'match' | string,
|
||||
type?: 'direct' | 'topic' | 'headers' | 'fanout' | 'match',
|
||||
arguments?: object
|
||||
}
|
||||
|
||||
type QueueDef = {
|
||||
interface QueueDef {
|
||||
durable?: boolean,
|
||||
messageTtl?: number,
|
||||
exclusive?: boolean,
|
||||
@ -22,22 +22,18 @@ type QueueDef = {
|
||||
maxPriority?: number
|
||||
}
|
||||
|
||||
export type BrokerOptions = {
|
||||
export interface BrokerOptions {
|
||||
load?: boolean,
|
||||
host: string,
|
||||
port: number
|
||||
user: string,
|
||||
pass: string,
|
||||
vhost: string,
|
||||
exchanges?: {
|
||||
[key: string]: ExchangeDef
|
||||
},
|
||||
queues?: {
|
||||
[key: string]: QueueDef
|
||||
}
|
||||
exchanges?: Record<string, ExchangeDef>,
|
||||
queues?: Record<string, QueueDef>
|
||||
}
|
||||
|
||||
type InternalMessage = {
|
||||
interface InternalMessage {
|
||||
properties: object,
|
||||
content: object
|
||||
}
|
||||
@ -54,23 +50,21 @@ type InternalQueueMsg = {
|
||||
type Consumer<T = unknown> = (content: T, msg: ConsumeMessage) => Promise<void> | void
|
||||
type Subscriber<T = unknown> = (content: T, msg: ConsumeMessage) => Promise<void> | void
|
||||
|
||||
export type SubscriptionOptions = {
|
||||
export interface SubscriptionOptions {
|
||||
exchangeType?: 'direct' | 'topic' | 'headers' | 'fanout' | 'match',
|
||||
routingKey?: string
|
||||
}
|
||||
|
||||
class MessageBroker
|
||||
class MessageBroker
|
||||
{
|
||||
|
||||
// Broker definitions
|
||||
#load: boolean;
|
||||
#hosts: string[];
|
||||
#port: number;
|
||||
#username: string;
|
||||
#password: string;
|
||||
#vhost: string;
|
||||
#exchanges: { [key: string]: ExchangeDef };
|
||||
#queues: {[key: string]: QueueDef};
|
||||
#exchanges: Record<string, ExchangeDef>;
|
||||
#queues: Record<string, QueueDef>;
|
||||
|
||||
// Wrapper related
|
||||
#connection: AmqpConnectionManager | null;
|
||||
@ -86,9 +80,8 @@ class MessageBroker
|
||||
#_qQueue: InternalQueueMsg[];
|
||||
#_qTO: NodeJS.Timeout | null;
|
||||
|
||||
constructor (server: IServer, options: BrokerOptions)
|
||||
constructor (server: IServer, options: BrokerOptions)
|
||||
{
|
||||
|
||||
this.#load = options.load ?? true;
|
||||
this.#hosts = options.host.split(',');
|
||||
this.#username = options.user;
|
||||
@ -96,10 +89,10 @@ class MessageBroker
|
||||
this.#vhost = options.vhost || '';
|
||||
this.#port = options.port;
|
||||
|
||||
if (!this.#hosts.length)
|
||||
if (!this.#hosts.length)
|
||||
throw new Error('Missing hosts configuration');
|
||||
|
||||
if (!options.port)
|
||||
if (!options.port)
|
||||
throw new Error('Missing port option');
|
||||
|
||||
this.#exchanges = options.exchanges || {};
|
||||
@ -119,10 +112,9 @@ class MessageBroker
|
||||
this.#_pQueue = [];
|
||||
this.#_qQueue = [];
|
||||
this.#_qTO = null;
|
||||
|
||||
}
|
||||
|
||||
async init ()
|
||||
async init ()
|
||||
{
|
||||
if (!this.#load)
|
||||
return this.#logger?.info('Not loading RabbitMQ');
|
||||
@ -130,30 +122,30 @@ class MessageBroker
|
||||
this.#logger?.info('Initialising message broker');
|
||||
const credentials = this.#username ? `${this.#username}:${this.#password}@` : '';
|
||||
const connectionStrings = this.#hosts.map(host => `amqp://${credentials}${host}:${this.#port}/${this.#vhost}`);
|
||||
this.#connection = await amqp.connect(connectionStrings);
|
||||
this.#connection = amqp.connect(connectionStrings);
|
||||
|
||||
this.#connection.on('disconnect', async ({ err }) =>
|
||||
this.#connection.on('disconnect', async ({ err }) =>
|
||||
{
|
||||
this.#logger?.status(`Disconnected: ${err.message}`);
|
||||
await this.#channel?.close();
|
||||
this.#channel = null;
|
||||
});
|
||||
this.#connection.on('blocked', ({ reason }) =>
|
||||
this.#connection.on('blocked', ({ reason }) =>
|
||||
{
|
||||
this.#logger?.status(`Blocked: ${reason}`);
|
||||
});
|
||||
this.#connection.on('connectFailed', ({ err }) =>
|
||||
this.#connection.on('connectFailed', ({ err }) =>
|
||||
{
|
||||
this.#logger?.error(`Message broker failed to connect: ${err.stack || err.message}`);
|
||||
});
|
||||
this.#connection.on('connect', async ({ url }) =>
|
||||
this.#connection.on('connect', async ({ url }) =>
|
||||
{
|
||||
this.#logger?.status(`Message broker connected to ${url}`);
|
||||
});
|
||||
|
||||
await new Promise<void>((resolve, reject) =>
|
||||
await new Promise<void>((resolve, reject) =>
|
||||
{
|
||||
this.#connection?.once('connect', () =>
|
||||
this.#connection?.once('connect', () =>
|
||||
{
|
||||
this.#connection?.removeListener('connectFailed', reject);
|
||||
resolve();
|
||||
@ -163,48 +155,53 @@ class MessageBroker
|
||||
|
||||
await this.createChannel();
|
||||
this.#connection.on('connect', this.createChannel.bind(this));
|
||||
|
||||
}
|
||||
|
||||
async close ()
|
||||
async close ()
|
||||
{
|
||||
if (this.#channel)
|
||||
this.#logger?.status('Closing broker');
|
||||
if (this.#channel)
|
||||
{
|
||||
await this.#channel.close();
|
||||
this.#logger?.info('Closing channel');
|
||||
this.#channel.removeAllListeners();
|
||||
await this.#channel.close();
|
||||
}
|
||||
if (this.#connection)
|
||||
if (this.#connection)
|
||||
{
|
||||
this.#logger?.info('Closing connection');
|
||||
await this.#connection.close();
|
||||
// No clue why the removeAllListeners function isn't exposed from the connection manager, but it exists since it's an EventEmitter
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
this.#connection.removeAllListeners();
|
||||
}
|
||||
this.#logger?.status('Broker closed');
|
||||
}
|
||||
|
||||
async createChannel ()
|
||||
async createChannel ()
|
||||
{
|
||||
const exchanges = Object.entries(this.#exchanges);
|
||||
const queues = Object.entries(this.#queues);
|
||||
|
||||
if (this.#channel)
|
||||
if (!this.#connection)
|
||||
throw new Error('Broker not connected');
|
||||
|
||||
if (this.#channel)
|
||||
{
|
||||
this.#logger?.debug('Closing old channel');
|
||||
this.#channel.removeAllListeners();
|
||||
await this.#channel.close().catch(err => this.#logger?.error(err.stack));
|
||||
}
|
||||
|
||||
if (!this.#connection)
|
||||
throw new Error();
|
||||
this.#logger?.debug('Creating channel');
|
||||
this.#channel = this.#connection.createChannel({
|
||||
setup: async (channel: Channel | ConfirmChannel) =>
|
||||
setup: async (channel: Channel | ConfirmChannel) =>
|
||||
{
|
||||
for (const [ name, props ] of exchanges)
|
||||
for (const [ name, props ] of exchanges)
|
||||
{
|
||||
await channel.assertExchange(name, props.type ?? 'fanout', props);
|
||||
}
|
||||
for (const [ name, props ] of queues)
|
||||
for (const [ name, props ] of queues)
|
||||
{
|
||||
await channel.assertQueue(name, props);
|
||||
}
|
||||
@ -215,11 +212,13 @@ class MessageBroker
|
||||
this.#channel.on('connect', () => this.#logger?.status('Channel connected'));
|
||||
this.#channel.on('error', (err, info) => this.#logger?.error(`${info.name}: ${err.stack}`));
|
||||
|
||||
await new Promise<void>((resolve, reject) =>
|
||||
await new Promise<void>((resolve, reject) =>
|
||||
{
|
||||
if (!this.#channel)
|
||||
return reject(new Error('Missing channel?'));
|
||||
this.#channel.once('connect', () =>
|
||||
if (!this.#channel)
|
||||
{
|
||||
reject(new Error('Missing channel?')); return;
|
||||
}
|
||||
this.#channel.once('connect', () =>
|
||||
{
|
||||
this.#channel?.removeListener('error', reject);
|
||||
resolve();
|
||||
@ -236,7 +235,7 @@ class MessageBroker
|
||||
/**
|
||||
* Consume queue
|
||||
*/
|
||||
async consume<T = unknown> (queue: string, consumer: Consumer<T>, options?: Options.Consume)
|
||||
async consume<T = unknown> (queue: string, consumer: Consumer<T>, options?: Options.Consume)
|
||||
{
|
||||
if (!this.#channel)
|
||||
throw new Error('Channel does not exist');
|
||||
@ -249,12 +248,13 @@ class MessageBroker
|
||||
this.#consumers.set(queue, list);
|
||||
}
|
||||
|
||||
private async _consume<T> (queue: string, consumer: Consumer<T>, options?: Options.Consume): Promise<void>
|
||||
private async _consume<T> (queue: string, consumer: Consumer<T>, options?: Options.Consume): Promise<void>
|
||||
{
|
||||
if (!this.#channel)
|
||||
if (!this.#channel)
|
||||
return Promise.reject(new Error('Channel doesn\'t exist'));
|
||||
await this.#channel.consume(queue, async (msg: ConsumeMessage) =>
|
||||
await this.#channel.consume(queue, async (msg: ConsumeMessage) =>
|
||||
{
|
||||
this.#logger?.debug(`Consumer for ${queue} (${consumer.name}) fired`);
|
||||
if (msg.content)
|
||||
await consumer(JSON.parse(msg.content.toString()), msg);
|
||||
this.#channel?.ack(msg);
|
||||
@ -264,7 +264,7 @@ class MessageBroker
|
||||
/**
|
||||
* Subscribe to exchange, ensures messages aren't lost by binding it to a queue
|
||||
*/
|
||||
async subscribe<T> (name: string, subscriber: Subscriber<T>, options: SubscriptionOptions = {})
|
||||
async subscribe<T> (name: string, subscriber: Subscriber<T>, options: SubscriptionOptions = {})
|
||||
{
|
||||
if (!this.#channel)
|
||||
throw new Error('Channel does not exist');
|
||||
@ -276,21 +276,21 @@ class MessageBroker
|
||||
const list = this.#subscribers.get(name) ?? [];
|
||||
list.push({ subscriber, options });
|
||||
this.#subscribers.set(name, list);
|
||||
|
||||
}
|
||||
|
||||
private async _subscribe<T> (name: string, listener: Subscriber<T>, options: SubscriptionOptions): Promise<void>
|
||||
private async _subscribe<T> (name: string, listener: Subscriber<T>, options: SubscriptionOptions): Promise<void>
|
||||
{
|
||||
if (!this.#channel)
|
||||
if (!this.#channel)
|
||||
return Promise.reject(new Error('Channel doesn\'t exist'));
|
||||
|
||||
|
||||
const type = options.exchangeType ?? 'fanout';
|
||||
await this.#channel.assertExchange(name, type, { durable: true });
|
||||
const queue = await this.#channel.assertQueue('', { exclusive: true });
|
||||
|
||||
await this.#channel.bindQueue(queue.queue, name, options.routingKey ?? '');
|
||||
await this.#channel.consume(queue.queue, async (msg) =>
|
||||
await this.#channel.consume(queue.queue, async (msg) =>
|
||||
{
|
||||
this.#logger?.debug(`Subscriber for ${name} (${listener.name}) fired`);
|
||||
if (msg.content && msg.content.toString().startsWith('{'))
|
||||
await listener(JSON.parse(msg.content.toString()), msg);
|
||||
else
|
||||
@ -302,23 +302,27 @@ class MessageBroker
|
||||
}
|
||||
|
||||
// Add item to queue
|
||||
async enqueue (queue: string, content: object, headers?: object): Promise<void | number>
|
||||
async enqueue (queue: string, content: object, headers?: object): Promise<void | number>
|
||||
{
|
||||
this.#logger?.debug(`Adding to queue ${queue}`);
|
||||
const properties = {
|
||||
persistent: true,
|
||||
persistent: true,
|
||||
contentType: 'application/json',
|
||||
headers
|
||||
};
|
||||
|
||||
// The channel is null while the failover is occurring, so enqueue messages for publishing once the connection is restored
|
||||
if (!this.#channel)
|
||||
{
|
||||
this.#logger?.debug('Channel is missing, adding to internal queue');
|
||||
return this.#_qQueue.push({ queue, content, properties });
|
||||
}
|
||||
|
||||
try
|
||||
try
|
||||
{
|
||||
await this.#channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)), properties);
|
||||
}
|
||||
catch (_)
|
||||
catch (_)
|
||||
{
|
||||
this.#_qQueue.push({ queue, content, properties });
|
||||
if (!this.#_qTO)
|
||||
@ -327,8 +331,9 @@ class MessageBroker
|
||||
}
|
||||
|
||||
// Publish to exchange
|
||||
async publish (exchange: string, content: object, { headers, routingKey = '' }: {headers?: string, routingKey?: string} = {}): Promise<void|number>
|
||||
async publish (exchange: string, content: object, { headers, routingKey = '' }: {headers?: string, routingKey?: string} = {}): Promise<void|number>
|
||||
{
|
||||
this.#logger?.debug(`Publishing to ${exchange}`);
|
||||
const properties = {
|
||||
contentType: 'application/json',
|
||||
headers
|
||||
@ -336,13 +341,16 @@ class MessageBroker
|
||||
|
||||
// The channel is null while the failover is occurring, so enqueue messages for publishing once the connection is restored
|
||||
if (!this.#channel)
|
||||
{
|
||||
this.#logger?.debug('Channel is missing, adding to internal queue');
|
||||
return this.#_pQueue.push({ exchange, content, routingKey, properties });
|
||||
}
|
||||
|
||||
try
|
||||
try
|
||||
{
|
||||
await this.#channel.publish(exchange, routingKey, Buffer.from(JSON.stringify(content)), properties);
|
||||
}
|
||||
catch (err)
|
||||
catch (err)
|
||||
{
|
||||
const error = err as Error;
|
||||
if (!error.message.includes('nack'))
|
||||
@ -353,14 +361,14 @@ class MessageBroker
|
||||
}
|
||||
}
|
||||
|
||||
assertExchange (exchange: string, props?: ExchangeDef)
|
||||
assertExchange (exchange: string, props?: ExchangeDef)
|
||||
{
|
||||
if (!this.#channel)
|
||||
if (!this.#channel)
|
||||
throw new Error('Channel doesn\'t exist');
|
||||
return this.#channel.assertExchange(exchange, props?.type ?? 'fanout', props);
|
||||
}
|
||||
|
||||
assertQueue (queue: string, opts?: QueueDef)
|
||||
assertQueue (queue: string, opts?: QueueDef)
|
||||
{
|
||||
if (!this.#channel)
|
||||
throw new Error('Channel doesn\'t exist');
|
||||
@ -368,14 +376,14 @@ class MessageBroker
|
||||
}
|
||||
|
||||
// Processes messages queued up while the broker was unreachable
|
||||
async #_processQueues ()
|
||||
async #_processQueues ()
|
||||
{
|
||||
if (!this.#channel)
|
||||
if (!this.#channel)
|
||||
throw new Error('Channel doesn\'t exist');
|
||||
this.#logger?.status('Processing queues of unsent messages');
|
||||
const pQ = [ ...this.#_pQueue ];
|
||||
this.#_pQueue = [];
|
||||
for (const msg of pQ)
|
||||
for (const msg of pQ)
|
||||
{
|
||||
const { exchange, content, routingKey, properties } = msg;
|
||||
const result = await this.#channel.publish(exchange, routingKey, Buffer.from(JSON.stringify(content)), properties)
|
||||
@ -384,7 +392,7 @@ class MessageBroker
|
||||
this.#_pQueue.push(msg);
|
||||
}
|
||||
const qQ = [ ...this.#_qQueue ];
|
||||
for (const msg of qQ)
|
||||
for (const msg of qQ)
|
||||
{
|
||||
const { queue, content, properties } = msg;
|
||||
const result = await this.#channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)), properties).catch(() => null);
|
||||
@ -392,30 +400,30 @@ class MessageBroker
|
||||
this.#_qQueue.push(msg);
|
||||
}
|
||||
this.#logger?.status('Done processing');
|
||||
if (this.#_qTO)
|
||||
if (this.#_qTO)
|
||||
{
|
||||
clearTimeout(this.#_qTO);
|
||||
this.#_qTO = null;
|
||||
}
|
||||
}
|
||||
|
||||
async #_restoreListeners ()
|
||||
async #_restoreListeners ()
|
||||
{
|
||||
this.#logger?.status(`Restoring ${this.#consumers.size} consumers`);
|
||||
for (const [ name, list ] of this.#consumers)
|
||||
for (const [ name, list ] of this.#consumers)
|
||||
{
|
||||
this.#logger?.debug(`Processing consumer ${name}: ${list.length}`);
|
||||
for (const { consumer, options } of list)
|
||||
for (const { consumer, options } of list)
|
||||
{
|
||||
await this._consume(name, consumer, options);
|
||||
}
|
||||
}
|
||||
|
||||
this.#logger?.status(`Restoring ${this.#subscribers.size} subscribers`);
|
||||
for (const [ name, list ] of this.#subscribers)
|
||||
for (const [ name, list ] of this.#subscribers)
|
||||
{
|
||||
this.#logger?.debug(`Processing subscriber ${name}: ${list.length}`);
|
||||
for (const { subscriber, options } of list)
|
||||
for (const { subscriber, options } of list)
|
||||
{
|
||||
await this._subscribe(name, subscriber, options);
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ import { inspect } from 'node:util';
|
||||
import { MongoClient, MongoClientOptions, Db, Document, WithId, ObjectId, Filter, IndexSpecification, CreateIndexesOptions, FindOptions, ModifyResult, DistinctOptions } from 'mongodb';
|
||||
import { IServer, ILogger } from './interfaces/index.js';
|
||||
|
||||
type Credentials = {
|
||||
interface Credentials {
|
||||
URI?: string,
|
||||
user?: string,
|
||||
password?: string,
|
||||
@ -12,12 +12,12 @@ type Credentials = {
|
||||
authDb?: string
|
||||
}
|
||||
|
||||
type MongoQuery = {
|
||||
interface MongoQuery {
|
||||
_id?: unknown,
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
export type MongoOptions = {
|
||||
export interface MongoOptions {
|
||||
credentials: Credentials,
|
||||
loggerOptions?: object,
|
||||
client?: MongoClientOptions,
|
||||
@ -26,13 +26,13 @@ export type MongoOptions = {
|
||||
metricsBuckets?: number[],
|
||||
}
|
||||
|
||||
type StringIndexable = {[key: string]: boolean | string | number | Document | object}
|
||||
type StringIndexable = Record<string, boolean | string | number | Document | object>;
|
||||
|
||||
const objIsSubset = (superObj: StringIndexable, subObj: StringIndexable): boolean =>
|
||||
{
|
||||
return Object.keys(subObj).every(ele =>
|
||||
return Object.keys(subObj).every(ele =>
|
||||
{
|
||||
if (typeof subObj[ele] === 'object' && typeof superObj[ele] === 'object')
|
||||
if (typeof subObj[ele] === 'object' && typeof superObj[ele] === 'object')
|
||||
{
|
||||
return objIsSubset(superObj[ele] as StringIndexable, subObj[ele] as StringIndexable);
|
||||
}
|
||||
@ -45,7 +45,7 @@ const objIsSubset = (superObj: StringIndexable, subObj: StringIndexable): boolea
|
||||
*
|
||||
* @class MongoDB
|
||||
*/
|
||||
class MongoDB
|
||||
class MongoDB
|
||||
{
|
||||
|
||||
#_database: string;
|
||||
@ -59,7 +59,7 @@ class MongoDB
|
||||
static #queryHistogram?: { startTimer: (labels: object) => () => void };
|
||||
#server: IServer;
|
||||
|
||||
constructor (server: IServer, config: MongoOptions)
|
||||
constructor (server: IServer, config: MongoOptions)
|
||||
{
|
||||
if (!server)
|
||||
throw new Error('Missing reference to server!');
|
||||
@ -67,7 +67,7 @@ class MongoDB
|
||||
throw new Error('No config options provided!');
|
||||
|
||||
const { user, password, host, port, database, URI, authDb } = config.credentials;
|
||||
if ((!host?.length || !port || !database?.length) && !URI)
|
||||
if ((!host?.length || !port || !database.length) && !URI)
|
||||
throw new Error('Must provide host, port, and database OR URI parameters!');
|
||||
|
||||
this.#config = config;
|
||||
@ -79,20 +79,20 @@ class MongoDB
|
||||
this.#logger = server.createLogger(this, config.loggerOptions);
|
||||
this.#server = server;
|
||||
|
||||
if (URI)
|
||||
if (URI)
|
||||
{
|
||||
this.#URI = URI;
|
||||
}
|
||||
else
|
||||
else
|
||||
{
|
||||
let AUTH_DB = authDb;
|
||||
const auth = user ? `${user}:${password}@` : '';
|
||||
if (!AUTH_DB && auth)
|
||||
if (!AUTH_DB && auth)
|
||||
{
|
||||
this.#logger?.warn('No explicit auth db provided with MONGO_AUTH_DB, will attempt to use MONGO_DB for auth source');
|
||||
AUTH_DB = authDb;
|
||||
}
|
||||
else if (!auth)
|
||||
else if (!auth)
|
||||
{
|
||||
this.#logger?.warn('No auth provided, proceeding without');
|
||||
}
|
||||
@ -109,12 +109,12 @@ class MongoDB
|
||||
.on('open', () => this.#logger?.info('MongoDB client connected'));
|
||||
}
|
||||
|
||||
get database ()
|
||||
get database ()
|
||||
{
|
||||
return this.#_database;
|
||||
}
|
||||
|
||||
get client ()
|
||||
get client ()
|
||||
{
|
||||
return this.#_client;
|
||||
}
|
||||
@ -124,7 +124,7 @@ class MongoDB
|
||||
*
|
||||
* @memberof MongoDB
|
||||
*/
|
||||
async init ()
|
||||
async init ()
|
||||
{
|
||||
if (!this.#load)
|
||||
return this.#logger?.info('Not loading MongoDB');
|
||||
@ -149,9 +149,9 @@ class MongoDB
|
||||
{
|
||||
this.#logger?.info('Setting up metric recording');
|
||||
MongoDB.#queryHistogram = new Prometheus.Histogram({
|
||||
name: 'mongo_queries',
|
||||
help: 'Tracks query duration in seconds and frequency',
|
||||
buckets: this.#config.metricsBuckets ?? Prometheus?.exponentialBuckets(0.005, 2, 10),
|
||||
name: 'mongo_queries',
|
||||
help: 'Tracks query duration in seconds and frequency',
|
||||
buckets: this.#config.metricsBuckets ?? Prometheus?.exponentialBuckets(0.005, 2, 10),
|
||||
labelNames: [ 'type' ] as const
|
||||
});
|
||||
if (this.#server.registerMetric)
|
||||
@ -162,7 +162,7 @@ class MongoDB
|
||||
return this;
|
||||
}
|
||||
|
||||
async close ()
|
||||
async close ()
|
||||
{
|
||||
if (!this.#db)
|
||||
return;
|
||||
@ -173,7 +173,7 @@ class MongoDB
|
||||
this.#logger?.status('Database closed');
|
||||
}
|
||||
|
||||
get mongoClient ()
|
||||
get mongoClient ()
|
||||
{
|
||||
return this.#_client;
|
||||
}
|
||||
@ -186,7 +186,7 @@ class MongoDB
|
||||
* @returns {Array} An array containing the corresponding objects for the query
|
||||
* @memberof Database
|
||||
*/
|
||||
async find<T extends Document> (db: string, query: MongoQuery, options?: FindOptions<T>): Promise<WithId<T>[]>
|
||||
async find<T extends Document> (db: string, query: MongoQuery, options?: FindOptions<T>): Promise<WithId<T>[]>
|
||||
{
|
||||
if (!this.#db)
|
||||
throw new Error('MongoDB not connected');
|
||||
@ -215,7 +215,7 @@ class MongoDB
|
||||
* @returns {Object} An object containing the queried data
|
||||
* @memberof Database
|
||||
*/
|
||||
async findOne<T extends Document> (db: string, query: MongoQuery, options: FindOptions<T> = {}): Promise<WithId<T> | null>
|
||||
async findOne<T extends Document> (db: string, query: MongoQuery, options: FindOptions<T> = {}): Promise<WithId<T> | null>
|
||||
{
|
||||
if (!this.#db)
|
||||
throw new Error('MongoDB not connected');
|
||||
@ -242,7 +242,7 @@ class MongoDB
|
||||
* @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified
|
||||
* @memberof Database
|
||||
*/
|
||||
async updateMany<T extends Document> (db: string, filter: MongoQuery, data: T, upsert = false)
|
||||
async updateMany<T extends Document> (db: string, filter: MongoQuery, data: T, upsert = false)
|
||||
{
|
||||
if (!this.#db)
|
||||
throw new Error('MongoDB not connected');
|
||||
@ -270,7 +270,7 @@ class MongoDB
|
||||
* @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified
|
||||
* @memberof Database
|
||||
*/
|
||||
async updateOne (db: string, filter: MongoQuery, data: Document, upsert = false)
|
||||
async updateOne (db: string, filter: MongoQuery, data: Document, upsert = false)
|
||||
{
|
||||
if (!this.#db)
|
||||
throw new Error('MongoDB not connected');
|
||||
@ -296,7 +296,7 @@ class MongoDB
|
||||
* @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified
|
||||
* @memberof Database
|
||||
*/
|
||||
async insertOne (db: string, data: Document)
|
||||
async insertOne (db: string, data: Document)
|
||||
{
|
||||
if (!this.#db)
|
||||
throw new Error('MongoDB not connected');
|
||||
@ -313,7 +313,7 @@ class MongoDB
|
||||
return result;
|
||||
}
|
||||
|
||||
async deleteOne (db: string, filter: Document)
|
||||
async deleteOne (db: string, filter: Document)
|
||||
{
|
||||
if (!this.#db)
|
||||
throw new Error('MongoDB not connected');
|
||||
@ -358,7 +358,7 @@ class MongoDB
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
filter._id = new ObjectId(filter._id);
|
||||
|
||||
|
||||
const endTimer = MongoDB.#queryHistogram?.startTimer({ type: 'distinct' });
|
||||
const result = await this.#db.collection<T>(db).distinct(key, filter, options);
|
||||
if (endTimer)
|
||||
@ -376,7 +376,7 @@ class MongoDB
|
||||
* @returns
|
||||
* @memberof Database
|
||||
*/
|
||||
async push (db: string, filter: Document, data: object, upsert = false)
|
||||
async push (db: string, filter: Document, data: object, upsert = false)
|
||||
{
|
||||
if (!this.#db)
|
||||
throw new Error('MongoDB not connected');
|
||||
@ -402,7 +402,7 @@ class MongoDB
|
||||
* @returns {object}
|
||||
* @memberof Database
|
||||
*/
|
||||
random<T extends Document> (db: string, filter: Document = {}, amount = 1)
|
||||
random<T extends Document> (db: string, filter: Document = {}, amount = 1)
|
||||
{
|
||||
if (!this.#db)
|
||||
throw new Error('MongoDB not connected');
|
||||
@ -420,7 +420,7 @@ class MongoDB
|
||||
return cursor.toArray();
|
||||
}
|
||||
|
||||
stats (options = {})
|
||||
stats (options = {})
|
||||
{
|
||||
if (!this.#db)
|
||||
throw new Error('MongoDB not connected');
|
||||
@ -428,14 +428,14 @@ class MongoDB
|
||||
return result;
|
||||
}
|
||||
|
||||
collection<T extends Document> (coll: string)
|
||||
collection<T extends Document> (coll: string)
|
||||
{
|
||||
if (!this.#db)
|
||||
throw new Error('MongoDB not connected');
|
||||
return this.#db.collection<T>(coll);
|
||||
}
|
||||
|
||||
count (coll: string, query: Document)
|
||||
count (coll: string, query: Document)
|
||||
{
|
||||
if (!this.#db)
|
||||
throw new Error('MongoDB not connected');
|
||||
@ -448,7 +448,7 @@ class MongoDB
|
||||
return Promise.reject(new Error('MongoDB not connected'));
|
||||
if (!(index instanceof Array))
|
||||
index = [ index ];
|
||||
|
||||
|
||||
const collections = await this.#db.collections();
|
||||
if (!collections.some((coll) => coll.namespace.split('.')[1] === collection))
|
||||
await this.#db.createCollection(collection);
|
||||
@ -461,7 +461,7 @@ class MongoDB
|
||||
if (existing)
|
||||
await this.#db.collection(collection).dropIndex(existing.name);
|
||||
|
||||
try
|
||||
try
|
||||
{
|
||||
await this.#db.collection(collection).createIndex(index, options);
|
||||
}
|
||||
@ -475,11 +475,11 @@ class MongoDB
|
||||
{
|
||||
if (!this.#db)
|
||||
return Promise.reject(new Error('MongoDB not connected'));
|
||||
for (const index of indices)
|
||||
for (const index of indices)
|
||||
await this.ensureIndex(collection, index, options);
|
||||
}
|
||||
|
||||
#indexesEqual (existing: Document, options?: CreateIndexesOptions & StringIndexable)
|
||||
#indexesEqual (existing: Document, options?: CreateIndexesOptions & StringIndexable)
|
||||
{
|
||||
// 3 keys on the existing means that only the name was given
|
||||
if (!options && Object.keys(existing).length === 3)
|
||||
@ -488,11 +488,11 @@ class MongoDB
|
||||
return false;
|
||||
|
||||
const keys = Object.keys(options);
|
||||
for (const key of keys)
|
||||
for (const key of keys)
|
||||
{
|
||||
if (typeof options[key] !== typeof existing[key])
|
||||
return false;
|
||||
|
||||
|
||||
if (typeof options[key] === 'object' && !objIsSubset(existing, options))
|
||||
return false;
|
||||
else if (options[key] !== existing[key])
|
||||
@ -500,7 +500,7 @@ class MongoDB
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,20 +3,20 @@ import { MessageBroker } from '../build/esm/index.js';
|
||||
const credentials = JSON.parse(readFileSync('../brokerCreds.json', { encoding: 'utf-8' }));
|
||||
|
||||
const broker = new MessageBroker({
|
||||
createLogger: () =>
|
||||
createLogger: () =>
|
||||
{
|
||||
return {
|
||||
debug: console.log,
|
||||
info: console.log,
|
||||
debug: console.log,
|
||||
info: console.log,
|
||||
status: console.log,
|
||||
warn: console.log,
|
||||
error: console.error
|
||||
warn: console.log,
|
||||
error: console.error
|
||||
};
|
||||
}
|
||||
}, credentials);
|
||||
|
||||
await broker.init();
|
||||
broker.subscribe('db_cluster_status', (message) =>
|
||||
broker.subscribe('db_cluster_status', (message) =>
|
||||
{
|
||||
if (message instanceof Buffer)
|
||||
console.log(message.toString());
|
||||
|
@ -5,57 +5,57 @@ import { readFileSync } from 'fs';
|
||||
const credentials = JSON.parse(readFileSync('./credentials.json', { encoding: 'utf-8' }));
|
||||
|
||||
const maria = new MariaDB({
|
||||
createLogger: () =>
|
||||
createLogger: () =>
|
||||
{
|
||||
return {
|
||||
debug: console.log,
|
||||
info: console.log,
|
||||
debug: console.log,
|
||||
info: console.log,
|
||||
status: console.log,
|
||||
warn: console.log,
|
||||
error: console.error
|
||||
warn: console.log,
|
||||
error: console.error
|
||||
};
|
||||
}
|
||||
}, {
|
||||
load: true,
|
||||
load: true,
|
||||
credentials,
|
||||
cluster: {
|
||||
canRetry: true,
|
||||
canRetry: true,
|
||||
removeNodeErrorCount: 5,
|
||||
restoreNodeTimeout: 60000,
|
||||
defaultSelector: 'RR'
|
||||
restoreNodeTimeout: 60000,
|
||||
defaultSelector: 'RR'
|
||||
}
|
||||
});
|
||||
|
||||
const broker = new MessageBroker({
|
||||
createLogger: () =>
|
||||
createLogger: () =>
|
||||
{
|
||||
return {
|
||||
debug: console.log,
|
||||
info: console.log,
|
||||
debug: console.log,
|
||||
info: console.log,
|
||||
status: console.log,
|
||||
warn: console.log,
|
||||
error: console.error
|
||||
warn: console.log,
|
||||
error: console.error
|
||||
};
|
||||
}
|
||||
}, {
|
||||
load: true,
|
||||
host: 'rabbitmq-01.stylis.local',
|
||||
user: 'stylis',
|
||||
pass: 'RrwJyrfeXFMimDH3hjZ5xSreMAmXtQJj',
|
||||
load: true,
|
||||
host: 'rabbitmq-01.stylis.local',
|
||||
user: 'stylis',
|
||||
pass: 'RrwJyrfeXFMimDH3hjZ5xSreMAmXtQJj',
|
||||
vhost: 'development',
|
||||
port: 5672
|
||||
port: 5672
|
||||
});
|
||||
|
||||
await broker.init();
|
||||
await maria.init();
|
||||
broker.subscribe('db_cluster_status', maria.statusUpdateListener);
|
||||
|
||||
setInterval(async () =>
|
||||
setInterval(async () =>
|
||||
{
|
||||
console.log('Result: ', await maria.query('SHOW STATUS WHERE `Variable_name` = "wsrep_gcomm_uuid" OR `Variable_name` = "wsrep_local_state_comment"', { node: 'maria-t03', errorIfNodeUnavailable: true }).catch(() => null));
|
||||
}, 15_000).unref();
|
||||
|
||||
process.on('SIGINT', async () =>
|
||||
process.on('SIGINT', async () =>
|
||||
{
|
||||
await broker.close();
|
||||
await maria.close();
|
||||
|
@ -5,24 +5,24 @@ import { MariaDB } from '../build/esm/index.js';
|
||||
const credentials = JSON.parse(readFileSync('../credentials.json', { encoding: 'utf-8' }));
|
||||
|
||||
const maria = new MariaDB({
|
||||
createLogger: () =>
|
||||
createLogger: () =>
|
||||
{
|
||||
return {
|
||||
debug: console.log,
|
||||
info: console.log,
|
||||
debug: console.log,
|
||||
info: console.log,
|
||||
status: console.log,
|
||||
warn: console.log,
|
||||
error: console.error
|
||||
warn: console.log,
|
||||
error: console.error
|
||||
};
|
||||
}
|
||||
}, {
|
||||
load: true,
|
||||
load: true,
|
||||
credentials,
|
||||
cluster: {
|
||||
canRetry: true,
|
||||
canRetry: true,
|
||||
removeNodeErrorCount: 5,
|
||||
restoreNodeTimeout: 60000,
|
||||
defaultSelector: 'RR'
|
||||
restoreNodeTimeout: 60000,
|
||||
defaultSelector: 'RR'
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -1,19 +1,19 @@
|
||||
import { MongoDB } from '../build/esm/index.js';
|
||||
|
||||
const mongo = new MongoDB({
|
||||
createLogger: () =>
|
||||
createLogger: () =>
|
||||
{
|
||||
return {
|
||||
debug: console.log,
|
||||
info: console.log,
|
||||
debug: console.log,
|
||||
info: console.log,
|
||||
status: console.log,
|
||||
warn: console.log,
|
||||
error: console.error
|
||||
warn: console.log,
|
||||
error: console.error
|
||||
};
|
||||
}
|
||||
}, {
|
||||
credentials: {
|
||||
URI: 'mongodb://127.0.0.1:27017',
|
||||
URI: 'mongodb://127.0.0.1:27017',
|
||||
database: 'framework-proto'
|
||||
},
|
||||
load: true
|
||||
|
Loading…
Reference in New Issue
Block a user