Skip to content

Commit

Permalink
rename to TaskScheduler to reduce collision risk with other concepts
Browse files Browse the repository at this point in the history
Signed-off-by: Fredrik Adelöw <freben@gmail.com>
  • Loading branch information
freben committed Nov 11, 2021
1 parent 92d85b8 commit 85cc6bd
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 33 deletions.
8 changes: 4 additions & 4 deletions packages/backend-tasks/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @backstage/backend-tasks

Common distributed task management / locking library for Backstage backends.
Common distributed task management for Backstage backends.

## Usage

Expand All @@ -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 () => {
Expand Down
9 changes: 5 additions & 4 deletions packages/backend-tasks/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
}

Expand All @@ -30,16 +30,17 @@ 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,
options?: {
databaseManager?: DatabaseManager;
logger?: Logger_2;
},
): TaskManager;
): TaskScheduler;
}
```
2 changes: 1 addition & 1 deletion packages/backend-tasks/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/backend-tasks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

/**
* Common distributed task management / locking library for Backstage backends
* Common distributed task management library for Backstage backends
*
* @packageDocumentation
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -72,7 +72,7 @@ export class TaskManager {
return knex;
});

return new PluginTaskManagerImpl(
return new PluginTaskSchedulerImpl(
databaseFactory,
this.logger.child({ plugin: pluginId }),
);
Expand Down
8 changes: 6 additions & 2 deletions packages/backend-tasks/src/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
5 changes: 2 additions & 3 deletions packages/backend-tasks/src/tasks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 85cc6bd

Please sign in to comment.