UNPKG

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).

166 lines 7.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getOutboxListenerEnvTemplate = exports.getInboxListenerEnvTemplate = exports.getOutboxListenerSettings = exports.getInboxListenerSettings = exports.outboxEnvPrefix = exports.inboxEnvPrefix = exports.fallbackEnvPrefix = exports.applyDefaultListenerConfigValues = void 0; const env_settings_1 = require("./env-settings"); const defaultSettings = { dbSchema: 'public', messageProcessingTimeoutInMs: 15000, maxAttempts: 5, enableMaxAttemptsProtection: true, maxPoisonousAttempts: 3, enablePoisonousMessageProtection: true, maxMessageNotFoundAttempts: 0, maxMessageNotFoundDelayInMs: 10, messageCleanupIntervalInMs: 5 * 60 * 1000, messageCleanupProcessedInSec: 7 * 24 * 60 * 60, messageCleanupAbandonedInSec: 14 * 24 * 60 * 60, messageCleanupAllInSec: 60 * 24 * 60 * 60, }; const applyDefaultListenerConfigValues = (config) => { var _a; const filledConfig = Object.assign(Object.assign({}, config), { dbHandlerConfig: (_a = config.dbHandlerConfig) !== null && _a !== void 0 ? _a : config.dbListenerConfig, settings: Object.assign(Object.assign({}, defaultSettings), config.settings) }); return filledConfig; }; exports.applyDefaultListenerConfigValues = applyDefaultListenerConfigValues; const basicSettingsMap = [ { constantName: 'DB_SCHEMA', default: defaultSettings.dbSchema, func: env_settings_1.getEnvVariableString, description: 'The database schema name where the table is located.', }, { constantName: 'MESSAGE_PROCESSING_TIMEOUT_IN_MS', default: defaultSettings.messageProcessingTimeoutInMs, func: env_settings_1.getEnvVariableNumber, description: 'Stop the message handler after this time has passed.', }, { constantName: 'MAX_ATTEMPTS', func: env_settings_1.getEnvVariableNumber, default: defaultSettings.maxAttempts, description: 'The maximum number of attempts to handle a message. With max 5 attempts a message is handled once initially and up to four more times for retries.', }, { constantName: 'MAX_POISONOUS_ATTEMPTS', default: defaultSettings.maxPoisonousAttempts, func: env_settings_1.getEnvVariableNumber, description: 'The maximum number of times a message should be attempted which was started but did not finish (neither error nor success).', }, { constantName: 'MESSAGE_CLEANUP_INTERVAL_IN_MS', default: defaultSettings.messageCleanupIntervalInMs, func: env_settings_1.getEnvVariableNumber, description: 'Time in milliseconds between the execution of the old message cleanups. Set it to zero to disable automatic message cleanup.', }, { constantName: 'MESSAGE_CLEANUP_PROCESSED_IN_SEC', default: defaultSettings.messageCleanupProcessedInSec, func: env_settings_1.getEnvVariableNumber, description: 'Delete messages that were successfully processed after X seconds.', }, { constantName: 'MESSAGE_CLEANUP_ABANDONED_IN_SEC', default: defaultSettings.messageCleanupAbandonedInSec, func: env_settings_1.getEnvVariableNumber, description: 'Delete messages that could not be processed after X seconds.', }, { constantName: 'MESSAGE_CLEANUP_ALL_IN_SEC', default: defaultSettings.messageCleanupAllInSec, func: env_settings_1.getEnvVariableNumber, description: 'Delete all old messages after X seconds.', }, ]; const outboxSettingsMap = [ { constantName: 'DB_TABLE', default: 'outbox', func: env_settings_1.getEnvVariableString, skipFallback: true, description: 'The name of the database outbox table.', }, { constantName: 'ENABLE_MAX_ATTEMPTS_PROTECTION', default: false, func: env_settings_1.getEnvVariableBoolean, skipFallback: true, description: 'Enable the max attempts protection.', }, { constantName: 'ENABLE_POISONOUS_MESSAGE_PROTECTION', default: false, func: env_settings_1.getEnvVariableBoolean, skipFallback: true, description: 'Enable the max poisonous attempts protection.', }, ]; const inboxSettingsMap = [ { constantName: 'DB_TABLE', default: 'inbox', func: env_settings_1.getEnvVariableString, skipFallback: true, description: 'The name of the database inbox table.', }, { constantName: 'ENABLE_MAX_ATTEMPTS_PROTECTION', default: true, func: env_settings_1.getEnvVariableBoolean, skipFallback: true, description: 'Enable the max attempts protection.', }, { constantName: 'ENABLE_POISONOUS_MESSAGE_PROTECTION', default: true, func: env_settings_1.getEnvVariableBoolean, skipFallback: true, description: 'Enable the max poisonous attempts protection.', }, ]; exports.fallbackEnvPrefix = 'TRX_'; exports.inboxEnvPrefix = 'TRX_INBOX_'; exports.outboxEnvPrefix = 'TRX_OUTBOX_'; /** * Loads the environment variables into the listener settings object. It * supports reading an inbox specific setting or a general one. * Please use the `getXxxxListenerEnvTemplate` functions to get a list of all * the outbox and inbox relevant settings for the replication or polling * listener. * @example * TRX_DB_SCHEMA=trx_schema * TRX_INBOX_DB_TABLE=inbox_table * TRX_MESSAGE_PROCESSING_TIMEOUT_IN_MS=30000 * @param env The process.env variable or a custom object. * @returns The listener settings object with filled with the ENV variables */ const getInboxListenerSettings = (env = process.env) => (0, env_settings_1.getConfigSettings)([...basicSettingsMap, ...inboxSettingsMap], exports.inboxEnvPrefix, exports.fallbackEnvPrefix, env); exports.getInboxListenerSettings = getInboxListenerSettings; /** * Loads the environment variables into the listener settings object. It * supports reading an outbox specific setting or a general one. * Please use the `getXxxxListenerEnvTemplate` functions to get a list of all * the outbox and outbox relevant settings for the replication or polling * listener. * @example * TRX_DB_SCHEMA=trx_schema * TRX_OUTBOX_DB_TABLE=outbox_table * TRX_MESSAGE_PROCESSING_TIMEOUT_IN_MS=30000 * @param env The process.env variable or a custom object. * @returns The listener settings object with filled with the ENV variables */ const getOutboxListenerSettings = (env = process.env) => (0, env_settings_1.getConfigSettings)([...basicSettingsMap, ...outboxSettingsMap], exports.outboxEnvPrefix, exports.fallbackEnvPrefix, env); exports.getOutboxListenerSettings = getOutboxListenerSettings; /** * Creates the available env variables and their default values for the basic * inbox listener env variables. */ const getInboxListenerEnvTemplate = (defaultOverrides) => (0, env_settings_1.getConfigSettingsEnvTemplate)([...basicSettingsMap, ...inboxSettingsMap], exports.inboxEnvPrefix, exports.fallbackEnvPrefix, defaultOverrides); exports.getInboxListenerEnvTemplate = getInboxListenerEnvTemplate; /** * Creates the available env variables and their default values for the basic * outbox listener env variables. */ const getOutboxListenerEnvTemplate = (defaultOverrides) => (0, env_settings_1.getConfigSettingsEnvTemplate)([...basicSettingsMap, ...outboxSettingsMap], exports.outboxEnvPrefix, exports.fallbackEnvPrefix, defaultOverrides); exports.getOutboxListenerEnvTemplate = getOutboxListenerEnvTemplate; //# sourceMappingURL=listener-config.js.map