remove the unneeded copy of the task scheduler interface

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2024-04-30 14:31:26 +02:00
parent ea2f38c51d
commit 4711ca38d6
4 changed files with 30 additions and 99 deletions
+22 -24
View File
@@ -2,21 +2,29 @@
"name": "@backstage/backend-defaults",
"description": "Backend defaults used by Backstage backend apps",
"version": "0.2.18",
"main": "src/index.ts",
"types": "src/index.ts",
"publishConfig": {
"access": "public",
"main": "dist/index.cjs.js",
"types": "dist/index.d.ts"
},
"backstage": {
"role": "node-library"
},
"publishConfig": {
"access": "public"
},
"keywords": [
"backstage"
],
"homepage": "https://backstage.io",
"repository": {
"type": "git",
"url": "https://github.com/backstage/backstage",
"directory": "packages/backend-defaults"
},
"license": "Apache-2.0",
"exports": {
".": "./src/index.ts",
"./scheduler": "./src/entrypoints/scheduler/index.ts",
"./package.json": "./package.json"
},
"main": "src/index.ts",
"types": "src/index.ts",
"typesVersions": {
"*": {
"scheduler": [
@@ -27,24 +35,18 @@
]
}
},
"homepage": "https://backstage.io",
"repository": {
"type": "git",
"url": "https://github.com/backstage/backstage",
"directory": "packages/backend-defaults"
},
"keywords": [
"backstage"
"files": [
"dist",
"migrations"
],
"license": "Apache-2.0",
"scripts": {
"build": "backstage-cli package build",
"clean": "backstage-cli package clean",
"lint": "backstage-cli package lint",
"test": "backstage-cli package test",
"prepack": "backstage-cli package prepack",
"postpack": "backstage-cli package postpack",
"clean": "backstage-cli package clean",
"start": "backstage-cli package start"
"start": "backstage-cli package start",
"test": "backstage-cli package test"
},
"dependencies": {
"@backstage/backend-app-api": "workspace:^",
@@ -66,9 +68,5 @@
"@backstage/backend-test-utils": "workspace:^",
"@backstage/cli": "workspace:^",
"wait-for-expect": "^3.0.2"
},
"files": [
"dist",
"migrations"
]
}
}
@@ -14,8 +14,9 @@
* limitations under the License.
*/
import { LoggerService } from '@backstage/backend-plugin-api';
import {
LoggerService,
SchedulerService,
SchedulerServiceTaskDescriptor,
SchedulerServiceTaskFunction,
SchedulerServiceTaskInvocationDefinition,
@@ -27,13 +28,13 @@ import { Knex } from 'knex';
import { Duration } from 'luxon';
import { LocalTaskWorker } from './LocalTaskWorker';
import { TaskWorker } from './TaskWorker';
import { PluginTaskScheduler, TaskSettingsV2 } from './types';
import { TaskSettingsV2 } from './types';
import { validateId } from './util';
/**
* Implements the actual task management.
*/
export class PluginTaskSchedulerImpl implements PluginTaskScheduler {
export class PluginTaskSchedulerImpl implements SchedulerService {
private readonly localTasksById = new Map<string, LocalTaskWorker>();
private readonly allScheduledTasks: SchedulerServiceTaskDescriptor[] = [];
@@ -23,13 +23,13 @@ import {
import {
LoggerService,
RootConfigService,
SchedulerService,
} from '@backstage/backend-plugin-api';
import { once } from 'lodash';
import { Duration } from 'luxon';
import { migrateBackendTasks } from '../database/migrateBackendTasks';
import { PluginTaskSchedulerImpl } from './PluginTaskSchedulerImpl';
import { PluginTaskSchedulerJanitor } from './PluginTaskSchedulerJanitor';
import { PluginTaskScheduler } from './types';
/**
* Deals with the scheduling of distributed tasks.
@@ -59,9 +59,9 @@ export class TaskScheduler {
* Instantiates a task manager instance for the given plugin.
*
* @param pluginId - The unique ID of the plugin, for example "catalog"
* @returns A {@link PluginTaskScheduler} instance
* @returns A {@link SchedulerService} instance
*/
forPlugin(pluginId: string): PluginTaskScheduler {
forPlugin(pluginId: string): SchedulerService {
return TaskScheduler.forPlugin({
pluginId,
databaseManager: this.databaseManager.forPlugin(pluginId),
@@ -73,7 +73,7 @@ export class TaskScheduler {
pluginId: string;
databaseManager: PluginDatabaseManager;
logger: LoggerService;
}): PluginTaskScheduler {
}): SchedulerService {
const databaseFactory = once(async () => {
const knex = await opts.databaseManager.getClient();
@@ -14,78 +14,10 @@
* limitations under the License.
*/
import {
SchedulerServiceTaskDescriptor,
SchedulerServiceTaskInvocationDefinition,
SchedulerServiceTaskRunner,
SchedulerServiceTaskScheduleDefinition,
} from '@backstage/backend-plugin-api';
import { CronTime } from 'cron';
import { Duration } from 'luxon';
import { z } from 'zod';
/**
* Deals with the scheduling of distributed tasks, for a given plugin.
*/
export interface PluginTaskScheduler {
/**
* Manually triggers a task by ID.
*
* If the task doesn't exist, a NotFoundError is thrown. If the task is
* currently running, a ConflictError is thrown.
*
* @param id - The task ID
*/
triggerTask(id: string): Promise<void>;
/**
* Schedules a task function for recurring runs.
*
* @remarks
*
* The `scope` task field controls whether to use coordinated exclusive
* invocation across workers, or to just coordinate within the current worker.
*
* This convenience method performs both the scheduling and invocation in one
* go.
*
* @param task - The task definition
*/
scheduleTask(
task: SchedulerServiceTaskScheduleDefinition &
SchedulerServiceTaskInvocationDefinition,
): Promise<void>;
/**
* Creates a scheduled but dormant recurring task, ready to be launched at a
* later time.
*
* @remarks
*
* This method is useful for pre-creating a schedule in outer code to be
* passed into an inner implementation, such that the outer code controls
* scheduling while inner code controls implementation.
*
* @param schedule - The task schedule
*/
createScheduledTaskRunner(
schedule: SchedulerServiceTaskScheduleDefinition,
): SchedulerServiceTaskRunner;
/**
* Returns all scheduled tasks registered to this scheduler.
*
* @remarks
*
* This method is useful for triggering tasks manually using the triggerTask
* functionality. Note that the returned tasks contain only tasks that have
* been initialized in this instance of the scheduler.
*
* @returns Scheduled tasks
*/
getScheduledTasks(): Promise<SchedulerServiceTaskDescriptor[]>;
}
function isValidOptionalDurationString(d: string | undefined): boolean {
try {
return !d || Duration.fromISO(d).isValid;