Skip to content

Commit

Permalink
Add forward-to-logger service and requester sample
Browse files Browse the repository at this point in the history
This commit adds a new service called "forward-to-logger" to the docker-compose.yml file. It also includes a new requester sample that demonstrates how to request data from another service and publish it on a different subject. This allows for forwarding data to the logger service.
  • Loading branch information
pklaschka committed Jan 5, 2024
1 parent dd755b6 commit 89e38d2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
14 changes: 14 additions & 0 deletions backend-deno/samples/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ services:
environment:
- SERVICE_NAME=sample-publisher
- DATA_SUBJECT=data.sample
forward-to-logger:
build:
context: ../
dockerfile: samples/Dockerfile
command: ["requester/mod.ts"]
depends_on:
- nats
env_file:
- .common.env
environment:
- SERVICE_NAME=forward-to-logger
- REQUEST_SUBJECT=latest.data.sample
- FREQUENCY=3000
- OUTPUT_SUBJECT=log.sample-data
logger:
build:
context: ../
Expand Down
13 changes: 13 additions & 0 deletions backend-deno/samples/requester/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Requester Sample

This sample shows how to request data from another service.

In a given frequency, the service will request the value from a given subject (with an empty request) and re-publish it on a different subject.

## Configuration Options

| Option | Description | Default |
| ------ | ----------- | ------- |
| `REQUEST_SUBJECT` | The subject to request data from. | (required) |
| `PUBLISH_SUBJECT` | The subject to publish the requested data on. | (required) |
| `FREQUENCY` | The frequency to request data from the `REQUEST_SUBJECT` in milliseconds. | `5000` |
27 changes: 27 additions & 0 deletions backend-deno/samples/requester/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { startService } from "https://deno.land/x/telestion/mod.ts";
import { z } from "https://deno.land/x/[email protected]/mod.ts";

const { messageBus, config } = await startService();

const { FREQUENCY, REQUEST_SUBJECT, OUTPUT_SUBJECT } = z.object({
FREQUENCY: z.coerce.number().positive().default(5000),
REQUEST_SUBJECT: z.string(),
OUTPUT_SUBJECT: z.string(),
}).parse(config);

console.log("Request started with config", {
FREQUENCY,
REQUEST_SUBJECT,
OUTPUT_SUBJECT,
});

setInterval(async () => {
try {
const data = await messageBus.request(REQUEST_SUBJECT);
console.log("Requester received requested data", data.data);
messageBus.publish(OUTPUT_SUBJECT, data.data);
console.log("Data forwarded to", OUTPUT_SUBJECT);
} catch (e) {
console.warn("Couldn't retrieve data", e);
}
}, FREQUENCY);

0 comments on commit 89e38d2

Please sign in to comment.