From d327896ecbb5524f3c413dcdfe46c5ceadc8daa1 Mon Sep 17 00:00:00 2001 From: Andre Wanlin Date: Wed, 8 Feb 2023 17:08:51 -0600 Subject: [PATCH] Refactored createRouter args Signed-off-by: Andre Wanlin --- packages/backend/src/plugins/linguist.ts | 11 +------- plugins/linguist-backend/README.md | 25 +++-------------- plugins/linguist-backend/api-report.md | 15 ++++++++--- .../src/service/router.test.ts | 26 ++++++++++++------ .../linguist-backend/src/service/router.ts | 27 +++++++++---------- .../src/service/standaloneServer.ts | 24 ++++++++++++----- 6 files changed, 64 insertions(+), 64 deletions(-) diff --git a/packages/backend/src/plugins/linguist.ts b/packages/backend/src/plugins/linguist.ts index 85bf3a72ec..948ca7fbb5 100644 --- a/packages/backend/src/plugins/linguist.ts +++ b/packages/backend/src/plugins/linguist.ts @@ -28,14 +28,5 @@ export default async function createPlugin( initialDelay: { seconds: 15 }, }; - return createRouter({ - logger: env.logger, - config: env.config, - reader: env.reader, - discovery: env.discovery, - database: env.database, - scheduler: env.scheduler, - schedule: schedule, - age: { days: 30 }, - }); + return createRouter({ schedule: schedule, age: { days: 30 } }, { ...env }); } diff --git a/plugins/linguist-backend/README.md b/plugins/linguist-backend/README.md index 0ad585dedd..c42112b694 100644 --- a/plugins/linguist-backend/README.md +++ b/plugins/linguist-backend/README.md @@ -36,15 +36,7 @@ Here's how to get the backend up and running: initialDelay: { seconds: 90 }, }; - return createRouter({ - logger: env.logger, - config: env.config, - reader: env.reader, - discovery: env.discovery, - database: env.database, - scheduler: env.scheduler, - schedule: schedule, - }); + return createRouter({ schedule: schedule }, { ...env }); } ``` @@ -79,19 +71,10 @@ const schedule: TaskScheduleDefinition = { }; ``` -The default setup will only generate the language breakdown for entities with the linguist annotation that have not been generated yet. If you want this process to also refresh the data you can do so by adding the `age` in your `packages/backend/src/plugins/linguist.ts`: +The default setup will only generate the language breakdown for entities with the linguist annotation that have not been generated yet. If you want this process to also refresh the data you can do so by adding the `age` (as a `HumanDuration`) in your `packages/backend/src/plugins/linguist.ts` when you call `createRouter`: -```diff - return createRouter({ - logger: env.logger, - config: env.config, - reader: env.reader, - discovery: env.discovery, - database: env.database, - scheduler: env.scheduler, - schedule: schedule, -+ age: { days: 15 }, - }); +```ts +return createRouter({ schedule: schedule, age: { days: 30 } }, { ...env }); ``` With the `age` setup like this if the language breakdown is older than 15 days it will get regenerated. It's recommended that if you choose to use this configuration to set it to a large value - 30, 90, or 180 - as this data generally does not change drastically. diff --git a/plugins/linguist-backend/api-report.md b/plugins/linguist-backend/api-report.md index 3524c02611..3fd2823815 100644 --- a/plugins/linguist-backend/api-report.md +++ b/plugins/linguist-backend/api-report.md @@ -19,7 +19,10 @@ import { TaskScheduleDefinition } from '@backstage/backend-tasks'; import { UrlReader } from '@backstage/backend-common'; // @public (undocumented) -export function createRouter(options: RouterOptions): Promise; +export function createRouter( + pluginOptions: PluginOptions, + routerOptions: RouterOptions, +): Promise; // @public (undocumented) export class LinguistBackendApi { @@ -65,9 +68,15 @@ export interface LinguistBackendStore { } // @public (undocumented) -export interface RouterOptions { +export interface PluginOptions { // (undocumented) age?: HumanDuration; + // (undocumented) + schedule?: TaskScheduleDefinition; +} + +// @public (undocumented) +export interface RouterOptions { // (undocumented) config: Config; // (undocumented) @@ -81,8 +90,6 @@ export interface RouterOptions { // (undocumented) reader: UrlReader; // (undocumented) - schedule?: TaskScheduleDefinition; - // (undocumented) scheduler?: PluginTaskScheduler; } diff --git a/plugins/linguist-backend/src/service/router.test.ts b/plugins/linguist-backend/src/service/router.test.ts index 0e20987a43..1de162c97a 100644 --- a/plugins/linguist-backend/src/service/router.test.ts +++ b/plugins/linguist-backend/src/service/router.test.ts @@ -26,6 +26,7 @@ import request from 'supertest'; import { ConfigReader } from '@backstage/config'; import { createRouter } from './router'; import { LinguistBackendApi } from '../api'; +import { TaskScheduleDefinition } from '@backstage/backend-tasks'; function createDatabase(): PluginDatabaseManager { return DatabaseManager.fromConfig( @@ -52,19 +53,28 @@ const mockUrlReader = UrlReaders.default({ config: new ConfigReader({}), }); +const schedule: TaskScheduleDefinition = { + frequency: { minutes: 2 }, + timeout: { minutes: 15 }, + initialDelay: { seconds: 15 }, +}; + describe('createRouter', () => { let linguistBackendApi: jest.Mocked; let app: express.Express; beforeAll(async () => { - const router = await createRouter({ - linguistBackendApi, - config: new ConfigReader({}), - discovery: testDiscovery, - database: createDatabase(), - reader: mockUrlReader, - logger: getVoidLogger(), - }); + const router = await createRouter( + { schedule: schedule, age: { days: 30 } }, + { + linguistBackendApi, + config: new ConfigReader({}), + discovery: testDiscovery, + database: createDatabase(), + reader: mockUrlReader, + logger: getVoidLogger(), + }, + ); app = express().use(router); }); diff --git a/plugins/linguist-backend/src/service/router.ts b/plugins/linguist-backend/src/service/router.ts index 122cb939e3..8cf426f2b3 100644 --- a/plugins/linguist-backend/src/service/router.ts +++ b/plugins/linguist-backend/src/service/router.ts @@ -32,6 +32,12 @@ import { } from '@backstage/backend-tasks'; import { HumanDuration } from '@backstage/types'; +/** @public */ +export interface PluginOptions { + schedule?: TaskScheduleDefinition; + age?: HumanDuration; +} + /** @public */ export interface RouterOptions { linguistBackendApi?: LinguistBackendApi; @@ -41,31 +47,24 @@ export interface RouterOptions { database: PluginDatabaseManager; discovery: PluginEndpointDiscovery; scheduler?: PluginTaskScheduler; - schedule?: TaskScheduleDefinition; - age?: HumanDuration; } /** @public */ export async function createRouter( - options: RouterOptions, + pluginOptions: PluginOptions, + routerOptions: RouterOptions, ): Promise { - const { - config, - logger, - reader, - database, - discovery, - scheduler, - schedule, - age, - } = options; + const { schedule, age } = pluginOptions; + + const { config, logger, reader, database, discovery, scheduler } = + routerOptions; const linguistBackendStore = await LinguistBackendDatabase.create( await database.getClient(), ); const linguistBackendApi = - options.linguistBackendApi || + routerOptions.linguistBackendApi || new LinguistBackendApi( config, logger, diff --git a/plugins/linguist-backend/src/service/standaloneServer.ts b/plugins/linguist-backend/src/service/standaloneServer.ts index 5578c20e95..874806e788 100644 --- a/plugins/linguist-backend/src/service/standaloneServer.ts +++ b/plugins/linguist-backend/src/service/standaloneServer.ts @@ -25,6 +25,7 @@ import { Server } from 'http'; import { Logger } from 'winston'; import { createRouter } from './router'; import knexFactory from 'knex'; +import { TaskScheduleDefinition } from '@backstage/backend-tasks'; export interface ServerOptions { port: number; @@ -51,14 +52,23 @@ export async function startStandaloneServer( return knex; }); + const schedule: TaskScheduleDefinition = { + frequency: { minutes: 2 }, + timeout: { minutes: 15 }, + initialDelay: { seconds: 15 }, + }; + logger.debug('Starting application server...'); - const router = await createRouter({ - database: { getClient: async () => db }, - config, - discovery: SingleHostDiscovery.fromConfig(config), - reader: UrlReaders.default({ logger, config }), - logger, - }); + const router = await createRouter( + { schedule: schedule, age: { days: 30 } }, + { + database: { getClient: async () => db }, + config, + discovery: SingleHostDiscovery.fromConfig(config), + reader: UrlReaders.default({ logger, config }), + logger, + }, + ); let service = createServiceBuilder(module) .setPort(options.port)