rename to TaskScheduler to reduce collision risk with other concepts
Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# @backstage/backend-tasks
|
||||
|
||||
Common distributed task management / locking library for Backstage backends.
|
||||
Common distributed task management for Backstage backends.
|
||||
|
||||
## Usage
|
||||
|
||||
@@ -15,12 +15,12 @@ yarn add @backstage/backend-tasks
|
||||
then make use of its facilities as necessary:
|
||||
|
||||
```typescript
|
||||
import { TaskManager } from '@backstage/backend-tasks';
|
||||
import { TaskScheduler } from '@backstage/backend-tasks';
|
||||
import { Duration } from 'luxon';
|
||||
|
||||
const manager = TaskManager.fromConfig(rootConfig).forPlugin('my-plugin');
|
||||
const scheduler = TaskScheduler.fromConfig(rootConfig).forPlugin('my-plugin');
|
||||
|
||||
await manager.scheduleTask({
|
||||
await scheduler.scheduleTask({
|
||||
id: 'refresh-things',
|
||||
frequency: Duration.fromObject({ minutes: 10 }),
|
||||
fn: async () => {
|
||||
|
||||
@@ -10,7 +10,7 @@ import { Duration } from 'luxon';
|
||||
import { Logger as Logger_2 } from 'winston';
|
||||
|
||||
// @public
|
||||
export interface PluginTaskManager {
|
||||
export interface PluginTaskScheduler {
|
||||
scheduleTask(task: TaskDefinition): Promise<void>;
|
||||
}
|
||||
|
||||
@@ -30,9 +30,10 @@ export type TaskFunction =
|
||||
| (() => void | Promise<void>);
|
||||
|
||||
// @public
|
||||
export class TaskManager {
|
||||
export class TaskScheduler {
|
||||
constructor(databaseManager: DatabaseManager, logger: Logger_2);
|
||||
forPlugin(pluginId: string): PluginTaskManager;
|
||||
// Warning: (ae-unresolved-link) The @link reference could not be resolved: The package "@backstage/backend-tasks" does not have an export "PluginTaskManager"
|
||||
forPlugin(pluginId: string): PluginTaskScheduler;
|
||||
// (undocumented)
|
||||
static fromConfig(
|
||||
config: Config,
|
||||
@@ -40,6 +41,6 @@ export class TaskManager {
|
||||
databaseManager?: DatabaseManager;
|
||||
logger?: Logger_2;
|
||||
},
|
||||
): TaskManager;
|
||||
): TaskScheduler;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@backstage/backend-tasks",
|
||||
"description": "Common distributed task management / locking library for Backstage backends",
|
||||
"description": "Common distributed task management library for Backstage backends",
|
||||
"version": "0.1.0",
|
||||
"main": "src/index.ts",
|
||||
"types": "src/index.ts",
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* Common distributed task management / locking library for Backstage backends
|
||||
* Common distributed task management library for Backstage backends
|
||||
*
|
||||
* @packageDocumentation
|
||||
*/
|
||||
|
||||
+2
-2
@@ -19,7 +19,7 @@ import { TestDatabaseId, TestDatabases } from '@backstage/backend-test-utils';
|
||||
import { Duration } from 'luxon';
|
||||
import waitForExpect from 'wait-for-expect';
|
||||
import { migrateBackendTasks } from '../database/migrateBackendTasks';
|
||||
import { PluginTaskManagerImpl } from './PluginTaskManagerImpl';
|
||||
import { PluginTaskSchedulerImpl } from './PluginTaskSchedulerImpl';
|
||||
|
||||
describe('PluginTaskManagerImpl', () => {
|
||||
const databases = TestDatabases.create({
|
||||
@@ -29,7 +29,7 @@ describe('PluginTaskManagerImpl', () => {
|
||||
async function init(databaseId: TestDatabaseId) {
|
||||
const knex = await databases.init(databaseId);
|
||||
await migrateBackendTasks(knex);
|
||||
const manager = new PluginTaskManagerImpl(
|
||||
const manager = new PluginTaskSchedulerImpl(
|
||||
async () => knex,
|
||||
getVoidLogger(),
|
||||
);
|
||||
+2
-2
@@ -17,13 +17,13 @@
|
||||
import { Knex } from 'knex';
|
||||
import { Logger } from 'winston';
|
||||
import { TaskWorker } from './TaskWorker';
|
||||
import { PluginTaskManager, TaskDefinition } from './types';
|
||||
import { PluginTaskScheduler, TaskDefinition } from './types';
|
||||
import { validateId } from './util';
|
||||
|
||||
/**
|
||||
* Implements the actual task management.
|
||||
*/
|
||||
export class PluginTaskManagerImpl implements PluginTaskManager {
|
||||
export class PluginTaskSchedulerImpl implements PluginTaskScheduler {
|
||||
constructor(
|
||||
private readonly databaseFactory: () => Promise<Knex>,
|
||||
private readonly logger: Logger,
|
||||
+1
-1
@@ -25,7 +25,7 @@ import { sleep } from './util';
|
||||
* Makes sure to auto-expire and clean up things that time out or for other
|
||||
* reasons should not be left lingering.
|
||||
*/
|
||||
export class PluginTaskManagerJanitor {
|
||||
export class PluginTaskSchedulerJanitor {
|
||||
private readonly knex: Knex;
|
||||
private readonly waitBetweenRuns: Duration;
|
||||
private readonly logger: Logger;
|
||||
+3
-3
@@ -17,10 +17,10 @@
|
||||
import { DatabaseManager, getVoidLogger } from '@backstage/backend-common';
|
||||
import { TestDatabaseId, TestDatabases } from '@backstage/backend-test-utils';
|
||||
import { Duration } from 'luxon';
|
||||
import { TaskManager } from './TaskManager';
|
||||
import { TaskScheduler } from './TaskScheduler';
|
||||
import waitForExpect from 'wait-for-expect';
|
||||
|
||||
describe('TaskManager', () => {
|
||||
describe('TaskScheduler', () => {
|
||||
const logger = getVoidLogger();
|
||||
const databases = TestDatabases.create({
|
||||
ids: ['POSTGRES_13', 'POSTGRES_9', 'SQLITE_3'],
|
||||
@@ -42,7 +42,7 @@ describe('TaskManager', () => {
|
||||
'can return a working plugin impl, %p',
|
||||
async databaseId => {
|
||||
const database = await createDatabase(databaseId);
|
||||
const manager = new TaskManager(database, logger).forPlugin('test');
|
||||
const manager = new TaskScheduler(database, logger).forPlugin('test');
|
||||
const fn = jest.fn();
|
||||
|
||||
await manager.scheduleTask({
|
||||
+10
-10
@@ -20,29 +20,29 @@ import { memoize } from 'lodash';
|
||||
import { Duration } from 'luxon';
|
||||
import { Logger } from 'winston';
|
||||
import { migrateBackendTasks } from '../database/migrateBackendTasks';
|
||||
import { PluginTaskManagerImpl } from './PluginTaskManagerImpl';
|
||||
import { PluginTaskManagerJanitor } from './PluginTaskManagerJanitor';
|
||||
import { PluginTaskManager } from './types';
|
||||
import { PluginTaskSchedulerImpl } from './PluginTaskSchedulerImpl';
|
||||
import { PluginTaskSchedulerJanitor } from './PluginTaskSchedulerJanitor';
|
||||
import { PluginTaskScheduler } from './types';
|
||||
|
||||
/**
|
||||
* Deals with management and locking related to distributed tasks.
|
||||
* Deals with the scheduling of distributed tasks.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export class TaskManager {
|
||||
export class TaskScheduler {
|
||||
static fromConfig(
|
||||
config: Config,
|
||||
options?: {
|
||||
databaseManager?: DatabaseManager;
|
||||
logger?: Logger;
|
||||
},
|
||||
): TaskManager {
|
||||
): TaskScheduler {
|
||||
const databaseManager =
|
||||
options?.databaseManager ?? DatabaseManager.fromConfig(config);
|
||||
const logger = (options?.logger || getRootLogger()).child({
|
||||
type: 'taskManager',
|
||||
});
|
||||
return new TaskManager(databaseManager, logger);
|
||||
return new TaskScheduler(databaseManager, logger);
|
||||
}
|
||||
|
||||
constructor(
|
||||
@@ -56,13 +56,13 @@ export class TaskManager {
|
||||
* @param pluginId - The unique ID of the plugin, for example "catalog"
|
||||
* @returns A {@link PluginTaskManager} instance
|
||||
*/
|
||||
forPlugin(pluginId: string): PluginTaskManager {
|
||||
forPlugin(pluginId: string): PluginTaskScheduler {
|
||||
const databaseFactory = memoize(async () => {
|
||||
const knex = await this.databaseManager.forPlugin(pluginId).getClient();
|
||||
|
||||
await migrateBackendTasks(knex);
|
||||
|
||||
const janitor = new PluginTaskManagerJanitor({
|
||||
const janitor = new PluginTaskSchedulerJanitor({
|
||||
knex,
|
||||
waitBetweenRuns: Duration.fromObject({ minutes: 1 }),
|
||||
logger: this.logger,
|
||||
@@ -72,7 +72,7 @@ export class TaskManager {
|
||||
return knex;
|
||||
});
|
||||
|
||||
return new PluginTaskManagerImpl(
|
||||
return new PluginTaskSchedulerImpl(
|
||||
databaseFactory,
|
||||
this.logger.child({ plugin: pluginId }),
|
||||
);
|
||||
@@ -14,5 +14,9 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export { TaskManager } from './TaskManager';
|
||||
export type { PluginTaskManager, TaskDefinition, TaskFunction } from './types';
|
||||
export { TaskScheduler } from './TaskScheduler';
|
||||
export type {
|
||||
PluginTaskScheduler,
|
||||
TaskDefinition,
|
||||
TaskFunction,
|
||||
} from './types';
|
||||
|
||||
@@ -92,12 +92,11 @@ export interface TaskDefinition {
|
||||
}
|
||||
|
||||
/**
|
||||
* Deals with management and locking related to distributed tasks, for a given
|
||||
* plugin.
|
||||
* Deals with the scheduling of distributed tasks, for a given plugin.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface PluginTaskManager {
|
||||
export interface PluginTaskScheduler {
|
||||
/**
|
||||
* Schedules a task function for coordinated exclusive invocation across
|
||||
* workers.
|
||||
|
||||
Reference in New Issue
Block a user