From 978a9daca87582673673600a268fde01719bba66 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 24 May 2024 17:13:21 +0200 Subject: [PATCH] events-backend: clean up local dev setup Signed-off-by: Patrik Oldsberg --- plugins/events-backend/dev/index.ts | 106 +++------------------------- 1 file changed, 11 insertions(+), 95 deletions(-) diff --git a/plugins/events-backend/dev/index.ts b/plugins/events-backend/dev/index.ts index 1c0d38215b..1b36751d59 100644 --- a/plugins/events-backend/dev/index.ts +++ b/plugins/events-backend/dev/index.ts @@ -20,7 +20,6 @@ import { createBackendPlugin, } from '@backstage/backend-plugin-api'; import { eventsServiceRef } from '@backstage/plugin-events-node'; -import { DefaultApiClient } from '../../events-node/src/generated'; const backend = createBackend(); @@ -34,16 +33,16 @@ backend.add( deps: { events: eventsServiceRef, logger: coreServices.logger, + rootLifecycle: coreServices.rootLifecycle, }, - async init({ events, logger }) { - // setInterval(() => { - // logger.info(`Publishing event to topic 'test'`); - // events.publish({ - // eventPayload: { foo: 'bar' }, - // topic: 'test', - // metadata: { meta: 'baz' }, - // }); - // }, 5000); + async init({ events, logger, rootLifecycle }) { + rootLifecycle.addStartupHook(async () => { + logger.info(`Publishing event to topic 'test'`); + await events.publish({ + eventPayload: { foo: 'bar' }, + topic: 'test', + }); + }); }, }); }, @@ -58,98 +57,15 @@ backend.add( deps: { events: eventsServiceRef, logger: coreServices.logger, - discovery: coreServices.discovery, - auth: coreServices.auth, - rootLifecycle: coreServices.rootLifecycle, }, - async init({ events, logger, discovery, rootLifecycle, auth }) { - events.subscribe({ + async init({ events, logger }) { + await events.subscribe({ id: 'test-1', topics: ['test'], async onEvent(event) { logger.info(`Received event: ${JSON.stringify(event, null, 2)}`); }, }); - - rootLifecycle.addStartupHook(async () => { - logger.info('Started!'); - - const client = new DefaultApiClient({ discoveryApi: discovery }); - const baseUrl = await discovery.getBaseUrl('events'); - console.log(`DEBUG: baseUrl=`, baseUrl); - const { token } = await auth.getPluginRequestToken({ - onBehalfOf: await auth.getOwnServiceCredentials(), - targetPluginId: 'events', - }); - - const subRes = await client.putSubscription( - { - path: { subscriptionId: '123' }, - body: { topics: ['test'] }, - }, - { token }, - ); - console.log( - `DEBUG: sub create req = ${subRes.status} ${subRes.statusText}`, - ); - const subRes2 = await client.putSubscription( - { - path: { subscriptionId: 'abc' }, - body: { topics: ['test'] }, - }, - { token }, - ); - console.log( - `DEBUG: sub create req = ${subRes2.status} ${subRes2.statusText}`, - ); - - const poll = async () => { - const res = await client.getSubscriptionEvents( - { - path: { subscriptionId: '123' }, - }, - { token }, - ); - - const data = res.status === 200 && (await res.json()); - console.log( - `DEBUG: sub poll req = ${res.status} ${res.statusText}`, - data, - ); - poll(); - }; - poll(); - - const poll2 = async () => { - const res = await client.getSubscriptionEvents( - { - path: { subscriptionId: 'abc' }, - }, - { token }, - ); - - const data = res.status === 200 && (await res.json()); - console.log( - `DEBUG: sub poll2 req = ${res.status} ${res.statusText}`, - data, - ); - poll2(); - }; - poll2(); - - setTimeout(() => { - console.log(`DEBUG: publishing!`); - client.postEvent( - { - body: { - event: { topic: 'test', payload: { foo: 'bar' } }, - subscriptionIds: ['123'], - }, - }, - { token }, - ); - }, 500); - }); }, }); },