-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: improved the Pulsar integration docs
- Loading branch information
1 parent
1076996
commit 4cacfe4
Showing
8 changed files
with
140 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export type DomainEvent<Name extends string, Data> = Readonly<{ | ||
name: Name | ||
data: Data | ||
}> | ||
export type MedicineAssigned = DomainEvent< | ||
'MedicineAssigned', | ||
{ | ||
medicineId: string | ||
patientId: string | ||
} | ||
> | ||
export type MedicineFinished = DomainEvent< | ||
'MedicineFinished', | ||
{ | ||
medicineId: string | ||
patientId: string | ||
} | ||
> | ||
export type MedicineEvent = MedicineAssigned | MedicineFinished |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { setTimeout } from 'node:timers/promises' | ||
import { OutboxConsumer } from '../../packages/hermes-mongodb/src/typings' | ||
import { MedicineEvent } from '../events' | ||
import { ObjectId } from '../node_modules/mongodb/mongodb' | ||
|
||
export const doPublishing = async (outbox: OutboxConsumer<MedicineEvent>) => { | ||
while (true) { | ||
const medicineId = new ObjectId().toString() | ||
const patientId = new ObjectId().toString() | ||
|
||
try { | ||
await outbox.publish({ | ||
name: 'MedicineAssigned', | ||
data: { | ||
medicineId, | ||
patientId, | ||
}, | ||
}) | ||
} catch { | ||
await setTimeout(1000) | ||
continue | ||
} | ||
|
||
console.info(`Event published for medicine ${medicineId} nad patient ${patientId}.`) | ||
|
||
await setTimeout(1000) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { setTimeout } from 'node:timers/promises' | ||
import { Consumer } from '../node_modules/pulsar-client/index' | ||
|
||
export const doReceiving = async (subscription: Consumer) => { | ||
while (true) { | ||
try { | ||
const message = await subscription.receive() | ||
const event = JSON.parse(message.getData().toString('utf-8')) | ||
|
||
console.info(`Consumed event for medicine ${event.data.medicineId} and patient ${event.data.patientId}`) | ||
} catch { | ||
await setTimeout(1000) | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { createOutboxConsumer } from '../../packages/hermes-mongodb/src/outbox' | ||
import { addDisposeOnSigterm } from '../../packages/hermes/src/addDisposeOnSigterm' | ||
import { swallow } from '../../packages/hermes/src/utils' | ||
import { MedicineEvent } from '../events' | ||
import { MongoClient } from '../node_modules/mongodb/mongodb' | ||
import { Client } from '../node_modules/pulsar-client/index' | ||
import { doPublishing } from './do-publishing' | ||
import { doReceiving } from './do-receiving' | ||
import { PulsarMutex } from './pulsar-mutex' | ||
|
||
const MONGODB_URI = `mongodb://127.0.0.1:27017/?replicaSet=rs0&directConnection=true` | ||
const PULSAR_URI = `pulsar://localhost:6650` | ||
|
||
const start = async () => { | ||
const pulsarClient = new Client({ serviceUrl: PULSAR_URI }) | ||
const mutex = new PulsarMutex(pulsarClient) | ||
const producer = await pulsarClient.createProducer({ topic: `public/default/events` }) | ||
const subscription = await pulsarClient.subscribe({ topic: `public/default/events`, subscription: 'test' }) | ||
|
||
addDisposeOnSigterm(async () => { | ||
await swallow(() => mutex.unlock()) | ||
await swallow(() => subscription.close()) | ||
await swallow(() => producer.close()) | ||
await swallow(() => pulsarClient.close()) | ||
}) | ||
|
||
await mutex.lock() | ||
|
||
try { | ||
const client = new MongoClient(MONGODB_URI) | ||
const db = client.db('aid-kit') | ||
|
||
await client.connect() | ||
|
||
const outbox = createOutboxConsumer<MedicineEvent>({ | ||
client, | ||
db, | ||
publish: async (event) => { | ||
// Normally, you should choose a corresponding topic for the given event. | ||
await producer.send({ | ||
data: Buffer.from(JSON.stringify(event)), | ||
}) | ||
}, | ||
}) | ||
|
||
// Hermes automatically registers the dispose on SIGTERM | ||
await outbox.start() | ||
|
||
doPublishing(outbox).catch(console.error) | ||
doReceiving(subscription).catch(console.error) | ||
} catch (error) { | ||
console.error(error) | ||
throw error | ||
} | ||
} | ||
|
||
start() |