Skip to content

Commit

Permalink
Introduce TaskScheduler into backends
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 Dec 25, 2021
1 parent a85d9fe commit 0dcd1dd
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 7 deletions.
41 changes: 41 additions & 0 deletions .changeset/slimy-snakes-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
'@backstage/create-app': patch
---

Add a `scheduler` to the plugin environment, which can schedule collaborative tasks across backends. To apply the same change in your backend, follow the steps below.

First install the package:

```shell
# From the Backstage repository root
cd packages/backend
yarn add @backstage/backend-tasks
```

Add the scheduler to your plugin environment type:

```diff
// In packages/backend/src/types.ts
+import { PluginTaskScheduler } from '@backstage/backend-tasks';

export type PluginEnvironment = {
+ scheduler: PluginTaskScheduler;
```

And finally make sure to add such an instance to each plugin's environment:

```diff
// In packages/backend/src/index.ts
+import { TaskScheduler } from '@backstage/backend-tasks';

function makeCreateEnv(config: Config) {
// ...
+ const taskScheduler = TaskScheduler.fromConfig(config);

return (plugin: string): PluginEnvironment => {
// ...
+ const scheduler = taskScheduler.forPlugin(plugin);
return {
+ scheduler,
// ...
```
1 change: 1 addition & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"dependencies": {
"@backstage/backend-common": "^0.10.0",
"@backstage/backend-tasks": "^0.1.0",
"@backstage/catalog-client": "^0.5.3",
"@backstage/catalog-model": "^0.9.8",
"@backstage/config": "^0.1.10",
Expand Down
9 changes: 6 additions & 3 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
useHotMemoize,
ServerTokenManager,
} from '@backstage/backend-common';
import { TaskScheduler } from '@backstage/backend-tasks';
import { Config } from '@backstage/config';
import healthcheck from './plugins/healthcheck';
import { metricsInit, metricsHandler } from './metrics';
Expand Down Expand Up @@ -68,16 +69,17 @@ function makeCreateEnv(config: Config) {
discovery,
tokenManager,
});

root.info(`Created UrlReader ${reader}`);

const databaseManager = DatabaseManager.fromConfig(config);
const cacheManager = CacheManager.fromConfig(config);
const taskScheduler = TaskScheduler.fromConfig(config);

root.info(`Created UrlReader ${reader}`);

return (plugin: string): PluginEnvironment => {
const logger = root.child({ type: 'plugin', plugin });
const database = databaseManager.forPlugin(plugin);
const cache = cacheManager.forPlugin(plugin);
const scheduler = taskScheduler.forPlugin(plugin);
return {
logger,
cache,
Expand All @@ -87,6 +89,7 @@ function makeCreateEnv(config: Config) {
discovery,
tokenManager,
permissions,
scheduler,
};
};
}
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
UrlReader,
} from '@backstage/backend-common';
import { ServerPermissionClient } from '@backstage/plugin-permission-node';
import { PluginTaskScheduler } from '@backstage/backend-tasks';

export type PluginEnvironment = {
logger: Logger;
Expand All @@ -34,4 +35,5 @@ export type PluginEnvironment = {
discovery: PluginEndpointDiscovery;
tokenManager: TokenManager;
permissions: ServerPermissionClient;
scheduler: PluginTaskScheduler;
};
1 change: 1 addition & 0 deletions packages/create-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
},
"peerDependencies": {
"@backstage/backend-common": "*",
"@backstage/backend-tasks": "*",
"@backstage/catalog-client": "*",
"@backstage/catalog-model": "*",
"@backstage/cli": "*",
Expand Down
2 changes: 2 additions & 0 deletions packages/create-app/src/lib/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ leaving any imports in place.

import { version as appDefaults } from '../../../app-defaults/package.json';
import { version as backendCommon } from '../../../backend-common/package.json';
import { version as backendTasks } from '../../../backend-tasks/package.json';
import { version as catalogClient } from '../../../catalog-client/package.json';
import { version as catalogModel } from '../../../catalog-model/package.json';
import { version as cli } from '../../../cli/package.json';
Expand Down Expand Up @@ -71,6 +72,7 @@ import { version as pluginUserSettings } from '../../../../plugins/user-settings
export const packageVersions = {
'@backstage/app-defaults': appDefaults,
'@backstage/backend-common': backendCommon,
'@backstage/backend-tasks': backendTasks,
'@backstage/catalog-client': catalogClient,
'@backstage/catalog-model': catalogModel,
'@backstage/cli': cli,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dependencies": {
"app": "0.0.0",
"@backstage/backend-common": "^{{version '@backstage/backend-common'}}",
"@backstage/backend-tasks": "^{{version '@backstage/backend-tasks'}}",
"@backstage/catalog-model": "^{{version '@backstage/catalog-model'}}",
"@backstage/catalog-client": "^{{version '@backstage/catalog-client'}}",
"@backstage/config": "^{{version '@backstage/config'}}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
UrlReaders,
ServerTokenManager,
} from '@backstage/backend-common';
import { TaskScheduler } from '@backstage/backend-tasks';
import { Config } from '@backstage/config';
import app from './plugins/app';
import auth from './plugins/auth';
Expand All @@ -33,18 +34,28 @@ function makeCreateEnv(config: Config) {
const root = getRootLogger();
const reader = UrlReaders.default({ logger: root, config });
const discovery = SingleHostDiscovery.fromConfig(config);

root.info(`Created UrlReader ${reader}`);

const cacheManager = CacheManager.fromConfig(config);
const databaseManager = DatabaseManager.fromConfig(config);
const tokenManager = ServerTokenManager.noop();
const taskScheduler = TaskScheduler.fromConfig(config);

root.info(`Created UrlReader ${reader}`);

return (plugin: string): PluginEnvironment => {
const logger = root.child({ type: 'plugin', plugin });
const database = databaseManager.forPlugin(plugin);
const cache = cacheManager.forPlugin(plugin);
return { logger, database, cache, config, reader, discovery, tokenManager };
const scheduler = taskScheduler.forPlugin(plugin);
return {
logger,
database,
cache,
config,
reader,
discovery,
tokenManager,
scheduler,
};
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
TokenManager,
UrlReader,
} from '@backstage/backend-common';
import { PluginTaskScheduler } from '@backstage/backend-tasks';

export type PluginEnvironment = {
logger: Logger;
Expand All @@ -16,4 +17,5 @@ export type PluginEnvironment = {
reader: UrlReader;
discovery: PluginEndpointDiscovery;
tokenManager: TokenManager;
scheduler: PluginTaskScheduler;
};

0 comments on commit 0dcd1dd

Please sign in to comment.