chore: reworking how we do the TaskScheduler for now

Signed-off-by: blam <ben@blam.sh>

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
blam
2023-01-04 16:31:29 +01:00
parent 9ec23f2d7c
commit 875cd848fd
8 changed files with 51 additions and 70 deletions
@@ -19,6 +19,7 @@ import {
coreServices,
createServiceFactory,
} from '@backstage/backend-plugin-api';
import { ConfigReader } from '@backstage/config';
/** @public */
export const databaseFactory = createServiceFactory({
@@ -28,7 +29,16 @@ export const databaseFactory = createServiceFactory({
plugin: coreServices.pluginMetadata,
},
async factory({ config }) {
const databaseManager = DatabaseManager.fromConfig(config);
const databaseManager = config.getOptional('backend.database')
? DatabaseManager.fromConfig(config)
: DatabaseManager.fromConfig(
new ConfigReader({
backend: {
database: { client: 'better-sqlite3', connection: ':memory:' },
},
}),
);
return async ({ plugin }) => {
return databaseManager.forPlugin(plugin.getId());
};
@@ -14,10 +14,10 @@
* limitations under the License.
*/
import { loggerToWinstonLogger } from '@backstage/backend-common';
import {
coreServices,
createServiceFactory,
loggerToWinstonLogger,
} from '@backstage/backend-plugin-api';
import { TaskScheduler } from '@backstage/backend-tasks';
@@ -25,15 +25,14 @@ import { TaskScheduler } from '@backstage/backend-tasks';
export const schedulerFactory = createServiceFactory({
service: coreServices.scheduler,
deps: {
config: coreServices.config,
plugin: coreServices.pluginMetadata,
databaseManager: coreServices.database,
logger: coreServices.logger,
},
async factory({ config }) {
const taskScheduler = TaskScheduler.fromConfig(config);
async factory() {
return async ({ plugin, databaseManager, logger }) => {
return taskScheduler.forPlugin(plugin.getId(), {
return TaskScheduler.forPlugin({
pluginId: plugin.getId(),
databaseManager,
logger: loggerToWinstonLogger(logger),
});
@@ -14,7 +14,11 @@
* limitations under the License.
*/
import { DatabaseManager, getRootLogger } from '@backstage/backend-common';
import {
DatabaseManager,
getRootLogger,
PluginDatabaseManager,
} from '@backstage/backend-common';
import { Config } from '@backstage/config';
import { once } from 'lodash';
import { Duration } from 'luxon';
@@ -57,27 +61,35 @@ export class TaskScheduler {
* @returns A {@link PluginTaskScheduler} instance
*/
forPlugin(pluginId: string): PluginTaskScheduler {
const databaseFactory = once(async () => {
const databaseManager = this.databaseManager.forPlugin(pluginId);
const knex = await databaseManager.getClient();
return TaskScheduler.forPlugin({
pluginId,
databaseManager: this.databaseManager.forPlugin(pluginId),
logger: this.logger,
});
}
if (!databaseManager.migrations?.skip) {
static forPlugin(opts: {
pluginId: string;
databaseManager: PluginDatabaseManager;
logger: Logger;
}): PluginTaskScheduler {
const databaseFactory = once(async () => {
const knex = await opts.databaseManager.getClient();
if (!opts.databaseManager.migrations?.skip) {
await migrateBackendTasks(knex);
}
const janitor = new PluginTaskSchedulerJanitor({
knex,
waitBetweenRuns: Duration.fromObject({ minutes: 1 }),
logger: this.logger,
logger: opts.logger,
});
janitor.start();
return knex;
});
return new PluginTaskSchedulerImpl(
databaseFactory,
this.logger.child({ plugin: pluginId }),
);
return new PluginTaskSchedulerImpl(databaseFactory, opts.logger);
}
}
@@ -15,5 +15,4 @@
*/
export { mockTokenManagerFactory } from './mockTokenManagerService';
export { mockConfigFactory } from './mockConfigService';
export { mockDatabaseFactory } from './mockDatabaseService';
export { mockDiscoveryFactory } from './mockDiscoveryService';
@@ -1,46 +0,0 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { DatabaseManager } from '@backstage/backend-common';
import {
coreServices,
createServiceFactory,
} from '@backstage/backend-plugin-api';
import { ConfigReader } from '@backstage/config';
/** @public */
export const mockDatabaseFactory = createServiceFactory({
service: coreServices.database,
deps: {
config: coreServices.config,
plugin: coreServices.pluginMetadata,
},
async factory({ config }) {
const databaseManager = config.getOptional('backend.database')
? DatabaseManager.fromConfig(config)
: DatabaseManager.fromConfig(
new ConfigReader({
backend: {
database: { client: 'better-sqlite', connection: ':memory:' },
},
}),
);
return async ({ plugin }) => {
return databaseManager.forPlugin(plugin.getId());
};
},
});
@@ -29,7 +29,7 @@ export const mockDiscoveryFactory = createServiceFactory({
// to provide a better way to create our mockDiscoveryService.
const discovery = SingleHostDiscovery.fromConfig(
new ConfigReader({
backend: { baseUrl: 'http://localhost:7000', listen: '0.0.0.0:7000' },
backend: { baseUrl: 'http://localhost:7007', listen: '0.0.0.0' },
}),
);
@@ -171,9 +171,12 @@ describe('TestBackend', () => {
config: coreServices.config,
database: coreServices.database,
discovery: coreServices.discovery,
httpRouter: coreServices.httpRouter,
lifecycle: coreServices.lifecycle,
logger: coreServices.logger,
permissions: coreServices.permissions,
pluginMetadata: coreServices.pluginMetadata,
rootHttpRouter: coreServices.rootHttpRouter,
rootLifecycle: coreServices.rootLifecycle,
rootLogger: coreServices.rootLogger,
scheduler: coreServices.scheduler,
@@ -181,7 +184,7 @@ describe('TestBackend', () => {
urlReader: coreServices.urlReader,
},
async init(deps) {
expect(Object.keys(deps)).toHaveLength(12);
expect(Object.keys(deps)).toHaveLength(14);
expect(Object.values(deps)).not.toContain(undefined);
},
});
@@ -25,6 +25,9 @@ import {
permissionsFactory,
schedulerFactory,
urlReaderFactory,
databaseFactory,
rootHttpRouterFactory,
httpRouterFactory,
} from '@backstage/backend-app-api';
import {
@@ -37,7 +40,6 @@ import {
import {
mockConfigFactory,
mockDatabaseFactory,
mockTokenManagerFactory,
mockDiscoveryFactory,
} from '../implementations';
@@ -68,16 +70,18 @@ export interface TestBackendOptions<
const defaultServiceFactories = [
cacheFactory(),
mockConfigFactory(),
mockDatabaseFactory(),
mockDiscoveryFactory(),
databaseFactory(),
httpRouterFactory(),
lifecycleFactory(),
loggerFactory(),
mockConfigFactory(),
mockDiscoveryFactory(),
mockTokenManagerFactory(),
permissionsFactory(),
rootHttpRouterFactory(),
rootLifecycleFactory(),
rootLoggerFactory(),
schedulerFactory(),
mockTokenManagerFactory(),
urlReaderFactory(),
];