@node-ts/bus-core
Advanced tools
Comparing version 1.1.11 to 1.2.0
@@ -5,7 +5,7 @@ "use strict"; | ||
class FailMessageOutsideHandlingContext extends Error { | ||
constructor(help = `Calling .fail() with a message indicates that the message received from the | ||
constructor(help = `Calling .failMessage() with a message indicates that the message received from the | ||
queue can not be processed even with retries and should immediately be sent | ||
to the dead letter queue. | ||
This error occurs when .fail() has been called outside of a message handling context, | ||
This error occurs when .failMessage() has been called outside of a message handling context, | ||
or more specifically - outside the stack of a Handler() operation`) { | ||
@@ -12,0 +12,0 @@ super(`Attempted to fail message outside of a message handling context`); |
export * from './fail-message-outside-handling-context'; | ||
export * from './class-handler-not-resolved'; | ||
export * from './container-not-registered'; | ||
export * from './return-message-outside-handling-context'; |
@@ -7,1 +7,2 @@ "use strict"; | ||
tslib_1.__exportStar(require("./container-not-registered"), exports); | ||
tslib_1.__exportStar(require("./return-message-outside-handling-context"), exports); |
@@ -112,4 +112,9 @@ import { Transport, TransportMessage } from '../transport'; | ||
*/ | ||
fail(): Promise<void>; | ||
failMessage(): Promise<void>; | ||
/** | ||
* Instructs that the current message should be returned to the queue for retry. | ||
* @throws ReturnMessageOutsideHandlingContext if called outside a message handling context | ||
*/ | ||
returnMessage(): Promise<void>; | ||
/** | ||
* Instructs the bus to start reading messages from the underlying service queue | ||
@@ -116,0 +121,0 @@ * and dispatching to message handlers. |
@@ -14,2 +14,3 @@ "use strict"; | ||
const als_1 = tslib_1.__importDefault(require("alscontext/dist/als/als")); | ||
const message_lifecycle_context_1 = require("../message-lifecycle-context"); | ||
const EMPTY_QUEUE_SLEEP_MS = 500; | ||
@@ -69,3 +70,7 @@ class BusInstance { | ||
yield this.dispatchMessageToHandlers(message.domainMessage, message.attributes); | ||
yield this.transport.deleteMessage(message); | ||
const { messageReturnedToQueue } = message_lifecycle_context_1.messageLifecycleContext.get(); | ||
if (!messageReturnedToQueue) { | ||
this.logger.debug('Message was returned to queue by a handler and will not be deleted', { message }); | ||
yield this.transport.deleteMessage(message); | ||
} | ||
return next(); | ||
@@ -156,3 +161,3 @@ }); | ||
*/ | ||
fail() { | ||
failMessage() { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
@@ -168,2 +173,18 @@ const message = message_handling_context_1.messageHandlingContext.get(); | ||
/** | ||
* Instructs that the current message should be returned to the queue for retry. | ||
* @throws ReturnMessageOutsideHandlingContext if called outside a message handling context | ||
*/ | ||
returnMessage() { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const context = message_lifecycle_context_1.messageLifecycleContext.get(); | ||
const message = message_handling_context_1.messageHandlingContext.get(); | ||
if (!context || !message) { | ||
throw new error_1.ReturnMessageOutsideHandlingContext(); | ||
} | ||
message_lifecycle_context_1.messageLifecycleContext.set(Object.assign(Object.assign({}, context), { messageReturnedToQueue: true })); | ||
this.logger.debug('Returning message', { message }); | ||
return this.transport.returnMessage(message); | ||
}); | ||
} | ||
/** | ||
* Instructs the bus to start reading messages from the underlying service queue | ||
@@ -277,7 +298,9 @@ * and dispatching to message handlers. | ||
try { | ||
yield this.messageReadMiddleware.dispatch(message); | ||
this.afterDispatch.emit({ | ||
message: message.domainMessage, | ||
attributes: message.attributes | ||
}); | ||
yield message_lifecycle_context_1.messageLifecycleContext.run({ messageReturnedToQueue: false }, () => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
yield this.messageReadMiddleware.dispatch(message); | ||
this.afterDispatch.emit({ | ||
message: message.domainMessage, | ||
attributes: message.attributes | ||
}); | ||
})); | ||
return true; | ||
@@ -284,0 +307,0 @@ } |
@@ -98,3 +98,3 @@ "use strict"; | ||
if (messageIndex < 0) { | ||
// actions like .fail() will cause the message to already be deleted | ||
// actions like .failMessage() will cause the message to already be deleted | ||
this.logger.debug('Message already deleted', { message, messageIndex }); | ||
@@ -101,0 +101,0 @@ return; |
@@ -53,3 +53,3 @@ import { Event, Command, MessageAttributes } from '@node-ts/bus-messages'; | ||
* | ||
* @returns The message construct from the underlying transport, that inclues both the raw message envelope | ||
* @returns The message construct from the underlying transport, that includes both the raw message envelope | ||
* plus the contents or body that contains the `@node-ts/bus-messages` message. | ||
@@ -69,3 +69,3 @@ */ | ||
*/ | ||
returnMessage(message: TransportMessage<TransportMessageType>): Promise<void>; | ||
returnMessage(message: TransportMessage<unknown>): Promise<void>; | ||
/** | ||
@@ -72,0 +72,0 @@ * An optional function that is called before startup that will provide core dependencies |
{ | ||
"name": "@node-ts/bus-core", | ||
"version": "1.1.11", | ||
"version": "1.2.0", | ||
"description": "A service bus for message-based, distributed node applications", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
export class FailMessageOutsideHandlingContext extends Error { | ||
constructor( | ||
readonly help = `Calling .fail() with a message indicates that the message received from the | ||
readonly help = `Calling .failMessage() with a message indicates that the message received from the | ||
queue can not be processed even with retries and should immediately be sent | ||
to the dead letter queue. | ||
This error occurs when .fail() has been called outside of a message handling context, | ||
This error occurs when .failMessage() has been called outside of a message handling context, | ||
or more specifically - outside the stack of a Handler() operation` | ||
@@ -9,0 +9,0 @@ ) { |
export * from './fail-message-outside-handling-context' | ||
export * from './class-handler-not-resolved' | ||
export * from './container-not-registered' | ||
export * from './return-message-outside-handling-context' |
@@ -425,3 +425,3 @@ import { Command, MessageAttributes } from '@node-ts/bus-messages' | ||
bus = Bus.configure().build() | ||
await bus.fail() | ||
await bus.failMessage() | ||
fail('Expected FailMessageOutsideHandlingContext to have been thrown') | ||
@@ -448,3 +448,3 @@ } catch (error) { | ||
handlerFor(TestCommand, async () => { | ||
await bus.fail() | ||
await bus.failMessage() | ||
events.emit('event') | ||
@@ -451,0 +451,0 @@ }) |
@@ -30,3 +30,4 @@ import { Transport, TransportMessage } from '../transport' | ||
ClassHandlerNotResolved, | ||
FailMessageOutsideHandlingContext | ||
FailMessageOutsideHandlingContext, | ||
ReturnMessageOutsideHandlingContext | ||
} from '../error' | ||
@@ -39,2 +40,3 @@ import { v4 as generateUuid } from 'uuid' | ||
import ALS from 'alscontext/dist/als/als' | ||
import { messageLifecycleContext } from '../message-lifecycle-context' | ||
@@ -234,3 +236,3 @@ const EMPTY_QUEUE_SLEEP_MS = 500 | ||
*/ | ||
async fail(): Promise<void> { | ||
async failMessage(): Promise<void> { | ||
const message = messageHandlingContext.get() | ||
@@ -245,2 +247,20 @@ if (!message) { | ||
/** | ||
* Instructs that the current message should be returned to the queue for retry. | ||
* @throws ReturnMessageOutsideHandlingContext if called outside a message handling context | ||
*/ | ||
async returnMessage(): Promise<void> { | ||
const context = messageLifecycleContext.get() | ||
const message = messageHandlingContext.get() | ||
if (!context || !message) { | ||
throw new ReturnMessageOutsideHandlingContext() | ||
} | ||
messageLifecycleContext.set({ | ||
...context, | ||
messageReturnedToQueue: true | ||
}) | ||
this.logger.debug('Returning message', { message }) | ||
return this.transport.returnMessage(message) | ||
} | ||
/** | ||
* Instructs the bus to start reading messages from the underlying service queue | ||
@@ -369,8 +389,14 @@ * and dispatching to message handlers. | ||
try { | ||
await this.messageReadMiddleware.dispatch(message) | ||
await messageLifecycleContext.run( | ||
{ messageReturnedToQueue: false }, | ||
async () => { | ||
await this.messageReadMiddleware.dispatch(message) | ||
this.afterDispatch.emit({ | ||
message: message.domainMessage, | ||
attributes: message.attributes | ||
}) | ||
this.afterDispatch.emit({ | ||
message: message.domainMessage, | ||
attributes: message.attributes | ||
}) | ||
} | ||
) | ||
return true | ||
@@ -556,3 +582,12 @@ } catch (error) { | ||
) | ||
await this.transport.deleteMessage(message) | ||
const { messageReturnedToQueue } = messageLifecycleContext.get() | ||
if (!messageReturnedToQueue) { | ||
this.logger.debug( | ||
'Message was returned to queue by a handler and will not be deleted', | ||
{ message } | ||
) | ||
await this.transport.deleteMessage(message) | ||
} | ||
return next() | ||
@@ -559,0 +594,0 @@ } |
@@ -205,3 +205,3 @@ import { InMemoryQueue, InMemoryMessage } from './in-memory-queue' | ||
await bus.send(new TestCommand()) | ||
await bus.fail() | ||
await bus.failMessage() | ||
}) | ||
@@ -208,0 +208,0 @@ ) |
@@ -147,3 +147,3 @@ import { Transport } from './transport' | ||
if (messageIndex < 0) { | ||
// actions like .fail() will cause the message to already be deleted | ||
// actions like .failMessage() will cause the message to already be deleted | ||
this.logger.debug('Message already deleted', { message, messageIndex }) | ||
@@ -150,0 +150,0 @@ return |
@@ -67,3 +67,3 @@ import { Event, Command, MessageAttributes } from '@node-ts/bus-messages' | ||
* | ||
* @returns The message construct from the underlying transport, that inclues both the raw message envelope | ||
* @returns The message construct from the underlying transport, that includes both the raw message envelope | ||
* plus the contents or body that contains the `@node-ts/bus-messages` message. | ||
@@ -85,3 +85,3 @@ */ | ||
*/ | ||
returnMessage(message: TransportMessage<TransportMessageType>): Promise<void> | ||
returnMessage(message: TransportMessage<unknown>): Promise<void> | ||
@@ -88,0 +88,0 @@ /** |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
375394
275
9196