Skip to content

Commit

Permalink
make the original task definition into just one object
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 b91fe07 commit d8c28d9
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 53 deletions.
12 changes: 5 additions & 7 deletions packages/backend-tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ import { TaskManager } from '@backstage/backend-tasks';

const manager = TaskManager.fromConfig(rootConfig).forPlugin('my-plugin');

const { unschedule } = await manager.scheduleTask(
'refresh-things',
{
frequency: Duration.fromObject({ minutes: 10 }),
},
async () => {
const { unschedule } = await manager.scheduleTask({
id: 'refresh-things',
frequency: Duration.fromObject({ minutes: 10 }),
fn: async () => {
await entityProvider.run();
},
);
});
```

## Documentation
Expand Down
22 changes: 10 additions & 12 deletions packages/backend-tasks/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ import { Logger as Logger_2 } from 'winston';

// @public
export interface PluginTaskManager {
scheduleTask(
id: string,
options: TaskOptions,
fn: () => void | Promise<void>,
): Promise<{
scheduleTask(task: TaskDefinition): Promise<{
unschedule: () => Promise<void>;
}>;
}

// @public
export interface TaskDefinition {
fn: () => void | Promise<void>;
frequency: Duration;
id: string;
initialDelay?: Duration;
timeout: Duration;
}

// @public
export class TaskManager {
constructor(databaseManager: DatabaseManager, logger: Logger_2);
Expand All @@ -32,11 +37,4 @@ export class TaskManager {
},
): TaskManager;
}

// @public
export interface TaskOptions {
frequency?: Duration;
initialDelay?: Duration;
timeout?: Duration;
}
```
12 changes: 5 additions & 7 deletions packages/backend-tasks/src/tasks/PluginTaskManagerImpl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,12 @@ describe('PluginTaskManagerImpl', () => {
const { manager } = await init(databaseId);

const fn = jest.fn();
const { unschedule } = await manager.scheduleTask(
'task1',
{
timeout: Duration.fromMillis(5000),
frequency: Duration.fromMillis(5000),
},
const { unschedule } = await manager.scheduleTask({
id: 'task1',
timeout: Duration.fromMillis(5000),
frequency: Duration.fromMillis(5000),
fn,
);
});

await waitForExpect(() => {
expect(fn).toBeCalled();
Expand Down
20 changes: 9 additions & 11 deletions packages/backend-tasks/src/tasks/PluginTaskManagerImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { Knex } from 'knex';
import { Logger } from 'winston';
import { TaskWorker } from './TaskWorker';
import { PluginTaskManager, TaskOptions } from './types';
import { PluginTaskManager, TaskDefinition } from './types';
import { validateId } from './util';

/**
Expand All @@ -30,25 +30,23 @@ export class PluginTaskManagerImpl implements PluginTaskManager {
) {}

async scheduleTask(
id: string,
options: TaskOptions,
fn: () => void | Promise<void>,
task: TaskDefinition,
): Promise<{ unschedule: () => Promise<void> }> {
validateId(id);
validateId(task.id);

const knex = await this.databaseFactory();

const task = new TaskWorker(id, fn, knex, this.logger);
await task.start({
const worker = new TaskWorker(task.id, task.fn, knex, this.logger);
await worker.start({
version: 1,
initialDelayDuration: options.initialDelay?.toISO(),
recurringAtMostEveryDuration: options.frequency.toISO(),
timeoutAfterDuration: options.timeout.toISO(),
initialDelayDuration: task.initialDelay?.toISO(),
recurringAtMostEveryDuration: task.frequency.toISO(),
timeoutAfterDuration: task.timeout.toISO(),
});

return {
async unschedule() {
await task.stop();
await worker.stop();
},
};
}
Expand Down
14 changes: 6 additions & 8 deletions packages/backend-tasks/src/tasks/TaskManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ describe('TaskManager', () => {
const database = await createDatabase(databaseId);
const manager = new TaskManager(database, logger).forPlugin('test');

const task = await manager.scheduleTask(
'task1',
{
timeout: Duration.fromMillis(5000),
frequency: Duration.fromMillis(5000),
},
() => {},
);
const task = await manager.scheduleTask({
id: 'task1',
timeout: Duration.fromMillis(5000),
frequency: Duration.fromMillis(5000),
fn: () => {},
});
expect(task.unschedule).toBeDefined();
},
);
Expand Down
2 changes: 1 addition & 1 deletion packages/backend-tasks/src/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
*/

export { TaskManager } from './TaskManager';
export type { PluginTaskManager, TaskOptions } from './types';
export type { PluginTaskManager, TaskDefinition } from './types';
20 changes: 13 additions & 7 deletions packages/backend-tasks/src/tasks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@ import { z } from 'zod';
*
* @public
*/
export interface TaskOptions {
export interface TaskDefinition {
/**
* A unique ID (within the scope of the plugin) for the task.
*/
id: string;

/**
* The actual task function to be invoked regularly.
*/
fn: () => void | Promise<void>;

/**
* The maximum amount of time that a single task invocation can take, before
* it's considered timed out and gets "released" such that a new invocation
Expand Down Expand Up @@ -77,17 +87,13 @@ export interface PluginTaskManager {
* its options are just overwritten with the given options, and things
* continue from there.
*
* @param id - A unique ID (within the scope of the plugin) for the task
* @param options - Options for the task
* @param fn - The actual task function to be invoked
* @param definition - The task definition
* @returns An `unschedule` function that can be used to stop the task
* invocations later on. This removes the task entirely from storage
* and stops its invocations across all workers.
*/
scheduleTask(
id: string,
options: TaskOptions,
fn: () => void | Promise<void>,
task: TaskDefinition,
): Promise<{ unschedule: () => Promise<void> }>;
}

Expand Down

0 comments on commit d8c28d9

Please sign in to comment.