events-backend: EventHub -> EventBus
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
+10
-10
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { EventParams } from '@backstage/plugin-events-node';
|
||||
import { EventHubStore } from './types';
|
||||
import { EventBusStore } from './types';
|
||||
import { Knex } from 'knex';
|
||||
import {
|
||||
DatabaseService,
|
||||
@@ -69,7 +69,7 @@ interface InternalDbConnection {
|
||||
}
|
||||
|
||||
// This internal class manages a single connection to the database that all listeners share
|
||||
class DatabaseEventHubListener {
|
||||
class DatabaseEventBusListener {
|
||||
readonly #client: InternalDbClient;
|
||||
readonly #logger: LoggerService;
|
||||
|
||||
@@ -84,7 +84,7 @@ class DatabaseEventHubListener {
|
||||
|
||||
constructor(client: InternalDbClient, logger: LoggerService) {
|
||||
this.#client = client;
|
||||
this.#logger = logger.child({ type: 'DatabaseEventHubListener' });
|
||||
this.#logger = logger.child({ type: 'DatabaseEventBusListener' });
|
||||
}
|
||||
|
||||
async listen(
|
||||
@@ -187,16 +187,16 @@ class DatabaseEventHubListener {
|
||||
}
|
||||
}
|
||||
|
||||
export class DatabaseEventHubStore implements EventHubStore {
|
||||
export class DatabaseEventBusStore implements EventBusStore {
|
||||
static async create(options: {
|
||||
database: DatabaseService;
|
||||
logger: LoggerService;
|
||||
}): Promise<DatabaseEventHubStore> {
|
||||
}): Promise<DatabaseEventBusStore> {
|
||||
const db = await options.database.getClient();
|
||||
|
||||
if (db.client.config.client !== 'pg') {
|
||||
throw new Error(
|
||||
`DatabaseEventHubStore only supports PostgreSQL, got '${db.client.config.client}'`,
|
||||
`DatabaseEventBusStore only supports PostgreSQL, got '${db.client.config.client}'`,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -204,20 +204,20 @@ export class DatabaseEventHubStore implements EventHubStore {
|
||||
await db.migrate.latest({
|
||||
directory: migrationsDir,
|
||||
});
|
||||
options.logger.info('DatabaseEventHubStore migrations ran successfully');
|
||||
options.logger.info('DatabaseEventBusStore migrations ran successfully');
|
||||
}
|
||||
|
||||
return new DatabaseEventHubStore(db, options.logger);
|
||||
return new DatabaseEventBusStore(db, options.logger);
|
||||
}
|
||||
|
||||
readonly #db: Knex;
|
||||
readonly #logger: LoggerService;
|
||||
readonly #listener: DatabaseEventHubListener;
|
||||
readonly #listener: DatabaseEventBusListener;
|
||||
|
||||
private constructor(db: Knex, logger: LoggerService) {
|
||||
this.#db = db;
|
||||
this.#logger = logger;
|
||||
this.#listener = new DatabaseEventHubListener(db.client, logger);
|
||||
this.#listener = new DatabaseEventBusListener(db.client, logger);
|
||||
}
|
||||
|
||||
async publish(options: {
|
||||
+2
-2
@@ -14,11 +14,11 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { EventParams } from '@backstage/plugin-events-node';
|
||||
import { EventHubStore } from './types';
|
||||
import { EventBusStore } from './types';
|
||||
|
||||
const MAX_BATCH_SIZE = 5;
|
||||
|
||||
export class MemoryEventHubStore implements EventHubStore {
|
||||
export class MemoryEventBusStore implements EventBusStore {
|
||||
#events = new Array<
|
||||
EventParams & { seq: number; subscriberIds: Set<string> }
|
||||
>();
|
||||
+7
-7
@@ -22,9 +22,9 @@ import {
|
||||
import { Handler } from 'express';
|
||||
import Router from 'express-promise-router';
|
||||
import { createOpenApiRouter } from '../../schema/openapi.generated';
|
||||
import { MemoryEventHubStore } from './MemoryEventHubStore';
|
||||
import { DatabaseEventHubStore } from './DatabaseEventHubStore';
|
||||
import { EventHubStore } from './types';
|
||||
import { MemoryEventBusStore } from './MemoryEventBusStore';
|
||||
import { DatabaseEventBusStore } from './DatabaseEventBusStore';
|
||||
import { EventBusStore } from './types';
|
||||
import { EventParams } from '@backstage/plugin-events-node';
|
||||
|
||||
export async function createEventBusRouter(options: {
|
||||
@@ -33,20 +33,20 @@ export async function createEventBusRouter(options: {
|
||||
httpAuth: HttpAuthService;
|
||||
}): Promise<Handler> {
|
||||
const { database, httpAuth } = options;
|
||||
const logger = options.logger.child({ type: 'EventHub' });
|
||||
const logger = options.logger.child({ type: 'EventBus' });
|
||||
const router = Router();
|
||||
|
||||
let store: EventHubStore;
|
||||
let store: EventBusStore;
|
||||
const db = await database.getClient();
|
||||
if (db.client.config.client === 'pg') {
|
||||
logger.info('Database is PostgreSQL, using database store');
|
||||
store = await DatabaseEventHubStore.create({
|
||||
store = await DatabaseEventBusStore.create({
|
||||
database,
|
||||
logger,
|
||||
});
|
||||
} else {
|
||||
logger.info('Database is not PostgreSQL, using memory store');
|
||||
store = new MemoryEventHubStore();
|
||||
store = new MemoryEventBusStore();
|
||||
}
|
||||
|
||||
const apiRouter = await createOpenApiRouter();
|
||||
@@ -14,4 +14,4 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export { createEventBusRouter } from './EventHub';
|
||||
export { createEventBusRouter } from './createEventBusRouter';
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
import { EventParams } from '@backstage/plugin-events-node';
|
||||
|
||||
export type EventHubStore = {
|
||||
export type EventBusStore = {
|
||||
publish(options: {
|
||||
params: EventParams;
|
||||
subscriberIds: string[];
|
||||
|
||||
Reference in New Issue
Block a user