events-backend: clean up local dev setup

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-05-24 17:13:21 +02:00
parent ea33c1240a
commit 978a9daca8
+11 -95
View File
@@ -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);
});
},
});
},