Merge pull request #15658 from backstage/rugvip/lifecyclelog

backend-plugin-api: update lifecycle service to use a logger instead of labels
This commit is contained in:
Johan Haals
2023-01-10 14:20:13 +01:00
committed by GitHub
7 changed files with 20 additions and 15 deletions
+1 -1
View File
@@ -2,4 +2,4 @@
'@backstage/backend-plugin-api': patch
---
Added `RootLifecycleService` and `rootLifecycleServiceRef`, as well as added a `labels` option to the existing `LifecycleServiceShutdownHook`.
Added `RootLifecycleService` and `rootLifecycleServiceRef`, as well as added a `logger` option to the existing `LifecycleServiceShutdownHook`.
@@ -25,17 +25,19 @@ import {
export const lifecycleFactory = createServiceFactory({
service: coreServices.lifecycle,
deps: {
logger: coreServices.logger,
rootLifecycle: coreServices.rootLifecycle,
pluginMetadata: coreServices.pluginMetadata,
},
async factory({ rootLifecycle }) {
return async ({ pluginMetadata }) => {
return async ({ logger, pluginMetadata }) => {
const plugin = pluginMetadata.getId();
return {
addShutdownHook(options: LifecycleServiceShutdownHook): void {
rootLifecycle.addShutdownHook({
...options,
labels: { ...options?.labels, plugin },
logger: options.logger?.child({ plugin }) ?? logger,
});
},
};
@@ -108,7 +108,7 @@ export const rootHttpRouterFactory = createServiceFactory({
async fn() {
await server.stop();
},
labels: { service: 'rootHttpRouter' },
logger,
});
await server.start();
@@ -22,7 +22,6 @@ describe('lifecycleService', () => {
const service = new BackendLifecycleImpl(getVoidLogger());
const hook = jest.fn();
service.addShutdownHook({
labels: { plugin: 'test' },
fn: async () => {
hook();
},
@@ -37,7 +36,6 @@ describe('lifecycleService', () => {
it('should not throw errors', async () => {
const service = new BackendLifecycleImpl(getVoidLogger());
service.addShutdownHook({
labels: { plugin: 'test' },
fn: async () => {
throw new Error('oh no');
},
@@ -13,18 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { loggerToWinstonLogger } from '@backstage/backend-common';
import {
createServiceFactory,
coreServices,
LifecycleServiceShutdownHook,
RootLifecycleService,
LoggerService,
} from '@backstage/backend-plugin-api';
import { Logger } from 'winston';
const CALLBACKS = ['SIGTERM', 'SIGINT', 'beforeExit'];
export class BackendLifecycleImpl implements RootLifecycleService {
constructor(private readonly logger: Logger) {
constructor(private readonly logger: LoggerService) {
CALLBACKS.map(signal => process.on(signal, () => this.shutdown()));
}
@@ -44,11 +44,12 @@ export class BackendLifecycleImpl implements RootLifecycleService {
this.logger.info(`Running ${this.#shutdownTasks.length} shutdown tasks...`);
await Promise.all(
this.#shutdownTasks.map(async hook => {
const { logger = this.logger } = hook;
try {
await hook.fn();
this.logger.info(`Shutdown hook succeeded`, hook.labels);
logger.info(`Shutdown hook succeeded`);
} catch (error) {
this.logger.error(`Shutdown hook failed, ${error}`, hook.labels);
logger.error(`Shutdown hook failed, ${error}`);
}
}),
);
@@ -64,6 +65,6 @@ export const rootLifecycleFactory = createServiceFactory({
logger: coreServices.rootLogger,
},
async factory({ logger }) {
return new BackendLifecycleImpl(loggerToWinstonLogger(logger));
return new BackendLifecycleImpl(logger);
},
});
+1 -1
View File
@@ -190,7 +190,7 @@ export interface LifecycleService {
// @public (undocumented)
export type LifecycleServiceShutdownHook = {
fn: () => void | Promise<void>;
labels?: Record<string, string>;
logger?: LoggerService;
};
// @public
@@ -14,14 +14,18 @@
* limitations under the License.
*/
import { LoggerService } from './LoggerService';
/**
* @public
**/
export type LifecycleServiceShutdownHook = {
fn: () => void | Promise<void>;
/** Labels to help identify the shutdown hook */
labels?: Record<string, string>;
/**
* Optional {@link LoggerService} that will be used for logging instead of the default logger.
*/
logger?: LoggerService;
};
/**