pg-trx-outbox
Version:
Transactional outbox of Postgres for Node.js with little Event Sourcing
41 lines (40 loc) • 1.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseAdapter = void 0;
const ts_fp_di_1 = require("ts-fp-di");
const perf_hooks_1 = require("perf_hooks");
class BaseAdapter {
async baseHandleMessage(message) {
return await (0, ts_fp_di_1.diInit)(async () => {
(0, ts_fp_di_1.diSet)('pg_trx_outbox_context_id', message.context_id);
let respItem;
const hist = (0, perf_hooks_1.monitorEventLoopDelay)();
hist.enable();
const now = performance.now();
const beforeMemory = process.memoryUsage();
const oldCpuUsage = process.cpuUsage();
try {
const { value, meta, error } = await this.handleMessage(message);
respItem = { value, status: 'fulfilled', ...(meta ? { meta } : {}), ...(error ? { error } : {}) };
}
catch (reason) {
respItem = { reason, status: 'rejected' };
}
const cpuUsage = process.cpuUsage(oldCpuUsage);
const afterMemory = process.memoryUsage();
const time = performance.now() - now;
hist.disable();
const { max, min, mean, stddev } = hist;
const pgTrxOutbox = {
time,
libuv: { max, min, mean, stddev },
beforeMemory,
afterMemory,
uptime: process.uptime(),
cpuUsage,
};
return { ...respItem, meta: { pgTrxOutbox, ...respItem.meta } };
});
}
}
exports.BaseAdapter = BaseAdapter;