pg-transactional-outbox
Version:
A PostgreSQL based transactional outbox and inbox pattern implementation to support exactly once message processing (with at least once message delivery).
69 lines • 3.58 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNextMessagesBatch = void 0;
const utils_1 = require("../common/utils");
const lastLogTime = {
inbox: 0,
outbox: 0,
};
/**
* Gets the next inbox messages from the database and sets the locked_until
* @param maxMessages The maximum number of messages to fetch.
* @param client The database client to use for the query.
* @param settings The settings object for the inbox table and function name.
* @param logger The logger to use for logging.
* @param outboxOrInbox The outbox or inbox name
* @returns A promise that resolves to the query result object.
*/
const getNextMessagesBatch = (maxMessages, client, settings, logger, outboxOrInbox) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
const schema = settings.nextMessagesFunctionSchema;
const func = settings.nextMessagesFunctionName;
const lock = settings.nextMessagesLockInMs;
const messagesResult = yield (0, utils_1.executeTransaction)(client, (client) => __awaiter(void 0, void 0, void 0, function* () {
return yield client.query(
/* sql */ `SELECT * FROM ${schema}.${func}(${maxMessages}, ${lock});`);
}), utils_1.IsolationLevel.RepeatableRead);
if ((_a = messagesResult.rowCount) !== null && _a !== void 0 ? _a : 0 > 0) {
logger.debug({ messageIds: messagesResult.rows.map((m) => m.id) }, `Found ${messagesResult.rowCount} ${outboxOrInbox} message(s) to process.`);
lastLogTime[outboxOrInbox] = Date.now();
}
else {
if (lastLogTime[outboxOrInbox] <= Date.now() - 60000) {
logger.trace(`Found no unprocessed ${outboxOrInbox} messages in the last minute.`);
lastLogTime[outboxOrInbox] = Date.now();
}
}
return messagesResult.rows.map(mapInbox);
});
exports.getNextMessagesBatch = getNextMessagesBatch;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mapInbox = (i) => {
var _a, _b, _c, _d, _e, _f;
return ({
id: i.id,
aggregateType: i.aggregate_type,
aggregateId: i.aggregate_id,
messageType: i.message_type,
payload: i.payload,
metadata: i.metadata,
createdAt: i.created_at.toISOString(),
concurrency: i.concurrency,
finishedAttempts: i.finished_attempts,
lockedUntil: (_b = (_a = i.locked_until) === null || _a === void 0 ? void 0 : _a.toISOString()) !== null && _b !== void 0 ? _b : null,
startedAttempts: i.started_attempts,
processedAt: (_d = (_c = i.processed_at) === null || _c === void 0 ? void 0 : _c.toISOString()) !== null && _d !== void 0 ? _d : null,
abandonedAt: (_f = (_e = i.abandoned_at) === null || _e === void 0 ? void 0 : _e.toISOString()) !== null && _f !== void 0 ? _f : null,
segment: i.segment,
});
};
//# sourceMappingURL=next-messages.js.map