Skip to content

Commit 2c19c92

Browse files
committed
Squashed commit of the following:
commit 0093a01e00e326b6bb738a4df43b5165f69c87a5 Author: Andrew den Hertog <andrew.denhertog@gmail.com> Date: Sun Mar 1 12:28:19 2020 +1000 Provide documentation for system message handlers
1 parent 80ffefa commit 2c19c92

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

packages/bus-core/src/handler/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,40 @@ class Handler {
7575

7676
```
7777

78+
## System and non-domain messages
79+
80+
Sometimes you want to subscribe your application to messages that it doesn't own or publish. This is common when integrating with external systems that publish messages that don't conform to the structure of those defined in `@node-ts/bus-messages`. In order to subscribe to and handle these types of messages, you need to provide a resolver and a topic identifier as part of your handler declaration.
81+
82+
For example, let's say we're working with AWS and want to be notified every time there's a new S3 object created in a bucket. S3 publishes these events to an existing SNS topic, and we want to subscribe our application SQS queue to it and handle these messages. The handler declaration would look like this:
83+
84+
```typescript
85+
import { S3Event } from 'aws-lambda'
86+
import { HandlesMessage, Handler } from '@node-ts/bus-core'
87+
88+
@HandlesMessage(
89+
(event: S3Event) => {
90+
// At runtime, `event` may or may not be an S3Event so we need to assert
91+
if (!Array.isArray(event.Records) || event.Records.length < 1) {
92+
return false
93+
}
94+
// Likewise we could get different types of S3Events (eg: deletions)
95+
return event.Records.some(eventsAreS3PutEvents)
96+
}, 'arn:aws:sns:us-east-1:000000000000:s3-object-created' // ARN that identifies the topic to subscribe to
97+
)
98+
export class LogS3ObjectCreatedEventHandler implements Handler<S3Event> {
99+
100+
async handle (event: S3Event): Promise<void> {
101+
console.log('New S3 object created', event)
102+
}
103+
}
104+
105+
const eventsAreS3PutEvents = (e: S3EventRecord): boolean => e.eventName === 'ObjectCreated:Put'
106+
```
107+
108+
When this handler is registered, it will automatically subscribe the application queue to the underlying topic. Note that the topic identifier is specific to the bus transport being used. Here `@node-ts/bus-sqs` is used so an SNS ARN is provided, but if you were using a different transport (eg: RabbitMQ) then you would provide a different identifier (eg: an Exchange name).
109+
110+
All messages received from the queue will be sent to the resolver. Due to the prototype/duck typing nature of Javascript, you'll need to place assertions into the resolver so that your handler only receives messages you are intending to handle.
111+
78112
## Messages that fail processing
79113

80114
When a message handling function throws an error while processing a message, the message is returned to the queue to be retried. Often the message will succeed on a subsequent retry depending on the reason for the error (eg: a core piece of infrastructure was down, an external service was unavailable, a network partition event occurred, a row lock version conflict stopped an update going through).

0 commit comments

Comments
 (0)