backend-app-api: wrap returned result
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -45,7 +45,7 @@ The `Backend.start()` method returns a `BackendStartupResult` with detailed succ
|
||||
|
||||
```ts
|
||||
backend.start(
|
||||
result => {
|
||||
({ result }) => {
|
||||
console.log(`Backend startup result: ${JSON.stringify(result, null, 2)}`);
|
||||
},
|
||||
error => {
|
||||
|
||||
@@ -18,7 +18,9 @@ export interface Backend {
|
||||
}>,
|
||||
): void;
|
||||
// (undocumented)
|
||||
start(): Promise<BackendStartupResult>;
|
||||
start(): Promise<{
|
||||
result: BackendStartupResult;
|
||||
}>;
|
||||
// (undocumented)
|
||||
stop(): Promise<void>;
|
||||
}
|
||||
@@ -35,8 +37,8 @@ export class BackendStartupError extends CustomErrorBase {
|
||||
// @public
|
||||
export interface BackendStartupResult {
|
||||
beginAt: Date;
|
||||
outcome: 'success' | 'failure';
|
||||
plugins: PluginStartupResult[];
|
||||
result: 'success' | 'failure';
|
||||
resultAt: Date;
|
||||
}
|
||||
|
||||
|
||||
@@ -1298,7 +1298,7 @@ describe('BackendInitializer', () => {
|
||||
}),
|
||||
);
|
||||
|
||||
const result = await init.start();
|
||||
const { result } = await init.start();
|
||||
|
||||
expect(result.plugins).toHaveLength(2);
|
||||
expect(result.plugins[0]).toMatchObject({
|
||||
@@ -1503,7 +1503,7 @@ describe('BackendInitializer', () => {
|
||||
}),
|
||||
);
|
||||
|
||||
const result = await init.start();
|
||||
const { result } = await init.start();
|
||||
|
||||
expect(result.plugins).toHaveLength(2);
|
||||
expect(result.plugins[0]).toMatchObject({
|
||||
@@ -1574,7 +1574,7 @@ describe('BackendInitializer', () => {
|
||||
}),
|
||||
);
|
||||
|
||||
const result = await init.start();
|
||||
const { result } = await init.start();
|
||||
|
||||
expect(result.plugins).toHaveLength(1);
|
||||
expect(result.plugins[0]).toMatchObject({
|
||||
@@ -1667,7 +1667,7 @@ describe('BackendInitializer', () => {
|
||||
}),
|
||||
);
|
||||
|
||||
const result = await init.start();
|
||||
const { result } = await init.start();
|
||||
|
||||
expect(result.plugins[0].modules).toHaveLength(3);
|
||||
expect(result.plugins[0].modules[0]).toMatchObject({
|
||||
@@ -1698,7 +1698,7 @@ describe('BackendInitializer', () => {
|
||||
}),
|
||||
);
|
||||
|
||||
const result = await init.start();
|
||||
const { result } = await init.start();
|
||||
|
||||
expect(result.plugins).toHaveLength(1);
|
||||
expect(result.plugins[0]).toMatchObject({
|
||||
@@ -1823,7 +1823,7 @@ describe('BackendInitializer', () => {
|
||||
}),
|
||||
);
|
||||
|
||||
const result = await init.start();
|
||||
const { result } = await init.start();
|
||||
|
||||
expect(result.plugins).toHaveLength(1);
|
||||
expect(result.plugins[0].pluginId).toBe('plugin1');
|
||||
|
||||
@@ -150,7 +150,7 @@ function createRootInstanceMetadataServiceFactory(
|
||||
}
|
||||
|
||||
export class BackendInitializer {
|
||||
#startPromise?: Promise<BackendStartupResult>;
|
||||
#startPromise?: Promise<{ result: BackendStartupResult }>;
|
||||
#stopPromise?: Promise<void>;
|
||||
#registrations = new Array<InternalBackendRegistrations>();
|
||||
#extensionPoints = new Map<string, { impl: unknown; pluginId: string }>();
|
||||
@@ -226,7 +226,7 @@ export class BackendInitializer {
|
||||
}
|
||||
}
|
||||
|
||||
async start(): Promise<BackendStartupResult> {
|
||||
async start(): Promise<{ result: BackendStartupResult }> {
|
||||
if (this.#startPromise) {
|
||||
throw new Error('Backend has already started');
|
||||
}
|
||||
@@ -240,7 +240,7 @@ export class BackendInitializer {
|
||||
return await this.#startPromise;
|
||||
}
|
||||
|
||||
async #doStart(): Promise<BackendStartupResult> {
|
||||
async #doStart(): Promise<{ result: BackendStartupResult }> {
|
||||
this.#serviceRegistry.checkForCircularDeps();
|
||||
|
||||
for (const feature of this.#registeredFeatures) {
|
||||
@@ -432,16 +432,16 @@ export class BackendInitializer {
|
||||
);
|
||||
});
|
||||
|
||||
const startupResult = resultCollector.finalize();
|
||||
if (startupResult.result === 'failure') {
|
||||
throw new BackendStartupError(startupResult);
|
||||
const result = resultCollector.finalize();
|
||||
if (result.outcome === 'failure') {
|
||||
throw new BackendStartupError(result);
|
||||
}
|
||||
|
||||
// Once all plugins and modules have been initialized, we can signal that the backend has started up successfully
|
||||
const lifecycleService = await this.#getRootLifecycleImpl();
|
||||
await lifecycleService.startup();
|
||||
|
||||
return startupResult;
|
||||
return { result };
|
||||
}
|
||||
|
||||
// It's fine to call .stop() multiple times, which for example can happen with manual stop + process exit
|
||||
|
||||
@@ -34,7 +34,7 @@ export class BackstageBackend implements Backend {
|
||||
}
|
||||
}
|
||||
|
||||
async start(): Promise<BackendStartupResult> {
|
||||
async start(): Promise<{ result: BackendStartupResult }> {
|
||||
return await this.#initializer.start();
|
||||
}
|
||||
|
||||
|
||||
@@ -170,7 +170,7 @@ export function createInitializationResultCollector(options: {
|
||||
return {
|
||||
beginAt,
|
||||
resultAt: new Date(),
|
||||
result: hasDisallowedFailures ? 'failure' : 'success',
|
||||
outcome: hasDisallowedFailures ? 'failure' : 'success',
|
||||
plugins: pluginResults,
|
||||
};
|
||||
},
|
||||
|
||||
@@ -26,7 +26,7 @@ import {
|
||||
*/
|
||||
export interface Backend {
|
||||
add(feature: BackendFeature | Promise<{ default: BackendFeature }>): void;
|
||||
start(): Promise<BackendStartupResult>;
|
||||
start(): Promise<{ result: BackendStartupResult }>;
|
||||
stop(): Promise<void>;
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ export interface BackendStartupResult {
|
||||
*/
|
||||
plugins: PluginStartupResult[];
|
||||
/**
|
||||
* The result of the backend startup.
|
||||
* The outcome of the backend startup.
|
||||
*/
|
||||
result: 'success' | 'failure';
|
||||
outcome: 'success' | 'failure';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user