From 0393323d176c6cf1caa5f717e5e3ca687587610e Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sat, 25 May 2024 09:38:44 +0200 Subject: [PATCH] events-backend: more elaborate local development setup with multiple backends Signed-off-by: Patrik Oldsberg --- plugins/events-backend/dev/index.ts | 100 ++++++++++++++++++++++++---- plugins/events-backend/package.json | 1 + yarn.lock | 1 + 3 files changed, 90 insertions(+), 12 deletions(-) diff --git a/plugins/events-backend/dev/index.ts b/plugins/events-backend/dev/index.ts index 1b36751d59..b629cbf8d0 100644 --- a/plugins/events-backend/dev/index.ts +++ b/plugins/events-backend/dev/index.ts @@ -14,18 +14,60 @@ * limitations under the License. */ +import { WinstonLogger } from '@backstage/backend-app-api'; import { createBackend } from '@backstage/backend-defaults'; import { coreServices, createBackendPlugin, + createServiceFactory, } from '@backstage/backend-plugin-api'; +import { mockServices } from '@backstage/backend-test-utils'; import { eventsServiceRef } from '@backstage/plugin-events-node'; -const backend = createBackend(); +function makeBackend(port: number) { + const backend = createBackend(); -backend.add(import('../src/alpha')); + backend.add( + mockServices.rootConfig.factory({ + data: { + backend: { + baseUrl: `http://localhost:${port}`, + listen: { port }, + database: { + client: 'pg', + connection: { + host: 'localhost', + port: 5432, + user: process.env.USER || 'postgres', + }, + }, + }, + }, + }), + ); + backend.add( + createServiceFactory({ + service: coreServices.rootLogger, + deps: {}, + async factory() { + return WinstonLogger.create({ + meta: { + service: 'backstage', + port, + }, + // level: 'debug', + format: WinstonLogger.colorFormat(), + }); + }, + }), + ); + backend.add(import('../src/alpha')); -backend.add( + return backend; +} + +const backend7008 = makeBackend(7008); +backend7008.add( createBackendPlugin({ pluginId: 'producer', register(reg) { @@ -37,19 +79,29 @@ backend.add( }, async init({ events, logger, rootLifecycle }) { rootLifecycle.addStartupHook(async () => { - logger.info(`Publishing event to topic 'test'`); - await events.publish({ - eventPayload: { foo: 'bar' }, - topic: 'test', - }); + const publish = () => { + logger.info(`Publishing event to topic 'test'`); + events + .publish({ + eventPayload: { foo: 'bar' }, + topic: 'test', + }) + .catch(error => { + logger.error(`Failed to publish event from producer`, error); + }); + }; + publish(); + setInterval(publish, 10000); }); }, }); }, }), ); +backend7008.start(); -backend.add( +const backend7009 = makeBackend(7009); +backend7009.add( createBackendPlugin({ pluginId: 'consumer', register(reg) { @@ -60,10 +112,10 @@ backend.add( }, async init({ events, logger }) { await events.subscribe({ - id: 'test-1', + id: 'test', topics: ['test'], async onEvent(event) { - logger.info(`Received event: ${JSON.stringify(event, null, 2)}`); + logger.info(`Received event: ${JSON.stringify(event)}`); }, }); }, @@ -71,5 +123,29 @@ backend.add( }, }), ); +backend7009.start(); -backend.start(); +const backend7010 = makeBackend(7010); +backend7010.add( + createBackendPlugin({ + pluginId: 'consumer', + register(reg) { + reg.registerInit({ + deps: { + events: eventsServiceRef, + logger: coreServices.logger, + }, + async init({ events, logger }) { + await events.subscribe({ + id: 'test', + topics: ['test'], + async onEvent(event) { + logger.info(`Received event: ${JSON.stringify(event)}`); + }, + }); + }, + }); + }, + }), +); +backend7010.start(); diff --git a/plugins/events-backend/package.json b/plugins/events-backend/package.json index f7a5ecc0c2..06addb0546 100644 --- a/plugins/events-backend/package.json +++ b/plugins/events-backend/package.json @@ -67,6 +67,7 @@ "winston": "^3.2.1" }, "devDependencies": { + "@backstage/backend-app-api": "workspace:^", "@backstage/backend-defaults": "workspace:^", "@backstage/backend-test-utils": "workspace:^", "@backstage/cli": "workspace:^", diff --git a/yarn.lock b/yarn.lock index 58c69ee093..4feebf926f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6137,6 +6137,7 @@ __metadata: version: 0.0.0-use.local resolution: "@backstage/plugin-events-backend@workspace:plugins/events-backend" dependencies: + "@backstage/backend-app-api": "workspace:^" "@backstage/backend-common": ^0.25.0 "@backstage/backend-defaults": "workspace:^" "@backstage/backend-openapi-utils": "workspace:^"