Introduce TaskScheduler into backends
Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
@@ -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,
|
||||
// ...
|
||||
```
|
||||
@@ -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",
|
||||
|
||||
@@ -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';
|
||||
@@ -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,
|
||||
@@ -87,6 +89,7 @@ function makeCreateEnv(config: Config) {
|
||||
discovery,
|
||||
tokenManager,
|
||||
permissions,
|
||||
scheduler,
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -34,4 +35,5 @@ export type PluginEnvironment = {
|
||||
discovery: PluginEndpointDiscovery;
|
||||
tokenManager: TokenManager;
|
||||
permissions: ServerPermissionClient;
|
||||
scheduler: PluginTaskScheduler;
|
||||
};
|
||||
|
||||
@@ -49,6 +49,7 @@
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@backstage/backend-common": "*",
|
||||
"@backstage/backend-tasks": "*",
|
||||
"@backstage/catalog-client": "*",
|
||||
"@backstage/catalog-model": "*",
|
||||
"@backstage/cli": "*",
|
||||
|
||||
@@ -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';
|
||||
@@ -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,
|
||||
|
||||
@@ -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'}}",
|
||||
|
||||
@@ -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';
|
||||
@@ -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,
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
TokenManager,
|
||||
UrlReader,
|
||||
} from '@backstage/backend-common';
|
||||
import { PluginTaskScheduler } from '@backstage/backend-tasks';
|
||||
|
||||
export type PluginEnvironment = {
|
||||
logger: Logger;
|
||||
@@ -16,4 +17,5 @@ export type PluginEnvironment = {
|
||||
reader: UrlReader;
|
||||
discovery: PluginEndpointDiscovery;
|
||||
tokenManager: TokenManager;
|
||||
scheduler: PluginTaskScheduler;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user