Skip to content

Commit 591288b

Browse files
committed
matching: expect AUS_ISTFAHRT_2 stream & consumer to be created by the user 💥📝
1 parent eb1c170 commit 591288b

File tree

2 files changed

+67
-33
lines changed

2 files changed

+67
-33
lines changed

lib/match.js

+21-33
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
import {createLogger} from './logger.js'
1515
import {register} from './metrics.js'
1616
import {
17-
AckPolicy as NatsAckPolicy,
1817
jsonCodec as natsJson,
1918
} from './nats.js'
2019
import {
@@ -68,13 +67,13 @@ const runGtfsMatching = async (cfg, opt = {}) => {
6867
ok(natsJetstreamManager)
6968

7069
const {
71-
natsConsumerDurableName,
70+
natsConsumerName,
7271
natsAckWait, // in milliseconds
7372
matchConcurrency,
7473
} = {
75-
natsConsumerDurableName: process.env.MATCHING_CONSUMER_DURABLE_NAME
76-
? process.env.MATCHING_CONSUMER_DURABLE_NAME
77-
: NATS_JETSTREAM_AUS_ISTFAHRT_STREAM_NAME + '_' + Math.random().toString(16).slice(2, 6),
74+
natsConsumerName: process.env.MATCHING_CONSUMER_NAME
75+
? process.env.MATCHING_CONSUMER_NAME
76+
: 'gtfs-rt-feed',
7877
natsAckWait: 60 * 1000, // 60 seconds
7978
matchConcurrency: process.env.MATCHING_CONCURRENCY
8079
? parseInt(process.env.MATCHING_CONCURRENCY)
@@ -315,19 +314,13 @@ const runGtfsMatching = async (cfg, opt = {}) => {
315314
}
316315

317316
{
318-
// todo: shouldn't this be done upfront by the person deploying the service?
319317
{
320-
// create/update NATS JetStream stream for AUS IstFahrts
321-
const streamInfo = await natsJetstreamManager.streams.add({
322-
name: NATS_JETSTREAM_AUS_ISTFAHRT_STREAM_NAME,
323-
subjects: [
324-
AUS_ISTFAHRT_TOPIC_PREFIX + '>',
325-
],
326-
// todo: limits?
327-
})
318+
// query details of the NATS JetStream stream for AUS IstFahrts
319+
const stream = await natsJetstreamClient.streams.get(NATS_JETSTREAM_AUS_ISTFAHRT_STREAM_NAME)
320+
const streamInfo = await stream.info()
328321
serviceLogger.debug({
329322
streamInfo,
330-
}, 'created/re-used NATS JetStream stream for AUS IstFahrts')
323+
}, 'using NATS JetStream stream for AUS IstFahrts')
331324
}
332325
{
333326
// create/update NATS JetStream stream for GTFS-RT data
@@ -343,27 +336,22 @@ const runGtfsMatching = async (cfg, opt = {}) => {
343336
}, 'created/re-used NATS JetStream stream for GTFS-RT data')
344337
}
345338

346-
// create durable NATS JetStream consumer for previously created stream
347-
const consumerInfo = await natsJetstreamManager.consumers.add(NATS_JETSTREAM_AUS_ISTFAHRT_STREAM_NAME, {
348-
ack_policy: NatsAckPolicy.Explicit,
349-
durable_name: natsConsumerDurableName,
350-
ack_wait: natsAckWait * 1000, // nats.js expects nanoseconds
351-
// todo: configure inactive_threshold?
352-
// todo: set max_ack_pending to 1 for strict ordering of messages?
353-
// todo: configure ack_wait?
339+
const istFahrtsConsumer = await natsJetstreamClient.consumers.get(
340+
NATS_JETSTREAM_AUS_ISTFAHRT_STREAM_NAME,
341+
natsConsumerName,
342+
)
354343

355-
// todo: https://nats-io.github.io/nats.deno/interfaces/ConsumerConfig.html ?
356-
357-
// todo: add trip ID to topic, consume with `DeliverLastPerSubject`? – would not work for partial IstFahrts
358-
})
359-
serviceLogger.debug({
360-
consumerInfo,
361-
}, 'created/re-used NATS JetStream consumer')
344+
{
345+
// query details of the (externally created) NATS JetStream consumer
346+
const consumerInfo = await istFahrtsConsumer.info()
347+
serviceLogger.debug({
348+
consumerInfo,
349+
}, 'using NATS JetStream consumer')
350+
}
362351

363-
const tripsConsumer = await natsJetstreamClient.consumers.get(NATS_JETSTREAM_AUS_ISTFAHRT_STREAM_NAME, consumerInfo.name)
364-
const tripsSub = await tripsConsumer.consume()
352+
const istFahrtsSub = await istFahrtsConsumer.consume()
365353
execPipe(
366-
tripsSub,
354+
istFahrtsSub,
367355

368356
// asyncBuffer workaround
369357
// see also https://github.com/iter-tools/iter-tools/issues/425#issuecomment-882875848

readme.md

+46
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,52 @@ To make sure that the connection works, use [`psql`](https://www.postgresql.org/
172172

173173
By default, `gtfs-rt-feed` will connect as `gtfs-rt-$MAJOR_VERSION` to `localhost:4222` without authentication.
174174

175+
#### create NATS stream & consumer
176+
177+
We also need to create a [NATS JetStream](https://docs.nats.io/nats-concepts/jetstream) [stream](https://docs.nats.io/nats-concepts/jetstream/streams) called `AUS_ISTFAHRT_2` that `gtfs-rt-feed` will read (unmatched) GTFS-RT messages from. This can be done using the [NATS CLI](https://github.com/nats-io/natscli):
178+
179+
```shell
180+
nats stream add \
181+
# omit this if you want to configure more details
182+
--defaults \
183+
# collect all messages published to these subjects
184+
--subjects='aus.istfahrt.>' \
185+
# acknowledge publishes
186+
--ack \
187+
# with limited storage, discard the oldest limits first
188+
--retention=limits --discard=old \
189+
--description='VDV-454 AUS IstFahrt messages' \
190+
# name of the stream
191+
AUS_ISTFAHRT_2
192+
```
193+
194+
On the `AUS_ISTFAHRT_2` stream, we create a durable [consumer]():
195+
196+
```shell
197+
nats consumer add \
198+
# omit this if you want to configure more details
199+
--defaults \
200+
# create a pull-based consumer (refer to the NATS JetStream docs)
201+
--pull \
202+
# let gtfs-rt-feed explicitly acknowledge all received messages
203+
--ack=explicit \
204+
# let the newly created consumer start with the latest messages in AUS_ISTFAHRT_2 (not all)
205+
--deliver=new \
206+
# send gtfs-rt-feed at most 200 messages at once
207+
--max-pending=200 \
208+
# when & how often to re-deliver a message that hasn't been acknowledged (usually because it couldn't be processed)
209+
--max-deliver=3 \
210+
--backoff=linear \
211+
--backoff-steps=2 \
212+
--backoff-min=15s \
213+
--backoff-max=2m \
214+
--description 'OpenDataVBB/gtfs-rt-feed' \
215+
# name of the stream
216+
AUS_ISTFAHRT_2 \
217+
# name of the consumer
218+
gtfs-rt-feed
219+
```
220+
175221
#### configure access to Redis
176222

177223
`gtfs-rt-feed` uses [`ioredis`](https://npmjs.com/package/ioredis) to connect to PostgreSQL; For details about supported environment variables and their defaults, refer to [its docs](https://github.com/redis/ioredis#readme).

0 commit comments

Comments
 (0)