@backstage/backend-defaults 0.9.0 → 0.10.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +48 -0
- package/config.d.ts +71 -0
- package/dist/cache.d.ts +2 -1
- package/dist/entrypoints/auth/DefaultAuthService.cjs.js.map +1 -1
- package/dist/entrypoints/cache/CacheManager.cjs.js +33 -3
- package/dist/entrypoints/cache/CacheManager.cjs.js.map +1 -1
- package/dist/entrypoints/scheduler/database/migrateBackendTasks.cjs.js +5 -4
- package/dist/entrypoints/scheduler/database/migrateBackendTasks.cjs.js.map +1 -1
- package/dist/entrypoints/scheduler/database/tables.cjs.js.map +1 -1
- package/dist/entrypoints/scheduler/lib/DefaultSchedulerService.cjs.js +5 -2
- package/dist/entrypoints/scheduler/lib/DefaultSchedulerService.cjs.js.map +1 -1
- package/dist/entrypoints/scheduler/lib/LocalTaskWorker.cjs.js +58 -10
- package/dist/entrypoints/scheduler/lib/LocalTaskWorker.cjs.js.map +1 -1
- package/dist/entrypoints/scheduler/lib/PluginTaskSchedulerImpl.cjs.js +42 -5
- package/dist/entrypoints/scheduler/lib/PluginTaskSchedulerImpl.cjs.js.map +1 -1
- package/dist/entrypoints/scheduler/lib/PluginTaskSchedulerJanitor.cjs.js +6 -2
- package/dist/entrypoints/scheduler/lib/PluginTaskSchedulerJanitor.cjs.js.map +1 -1
- package/dist/entrypoints/scheduler/lib/TaskWorker.cjs.js +60 -10
- package/dist/entrypoints/scheduler/lib/TaskWorker.cjs.js.map +1 -1
- package/dist/entrypoints/scheduler/lib/types.cjs.js.map +1 -1
- package/dist/entrypoints/scheduler/lib/util.cjs.js +16 -1
- package/dist/entrypoints/scheduler/lib/util.cjs.js.map +1 -1
- package/dist/entrypoints/scheduler/schedulerServiceFactory.cjs.js +17 -3
- package/dist/entrypoints/scheduler/schedulerServiceFactory.cjs.js.map +1 -1
- package/dist/entrypoints/urlReader/lib/GitlabUrlReader.cjs.js +11 -8
- package/dist/entrypoints/urlReader/lib/GitlabUrlReader.cjs.js.map +1 -1
- package/dist/package.json.cjs.js +2 -1
- package/dist/package.json.cjs.js.map +1 -1
- package/dist/scheduler.d.ts +4 -2
- package/migrations/scheduler/20250411000000_last_run.js +47 -0
- package/package.json +18 -17
|
@@ -2,13 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
var api = require('@opentelemetry/api');
|
|
4
4
|
var luxon = require('luxon');
|
|
5
|
+
var Router = require('express-promise-router');
|
|
5
6
|
var LocalTaskWorker = require('./LocalTaskWorker.cjs.js');
|
|
6
7
|
var TaskWorker = require('./TaskWorker.cjs.js');
|
|
7
8
|
var util = require('./util.cjs.js');
|
|
8
9
|
|
|
10
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
|
|
11
|
+
|
|
12
|
+
var Router__default = /*#__PURE__*/_interopDefaultCompat(Router);
|
|
13
|
+
|
|
9
14
|
const tracer = api.trace.getTracer(util.TRACER_ID);
|
|
10
15
|
class PluginTaskSchedulerImpl {
|
|
11
|
-
constructor(databaseFactory, logger, rootLifecycle) {
|
|
16
|
+
constructor(pluginId, databaseFactory, logger, rootLifecycle) {
|
|
17
|
+
this.pluginId = pluginId;
|
|
12
18
|
this.databaseFactory = databaseFactory;
|
|
13
19
|
this.logger = logger;
|
|
14
20
|
const meter = api.metrics.getMeter("default");
|
|
@@ -31,10 +37,11 @@ class PluginTaskSchedulerImpl {
|
|
|
31
37
|
}
|
|
32
38
|
);
|
|
33
39
|
this.shutdownInitiated = new Promise((shutdownInitiated) => {
|
|
34
|
-
rootLifecycle
|
|
40
|
+
rootLifecycle.addShutdownHook(() => shutdownInitiated(true));
|
|
35
41
|
});
|
|
36
42
|
}
|
|
37
|
-
|
|
43
|
+
localWorkersById = /* @__PURE__ */ new Map();
|
|
44
|
+
globalWorkersById = /* @__PURE__ */ new Map();
|
|
38
45
|
allScheduledTasks = [];
|
|
39
46
|
shutdownInitiated;
|
|
40
47
|
counter;
|
|
@@ -42,7 +49,7 @@ class PluginTaskSchedulerImpl {
|
|
|
42
49
|
lastStarted;
|
|
43
50
|
lastCompleted;
|
|
44
51
|
async triggerTask(id) {
|
|
45
|
-
const localTask = this.
|
|
52
|
+
const localTask = this.localWorkersById.get(id);
|
|
46
53
|
if (localTask) {
|
|
47
54
|
localTask.trigger();
|
|
48
55
|
return;
|
|
@@ -70,6 +77,7 @@ class PluginTaskSchedulerImpl {
|
|
|
70
77
|
this.logger.child({ task: task.id })
|
|
71
78
|
);
|
|
72
79
|
await worker.start(settings, { signal: abortController.signal });
|
|
80
|
+
this.globalWorkersById.set(task.id, worker);
|
|
73
81
|
} else {
|
|
74
82
|
const worker = new LocalTaskWorker.LocalTaskWorker(
|
|
75
83
|
task.id,
|
|
@@ -77,7 +85,7 @@ class PluginTaskSchedulerImpl {
|
|
|
77
85
|
this.logger.child({ task: task.id })
|
|
78
86
|
);
|
|
79
87
|
worker.start(settings, { signal: abortController.signal });
|
|
80
|
-
this.
|
|
88
|
+
this.localWorkersById.set(task.id, worker);
|
|
81
89
|
}
|
|
82
90
|
this.allScheduledTasks.push({
|
|
83
91
|
id: task.id,
|
|
@@ -95,6 +103,35 @@ class PluginTaskSchedulerImpl {
|
|
|
95
103
|
async getScheduledTasks() {
|
|
96
104
|
return this.allScheduledTasks;
|
|
97
105
|
}
|
|
106
|
+
getRouter() {
|
|
107
|
+
const router = Router__default.default();
|
|
108
|
+
router.get("/.backstage/scheduler/v1/tasks", async (_, res) => {
|
|
109
|
+
const globalState = await TaskWorker.TaskWorker.taskStates(
|
|
110
|
+
await this.databaseFactory()
|
|
111
|
+
);
|
|
112
|
+
const tasks = new Array();
|
|
113
|
+
for (const task of this.allScheduledTasks) {
|
|
114
|
+
tasks.push({
|
|
115
|
+
taskId: task.id,
|
|
116
|
+
pluginId: this.pluginId,
|
|
117
|
+
scope: task.scope,
|
|
118
|
+
settings: task.settings,
|
|
119
|
+
taskState: this.localWorkersById.get(task.id)?.taskState() ?? globalState.get(task.id) ?? null,
|
|
120
|
+
workerState: this.localWorkersById.get(task.id)?.workerState() ?? this.globalWorkersById.get(task.id)?.workerState() ?? null
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
res.json({ tasks });
|
|
124
|
+
});
|
|
125
|
+
router.post(
|
|
126
|
+
"/.backstage/scheduler/v1/tasks/:id/trigger",
|
|
127
|
+
async (req, res) => {
|
|
128
|
+
const { id } = req.params;
|
|
129
|
+
await this.triggerTask(id);
|
|
130
|
+
res.status(200).end();
|
|
131
|
+
}
|
|
132
|
+
);
|
|
133
|
+
return router;
|
|
134
|
+
}
|
|
98
135
|
instrumentedFunction(task, scope) {
|
|
99
136
|
return async (abort) => {
|
|
100
137
|
const labels = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PluginTaskSchedulerImpl.cjs.js","sources":["../../../../src/entrypoints/scheduler/lib/PluginTaskSchedulerImpl.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n LoggerService,\n RootLifecycleService,\n SchedulerService,\n SchedulerServiceTaskDescriptor,\n SchedulerServiceTaskFunction,\n SchedulerServiceTaskInvocationDefinition,\n SchedulerServiceTaskRunner,\n SchedulerServiceTaskScheduleDefinition,\n} from '@backstage/backend-plugin-api';\nimport { Counter, Histogram, Gauge, metrics, trace } from '@opentelemetry/api';\nimport { Knex } from 'knex';\nimport { Duration } from 'luxon';\nimport { LocalTaskWorker } from './LocalTaskWorker';\nimport { TaskWorker } from './TaskWorker';\nimport { TaskSettingsV2 } from './types';\nimport { delegateAbortController, TRACER_ID, validateId } from './util';\n\nconst tracer = trace.getTracer(TRACER_ID);\n\n/**\n * Implements the actual task management.\n */\nexport class PluginTaskSchedulerImpl implements SchedulerService {\n private readonly localTasksById = new Map<string, LocalTaskWorker>();\n private readonly allScheduledTasks: SchedulerServiceTaskDescriptor[] = [];\n private readonly shutdownInitiated: Promise<boolean>;\n\n private readonly counter: Counter;\n private readonly duration: Histogram;\n private readonly lastStarted: Gauge;\n private readonly lastCompleted: Gauge;\n\n constructor(\n private readonly databaseFactory: () => Promise<Knex>,\n private readonly logger: LoggerService,\n rootLifecycle?: RootLifecycleService,\n ) {\n const meter = metrics.getMeter('default');\n this.counter = meter.createCounter('backend_tasks.task.runs.count', {\n description: 'Total number of times a task has been run',\n });\n this.duration = meter.createHistogram('backend_tasks.task.runs.duration', {\n description: 'Histogram of task run durations',\n unit: 'seconds',\n });\n this.lastStarted = meter.createGauge('backend_tasks.task.runs.started', {\n description: 'Epoch timestamp seconds when the task was last started',\n unit: 'seconds',\n });\n this.lastCompleted = meter.createGauge(\n 'backend_tasks.task.runs.completed',\n {\n description: 'Epoch timestamp seconds when the task was last completed',\n unit: 'seconds',\n },\n );\n this.shutdownInitiated = new Promise(shutdownInitiated => {\n rootLifecycle?.addShutdownHook(() => shutdownInitiated(true));\n });\n }\n\n async triggerTask(id: string): Promise<void> {\n const localTask = this.localTasksById.get(id);\n if (localTask) {\n localTask.trigger();\n return;\n }\n\n const knex = await this.databaseFactory();\n await TaskWorker.trigger(knex, id);\n }\n\n async scheduleTask(\n task: SchedulerServiceTaskScheduleDefinition &\n SchedulerServiceTaskInvocationDefinition,\n ): Promise<void> {\n validateId(task.id);\n const scope = task.scope ?? 'global';\n\n const settings: TaskSettingsV2 = {\n version: 2,\n cadence: parseDuration(task.frequency),\n initialDelayDuration:\n task.initialDelay && parseDuration(task.initialDelay),\n timeoutAfterDuration: parseDuration(task.timeout),\n };\n\n // Delegated abort controller that will abort either when the provided\n // controller aborts, or when a root lifecycle shutdown happens\n const abortController = delegateAbortController(task.signal);\n this.shutdownInitiated.then(() => abortController.abort());\n\n if (scope === 'global') {\n const knex = await this.databaseFactory();\n const worker = new TaskWorker(\n task.id,\n this.instrumentedFunction(task, scope),\n knex,\n this.logger.child({ task: task.id }),\n );\n await worker.start(settings, { signal: abortController.signal });\n } else {\n const worker = new LocalTaskWorker(\n task.id,\n this.instrumentedFunction(task, scope),\n this.logger.child({ task: task.id }),\n );\n worker.start(settings, { signal: abortController.signal });\n this.localTasksById.set(task.id, worker);\n }\n\n this.allScheduledTasks.push({\n id: task.id,\n scope: scope,\n settings: settings,\n });\n }\n\n createScheduledTaskRunner(\n schedule: SchedulerServiceTaskScheduleDefinition,\n ): SchedulerServiceTaskRunner {\n return {\n run: async task => {\n await this.scheduleTask({ ...task, ...schedule });\n },\n };\n }\n\n async getScheduledTasks(): Promise<SchedulerServiceTaskDescriptor[]> {\n return this.allScheduledTasks;\n }\n\n private instrumentedFunction(\n task: SchedulerServiceTaskInvocationDefinition,\n scope: string,\n ): SchedulerServiceTaskFunction {\n return async abort => {\n const labels: Record<string, string> = {\n taskId: task.id,\n scope,\n };\n this.counter.add(1, { ...labels, result: 'started' });\n this.lastStarted.record(Date.now() / 1000, { taskId: task.id });\n\n const startTime = process.hrtime();\n\n try {\n await tracer.startActiveSpan(`task ${task.id}`, async span => {\n try {\n span.setAttributes(labels);\n await task.fn(abort);\n } catch (error) {\n if (error instanceof Error) {\n span.recordException(error);\n }\n throw error;\n } finally {\n span.end();\n }\n });\n labels.result = 'completed';\n } catch (ex) {\n labels.result = 'failed';\n throw ex;\n } finally {\n const delta = process.hrtime(startTime);\n const endTime = delta[0] + delta[1] / 1e9;\n this.counter.add(1, labels);\n this.duration.record(endTime, labels);\n this.lastCompleted.record(Date.now() / 1000, labels);\n }\n };\n }\n}\n\nexport function parseDuration(\n frequency: SchedulerServiceTaskScheduleDefinition['frequency'],\n): string {\n if (typeof frequency === 'object' && 'cron' in frequency) {\n return frequency.cron;\n }\n if (typeof frequency === 'object' && 'trigger' in frequency) {\n return frequency.trigger;\n }\n\n const parsed = Duration.isDuration(frequency)\n ? frequency\n : Duration.fromObject(frequency);\n\n if (!parsed.isValid) {\n throw new Error(\n `Invalid duration, ${parsed.invalidReason}: ${parsed.invalidExplanation}`,\n );\n }\n\n return parsed.toISO()!;\n}\n"],"names":["trace","TRACER_ID","metrics","TaskWorker","validateId","delegateAbortController","LocalTaskWorker","Duration"],"mappings":";;;;;;;;AAkCA,MAAM,MAAA,GAASA,SAAM,CAAA,SAAA,CAAUC,cAAS,CAAA;AAKjC,MAAM,uBAAoD,CAAA;AAAA,EAU/D,WAAA,CACmB,eACA,EAAA,MAAA,EACjB,aACA,EAAA;AAHiB,IAAA,IAAA,CAAA,eAAA,GAAA,eAAA;AACA,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AAGjB,IAAM,MAAA,KAAA,GAAQC,WAAQ,CAAA,QAAA,CAAS,SAAS,CAAA;AACxC,IAAK,IAAA,CAAA,OAAA,GAAU,KAAM,CAAA,aAAA,CAAc,+BAAiC,EAAA;AAAA,MAClE,WAAa,EAAA;AAAA,KACd,CAAA;AACD,IAAK,IAAA,CAAA,QAAA,GAAW,KAAM,CAAA,eAAA,CAAgB,kCAAoC,EAAA;AAAA,MACxE,WAAa,EAAA,iCAAA;AAAA,MACb,IAAM,EAAA;AAAA,KACP,CAAA;AACD,IAAK,IAAA,CAAA,WAAA,GAAc,KAAM,CAAA,WAAA,CAAY,iCAAmC,EAAA;AAAA,MACtE,WAAa,EAAA,wDAAA;AAAA,MACb,IAAM,EAAA;AAAA,KACP,CAAA;AACD,IAAA,IAAA,CAAK,gBAAgB,KAAM,CAAA,WAAA;AAAA,MACzB,mCAAA;AAAA,MACA;AAAA,QACE,WAAa,EAAA,0DAAA;AAAA,QACb,IAAM,EAAA;AAAA;AACR,KACF;AACA,IAAK,IAAA,CAAA,iBAAA,GAAoB,IAAI,OAAA,CAAQ,CAAqB,iBAAA,K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,IAAA,CAAA,QAAA,CAAS,MAAO,CAAA,OAAA,EAAS,MAAM,CAAA;AACpC,QAAA,IAAA,CAAK,cAAc,MAAO,CAAA,IAAA,CAAK,GAAI,EAAA,GAAI,KAAM,MAAM,CAAA;AAAA;AACrD,KACF;AAAA;AAEJ;AAEO,SAAS,cACd,SACQ,EAAA;AACR,EAAA,IAAI,OAAO,SAAA,KAAc,QAAY,IAAA,MAAA,IAAU,SAAW,EAAA;AACxD,IAAA,OAAO,SAAU,CAAA,IAAA;AAAA;AAEnB,EAAA,IAAI,OAAO,SAAA,KAAc,QAAY,IAAA,SAAA,IAAa,SAAW,EAAA;AAC3D,IAAA,OAAO,SAAU,CAAA,OAAA;AAAA;AAGnB,EAAM,MAAA,MAAA,GAASC,eAAS,UAAW,CAAA,SAAS,IACxC,SACA,GAAAA,cAAA,CAAS,WAAW,SAAS,CAAA;AAEjC,EAAI,IAAA,CAAC,OAAO,OAAS,EAAA;AACnB,IAAA,MAAM,IAAI,KAAA;AAAA,MACR,CAAqB,kBAAA,EAAA,MAAA,CAAO,aAAa,CAAA,EAAA,EAAK,OAAO,kBAAkB,CAAA;AAAA,KACzE;AAAA;AAGF,EAAA,OAAO,OAAO,KAAM,EAAA;AACtB;;;;;"}
|
|
1
|
+
{"version":3,"file":"PluginTaskSchedulerImpl.cjs.js","sources":["../../../../src/entrypoints/scheduler/lib/PluginTaskSchedulerImpl.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n LoggerService,\n RootLifecycleService,\n SchedulerService,\n SchedulerServiceTaskDescriptor,\n SchedulerServiceTaskFunction,\n SchedulerServiceTaskInvocationDefinition,\n SchedulerServiceTaskRunner,\n SchedulerServiceTaskScheduleDefinition,\n} from '@backstage/backend-plugin-api';\nimport { Counter, Histogram, Gauge, metrics, trace } from '@opentelemetry/api';\nimport { Knex } from 'knex';\nimport { Duration } from 'luxon';\nimport express from 'express';\nimport Router from 'express-promise-router';\nimport { LocalTaskWorker } from './LocalTaskWorker';\nimport { TaskWorker } from './TaskWorker';\nimport { TaskSettingsV2, TaskApiTasksResponse } from './types';\nimport { delegateAbortController, TRACER_ID, validateId } from './util';\n\nconst tracer = trace.getTracer(TRACER_ID);\n\n/**\n * Implements the actual task management.\n */\nexport class PluginTaskSchedulerImpl implements SchedulerService {\n private readonly localWorkersById = new Map<string, LocalTaskWorker>();\n private readonly globalWorkersById = new Map<string, TaskWorker>();\n private readonly allScheduledTasks: SchedulerServiceTaskDescriptor[] = [];\n private readonly shutdownInitiated: Promise<boolean>;\n\n private readonly counter: Counter;\n private readonly duration: Histogram;\n private readonly lastStarted: Gauge;\n private readonly lastCompleted: Gauge;\n\n constructor(\n private readonly pluginId: string,\n private readonly databaseFactory: () => Promise<Knex>,\n private readonly logger: LoggerService,\n rootLifecycle: RootLifecycleService,\n ) {\n const meter = metrics.getMeter('default');\n this.counter = meter.createCounter('backend_tasks.task.runs.count', {\n description: 'Total number of times a task has been run',\n });\n this.duration = meter.createHistogram('backend_tasks.task.runs.duration', {\n description: 'Histogram of task run durations',\n unit: 'seconds',\n });\n this.lastStarted = meter.createGauge('backend_tasks.task.runs.started', {\n description: 'Epoch timestamp seconds when the task was last started',\n unit: 'seconds',\n });\n this.lastCompleted = meter.createGauge(\n 'backend_tasks.task.runs.completed',\n {\n description: 'Epoch timestamp seconds when the task was last completed',\n unit: 'seconds',\n },\n );\n this.shutdownInitiated = new Promise(shutdownInitiated => {\n rootLifecycle.addShutdownHook(() => shutdownInitiated(true));\n });\n }\n\n async triggerTask(id: string): Promise<void> {\n const localTask = this.localWorkersById.get(id);\n if (localTask) {\n localTask.trigger();\n return;\n }\n\n const knex = await this.databaseFactory();\n await TaskWorker.trigger(knex, id);\n }\n\n async scheduleTask(\n task: SchedulerServiceTaskScheduleDefinition &\n SchedulerServiceTaskInvocationDefinition,\n ): Promise<void> {\n validateId(task.id);\n const scope = task.scope ?? 'global';\n\n const settings: TaskSettingsV2 = {\n version: 2,\n cadence: parseDuration(task.frequency),\n initialDelayDuration:\n task.initialDelay && parseDuration(task.initialDelay),\n timeoutAfterDuration: parseDuration(task.timeout),\n };\n\n // Delegated abort controller that will abort either when the provided\n // controller aborts, or when a root lifecycle shutdown happens\n const abortController = delegateAbortController(task.signal);\n this.shutdownInitiated.then(() => abortController.abort());\n\n if (scope === 'global') {\n const knex = await this.databaseFactory();\n const worker = new TaskWorker(\n task.id,\n this.instrumentedFunction(task, scope),\n knex,\n this.logger.child({ task: task.id }),\n );\n await worker.start(settings, { signal: abortController.signal });\n this.globalWorkersById.set(task.id, worker);\n } else {\n const worker = new LocalTaskWorker(\n task.id,\n this.instrumentedFunction(task, scope),\n this.logger.child({ task: task.id }),\n );\n worker.start(settings, { signal: abortController.signal });\n this.localWorkersById.set(task.id, worker);\n }\n\n this.allScheduledTasks.push({\n id: task.id,\n scope: scope,\n settings: settings,\n });\n }\n\n createScheduledTaskRunner(\n schedule: SchedulerServiceTaskScheduleDefinition,\n ): SchedulerServiceTaskRunner {\n return {\n run: async task => {\n await this.scheduleTask({ ...task, ...schedule });\n },\n };\n }\n\n async getScheduledTasks(): Promise<SchedulerServiceTaskDescriptor[]> {\n return this.allScheduledTasks;\n }\n\n getRouter(): express.Router {\n const router = Router();\n\n router.get('/.backstage/scheduler/v1/tasks', async (_, res) => {\n const globalState = await TaskWorker.taskStates(\n await this.databaseFactory(),\n );\n\n const tasks = new Array<TaskApiTasksResponse>();\n for (const task of this.allScheduledTasks) {\n tasks.push({\n taskId: task.id,\n pluginId: this.pluginId,\n scope: task.scope,\n settings: task.settings,\n taskState:\n this.localWorkersById.get(task.id)?.taskState() ??\n globalState.get(task.id) ??\n null,\n workerState:\n this.localWorkersById.get(task.id)?.workerState() ??\n this.globalWorkersById.get(task.id)?.workerState() ??\n null,\n });\n }\n\n res.json({ tasks });\n });\n\n router.post(\n '/.backstage/scheduler/v1/tasks/:id/trigger',\n async (req, res) => {\n const { id } = req.params;\n await this.triggerTask(id);\n res.status(200).end();\n },\n );\n\n return router;\n }\n\n private instrumentedFunction(\n task: SchedulerServiceTaskInvocationDefinition,\n scope: string,\n ): SchedulerServiceTaskFunction {\n return async abort => {\n const labels: Record<string, string> = {\n taskId: task.id,\n scope,\n };\n this.counter.add(1, { ...labels, result: 'started' });\n this.lastStarted.record(Date.now() / 1000, { taskId: task.id });\n\n const startTime = process.hrtime();\n\n try {\n await tracer.startActiveSpan(`task ${task.id}`, async span => {\n try {\n span.setAttributes(labels);\n await task.fn(abort);\n } catch (error) {\n if (error instanceof Error) {\n span.recordException(error);\n }\n throw error;\n } finally {\n span.end();\n }\n });\n labels.result = 'completed';\n } catch (ex) {\n labels.result = 'failed';\n throw ex;\n } finally {\n const delta = process.hrtime(startTime);\n const endTime = delta[0] + delta[1] / 1e9;\n this.counter.add(1, labels);\n this.duration.record(endTime, labels);\n this.lastCompleted.record(Date.now() / 1000, labels);\n }\n };\n }\n}\n\nexport function parseDuration(\n frequency: SchedulerServiceTaskScheduleDefinition['frequency'],\n): string {\n if (typeof frequency === 'object' && 'cron' in frequency) {\n return frequency.cron;\n }\n if (typeof frequency === 'object' && 'trigger' in frequency) {\n return frequency.trigger;\n }\n\n const parsed = Duration.isDuration(frequency)\n ? frequency\n : Duration.fromObject(frequency);\n\n if (!parsed.isValid) {\n throw new Error(\n `Invalid duration, ${parsed.invalidReason}: ${parsed.invalidExplanation}`,\n );\n }\n\n return parsed.toISO()!;\n}\n"],"names":["trace","TRACER_ID","metrics","TaskWorker","validateId","delegateAbortController","LocalTaskWorker","Router","Duration"],"mappings":";;;;;;;;;;;;;AAoCA,MAAM,M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AA,CAAK,cAAc,MAAO,CAAA,IAAA,CAAK,GAAI,EAAA,GAAI,KAAM,MAAM,CAAA;AAAA;AACrD,KACF;AAAA;AAEJ;AAEO,SAAS,cACd,SACQ,EAAA;AACR,EAAA,IAAI,OAAO,SAAA,KAAc,QAAY,IAAA,MAAA,IAAU,SAAW,EAAA;AACxD,IAAA,OAAO,SAAU,CAAA,IAAA;AAAA;AAEnB,EAAA,IAAI,OAAO,SAAA,KAAc,QAAY,IAAA,SAAA,IAAa,SAAW,EAAA;AAC3D,IAAA,OAAO,SAAU,CAAA,OAAA;AAAA;AAGnB,EAAM,MAAA,MAAA,GAASK,eAAS,UAAW,CAAA,SAAS,IACxC,SACA,GAAAA,cAAA,CAAS,WAAW,SAAS,CAAA;AAEjC,EAAI,IAAA,CAAC,OAAO,OAAS,EAAA;AACnB,IAAA,MAAM,IAAI,KAAA;AAAA,MACR,CAAqB,kBAAA,EAAA,MAAA,CAAO,aAAa,CAAA,EAAA,EAAK,OAAO,kBAAkB,CAAA;AAAA,KACzE;AAAA;AAGF,EAAA,OAAO,OAAO,KAAM,EAAA;AACtB;;;;;"}
|
|
@@ -34,13 +34,17 @@ class PluginTaskSchedulerJanitor {
|
|
|
34
34
|
).update({
|
|
35
35
|
current_run_ticket: dbNull,
|
|
36
36
|
current_run_started_at: dbNull,
|
|
37
|
-
current_run_expires_at: dbNull
|
|
37
|
+
current_run_expires_at: dbNull,
|
|
38
|
+
last_run_ended_at: this.knex.fn.now(),
|
|
39
|
+
last_run_error_json: util.serializeError(new Error("Task timed out"))
|
|
38
40
|
});
|
|
39
41
|
} else {
|
|
40
42
|
tasks = await this.knex(tables.DB_TASKS_TABLE).where("current_run_expires_at", "<", this.knex.fn.now()).update({
|
|
41
43
|
current_run_ticket: dbNull,
|
|
42
44
|
current_run_started_at: dbNull,
|
|
43
|
-
current_run_expires_at: dbNull
|
|
45
|
+
current_run_expires_at: dbNull,
|
|
46
|
+
last_run_ended_at: this.knex.fn.now(),
|
|
47
|
+
last_run_error_json: util.serializeError(new Error("Task timed out"))
|
|
44
48
|
}).returning(["id"]);
|
|
45
49
|
}
|
|
46
50
|
if (typeof tasks === "number") {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PluginTaskSchedulerJanitor.cjs.js","sources":["../../../../src/entrypoints/scheduler/lib/PluginTaskSchedulerJanitor.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { LoggerService } from '@backstage/backend-plugin-api';\nimport { Knex } from 'knex';\nimport { Duration } from 'luxon';\nimport { DB_TASKS_TABLE, DbTasksRow } from '../database/tables';\nimport { sleep } from './util';\n\n/**\n * Makes sure to auto-expire and clean up things that time out or for other\n * reasons should not be left lingering.\n */\nexport class PluginTaskSchedulerJanitor {\n private readonly knex: Knex;\n private readonly waitBetweenRuns: Duration;\n private readonly logger: LoggerService;\n\n constructor(options: {\n knex: Knex;\n waitBetweenRuns: Duration;\n logger: LoggerService;\n }) {\n this.knex = options.knex;\n this.waitBetweenRuns = options.waitBetweenRuns;\n this.logger = options.logger;\n }\n\n async start(abortSignal?: AbortSignal) {\n while (!abortSignal?.aborted) {\n try {\n await this.runOnce();\n } catch (e) {\n this.logger.warn(`Error while performing janitorial tasks, ${e}`);\n }\n\n await sleep(this.waitBetweenRuns, abortSignal);\n }\n }\n\n private async runOnce() {\n const dbNull = this.knex.raw('null');\n const configClient = this.knex.client.config.client;\n\n let tasks: Array<{ id: string }>;\n if (configClient.includes('sqlite3') || configClient.includes('mysql')) {\n tasks = await this.knex<DbTasksRow>(DB_TASKS_TABLE)\n .select('id')\n .where('current_run_expires_at', '<', this.knex.fn.now());\n await this.knex<DbTasksRow>(DB_TASKS_TABLE)\n .whereIn(\n 'id',\n tasks.map(t => t.id),\n )\n .update({\n current_run_ticket: dbNull,\n current_run_started_at: dbNull,\n current_run_expires_at: dbNull,\n });\n } else {\n tasks = await this.knex<DbTasksRow>(DB_TASKS_TABLE)\n .where('current_run_expires_at', '<', this.knex.fn.now())\n .update({\n current_run_ticket: dbNull,\n current_run_started_at: dbNull,\n current_run_expires_at: dbNull,\n })\n .returning(['id']);\n }\n\n // In rare cases, knex drivers may ignore \"returning\", and return the number\n // of rows changed instead\n if (typeof tasks === 'number') {\n if (tasks > 0) {\n this.logger.warn(`${tasks} tasks timed out and were lost`);\n }\n } else {\n for (const { id } of tasks) {\n this.logger.warn(`Task timed out and was lost: ${id}`);\n }\n }\n }\n}\n"],"names":["sleep","DB_TASKS_TABLE"],"mappings":";;;;;AA0BO,MAAM,0BAA2B,CAAA;AAAA,EACrB,IAAA;AAAA,EACA,eAAA;AAAA,EACA,MAAA;AAAA,EAEjB,YAAY,OAIT,EAAA;AACD,IAAA,IAAA,CAAK,OAAO,OAAQ,CAAA,IAAA;AACpB,IAAA,IAAA,CAAK,kBAAkB,OAAQ,CAAA,eAAA;AAC/B,IAAA,IAAA,CAAK,SAAS,OAAQ,CAAA,MAAA;AAAA;AACxB,EAEA,MAAM,MAAM,WAA2B,EAAA;AACrC,IAAO,OAAA,CAAC,aAAa,OAAS,EAAA;AAC5B,MAAI,IAAA;AACF,QAAA,MAAM,KAAK,OAAQ,EAAA;AAAA,eACZ,CAAG,EAAA;AACV,QAAA,IAAA,CAAK,MAAO,CAAA,IAAA,CAAK,CAA4C,yCAAA,EAAA,CAAC,CAAE,CAAA,CAAA;AAAA;AAGlE,MAAM,MAAAA,UAAA,CAAM,IAAK,CAAA,eAAA,EAAiB,WAAW,CAAA;AAAA;AAC/C;AACF,EAEA,MAAc,OAAU,GAAA;AACtB,IAAA,MAAM,MAAS,GAAA,IAAA,CAAK,IAAK,CAAA,GAAA,CAAI,MAAM,CAAA;AACnC,IAAA,MAAM,YAAe,GAAA,IAAA,CAAK,IAAK,CAAA,MAAA,CAAO,MAAO,CAAA,MAAA;AAE7C,IAAI,IAAA,KAAA;AACJ,IAAA,IAAI,aAAa,QAAS,CAAA,SAAS,KAAK,YAAa,CAAA,QAAA,CAAS,OAAO,CAAG,EAAA;AACtE,MAAA,KAAA,GAAQ,MAAM,IAAA,CAAK,IAAiB,CAAAC,qBAAc,EAC/C,MAAO,CAAA,IAAI,CACX,CAAA,KAAA,CAAM,0BAA0B,GAAK,EAAA,IAAA,CAAK,IAAK,CAAA,EAAA,CAAG,KAAK,CAAA;AAC1D,MAAM,MAAA,IAAA,CAAK,IAAiB,CAAAA,qBAAc,CACvC,CAAA,OAAA;AAAA,QACC,IAAA;AAAA,QACA,KAAM,CAAA,GAAA,CAAI,CAAK,CAAA,KAAA,CAAA,CAAE,EAAE;AAAA,QAEpB,MAAO,CAAA;AAAA,QACN,kBAAoB,EAAA,MAAA;AAAA,QACpB,sBAAwB,EAAA,MAAA;AAAA,QACxB,sBAAwB,EAAA;AAAA,
|
|
1
|
+
{"version":3,"file":"PluginTaskSchedulerJanitor.cjs.js","sources":["../../../../src/entrypoints/scheduler/lib/PluginTaskSchedulerJanitor.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { LoggerService } from '@backstage/backend-plugin-api';\nimport { Knex } from 'knex';\nimport { Duration } from 'luxon';\nimport { DB_TASKS_TABLE, DbTasksRow } from '../database/tables';\nimport { serializeError, sleep } from './util';\n\n/**\n * Makes sure to auto-expire and clean up things that time out or for other\n * reasons should not be left lingering.\n */\nexport class PluginTaskSchedulerJanitor {\n private readonly knex: Knex;\n private readonly waitBetweenRuns: Duration;\n private readonly logger: LoggerService;\n\n constructor(options: {\n knex: Knex;\n waitBetweenRuns: Duration;\n logger: LoggerService;\n }) {\n this.knex = options.knex;\n this.waitBetweenRuns = options.waitBetweenRuns;\n this.logger = options.logger;\n }\n\n async start(abortSignal?: AbortSignal) {\n while (!abortSignal?.aborted) {\n try {\n await this.runOnce();\n } catch (e) {\n this.logger.warn(`Error while performing janitorial tasks, ${e}`);\n }\n\n await sleep(this.waitBetweenRuns, abortSignal);\n }\n }\n\n private async runOnce() {\n const dbNull = this.knex.raw('null');\n const configClient = this.knex.client.config.client;\n\n let tasks: Array<{ id: string }>;\n if (configClient.includes('sqlite3') || configClient.includes('mysql')) {\n tasks = await this.knex<DbTasksRow>(DB_TASKS_TABLE)\n .select('id')\n .where('current_run_expires_at', '<', this.knex.fn.now());\n await this.knex<DbTasksRow>(DB_TASKS_TABLE)\n .whereIn(\n 'id',\n tasks.map(t => t.id),\n )\n .update({\n current_run_ticket: dbNull,\n current_run_started_at: dbNull,\n current_run_expires_at: dbNull,\n last_run_ended_at: this.knex.fn.now(),\n last_run_error_json: serializeError(new Error('Task timed out')),\n });\n } else {\n tasks = await this.knex<DbTasksRow>(DB_TASKS_TABLE)\n .where('current_run_expires_at', '<', this.knex.fn.now())\n .update({\n current_run_ticket: dbNull,\n current_run_started_at: dbNull,\n current_run_expires_at: dbNull,\n last_run_ended_at: this.knex.fn.now(),\n last_run_error_json: serializeError(new Error('Task timed out')),\n })\n .returning(['id']);\n }\n\n // In rare cases, knex drivers may ignore \"returning\", and return the number\n // of rows changed instead\n if (typeof tasks === 'number') {\n if (tasks > 0) {\n this.logger.warn(`${tasks} tasks timed out and were lost`);\n }\n } else {\n for (const { id } of tasks) {\n this.logger.warn(`Task timed out and was lost: ${id}`);\n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D;AACF;AAEJ;;;;"}
|
|
@@ -17,6 +17,9 @@ class TaskWorker {
|
|
|
17
17
|
this.logger = logger;
|
|
18
18
|
this.workCheckFrequency = workCheckFrequency;
|
|
19
19
|
}
|
|
20
|
+
#workerState = {
|
|
21
|
+
status: "idle"
|
|
22
|
+
};
|
|
20
23
|
async start(settings, options) {
|
|
21
24
|
try {
|
|
22
25
|
await this.persistTask(settings);
|
|
@@ -34,16 +37,11 @@ class TaskWorker {
|
|
|
34
37
|
workCheckFrequency = cadence;
|
|
35
38
|
}
|
|
36
39
|
}
|
|
37
|
-
let attemptNum = 1;
|
|
38
40
|
(async () => {
|
|
41
|
+
let attemptNum = 1;
|
|
39
42
|
for (; ; ) {
|
|
40
43
|
try {
|
|
41
|
-
|
|
42
|
-
await util.sleep(
|
|
43
|
-
luxon.Duration.fromISO(settings.initialDelayDuration),
|
|
44
|
-
options.signal
|
|
45
|
-
);
|
|
46
|
-
}
|
|
44
|
+
await this.performInitialWait(settings, options.signal);
|
|
47
45
|
while (!options.signal.aborted) {
|
|
48
46
|
const runResult = await this.runOnce(options.signal);
|
|
49
47
|
if (runResult.result === "abort") {
|
|
@@ -64,6 +62,20 @@ class TaskWorker {
|
|
|
64
62
|
}
|
|
65
63
|
})();
|
|
66
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Does the once-at-startup initial wait, if configured.
|
|
67
|
+
*/
|
|
68
|
+
async performInitialWait(settings, signal) {
|
|
69
|
+
if (settings.initialDelayDuration) {
|
|
70
|
+
this.#workerState = {
|
|
71
|
+
status: "initial-wait"
|
|
72
|
+
};
|
|
73
|
+
await util.sleep(luxon.Duration.fromISO(settings.initialDelayDuration), signal);
|
|
74
|
+
}
|
|
75
|
+
this.#workerState = {
|
|
76
|
+
status: "idle"
|
|
77
|
+
};
|
|
78
|
+
}
|
|
67
79
|
static async trigger(knex, taskId) {
|
|
68
80
|
const rows = await knex(tables.DB_TASKS_TABLE).select(knex.raw(1)).where("id", "=", taskId);
|
|
69
81
|
if (rows.length !== 1) {
|
|
@@ -76,6 +88,36 @@ class TaskWorker {
|
|
|
76
88
|
throw new errors.ConflictError(`Task ${taskId} is currently running`);
|
|
77
89
|
}
|
|
78
90
|
}
|
|
91
|
+
static async taskStates(knex) {
|
|
92
|
+
const rows = await knex(tables.DB_TASKS_TABLE);
|
|
93
|
+
return new Map(
|
|
94
|
+
rows.map((row) => {
|
|
95
|
+
const startedAt = row.current_run_started_at ? util.dbTime(row.current_run_started_at).toISO() : void 0;
|
|
96
|
+
const timesOutAt = row.current_run_expires_at ? util.dbTime(row.current_run_expires_at).toISO() : void 0;
|
|
97
|
+
const startsAt = row.next_run_start_at ? util.dbTime(row.next_run_start_at).toISO() : void 0;
|
|
98
|
+
const lastRunEndedAt = row.last_run_ended_at ? util.dbTime(row.last_run_ended_at).toISO() : void 0;
|
|
99
|
+
const lastRunError = row.last_run_error_json || void 0;
|
|
100
|
+
return [
|
|
101
|
+
row.id,
|
|
102
|
+
startedAt ? {
|
|
103
|
+
status: "running",
|
|
104
|
+
startedAt,
|
|
105
|
+
timesOutAt,
|
|
106
|
+
lastRunEndedAt,
|
|
107
|
+
lastRunError
|
|
108
|
+
} : {
|
|
109
|
+
status: "idle",
|
|
110
|
+
startsAt,
|
|
111
|
+
lastRunEndedAt,
|
|
112
|
+
lastRunError
|
|
113
|
+
}
|
|
114
|
+
];
|
|
115
|
+
})
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
workerState() {
|
|
119
|
+
return this.#workerState;
|
|
120
|
+
}
|
|
79
121
|
/**
|
|
80
122
|
* Makes a single attempt at running the task to completion, if ready.
|
|
81
123
|
*
|
|
@@ -97,13 +139,19 @@ class TaskWorker {
|
|
|
97
139
|
taskAbortController.abort();
|
|
98
140
|
}, luxon.Duration.fromISO(taskSettings.timeoutAfterDuration).as("milliseconds"));
|
|
99
141
|
try {
|
|
142
|
+
this.#workerState = {
|
|
143
|
+
status: "running"
|
|
144
|
+
};
|
|
100
145
|
await this.fn(taskAbortController.signal);
|
|
101
146
|
taskAbortController.abort();
|
|
102
147
|
} catch (e) {
|
|
103
148
|
this.logger.error(e);
|
|
104
|
-
await this.tryReleaseTask(ticket, taskSettings);
|
|
149
|
+
await this.tryReleaseTask(ticket, taskSettings, e);
|
|
105
150
|
return { result: "failed" };
|
|
106
151
|
} finally {
|
|
152
|
+
this.#workerState = {
|
|
153
|
+
status: "idle"
|
|
154
|
+
};
|
|
107
155
|
clearTimeout(timeoutHandle);
|
|
108
156
|
}
|
|
109
157
|
await this.tryReleaseTask(ticket, taskSettings);
|
|
@@ -220,7 +268,7 @@ class TaskWorker {
|
|
|
220
268
|
});
|
|
221
269
|
return rows === 1;
|
|
222
270
|
}
|
|
223
|
-
async tryReleaseTask(ticket, settings) {
|
|
271
|
+
async tryReleaseTask(ticket, settings, error) {
|
|
224
272
|
const isManual = settings?.cadence === "manual";
|
|
225
273
|
const isDuration = settings?.cadence.startsWith("P");
|
|
226
274
|
const isCron = !isManual && !isDuration;
|
|
@@ -257,7 +305,9 @@ class TaskWorker {
|
|
|
257
305
|
next_run_start_at: nextRun,
|
|
258
306
|
current_run_ticket: this.knex.raw("null"),
|
|
259
307
|
current_run_started_at: this.knex.raw("null"),
|
|
260
|
-
current_run_expires_at: this.knex.raw("null")
|
|
308
|
+
current_run_expires_at: this.knex.raw("null"),
|
|
309
|
+
last_run_ended_at: this.knex.fn.now(),
|
|
310
|
+
last_run_error_json: error ? util.serializeError(error) : this.knex.raw("null")
|
|
261
311
|
});
|
|
262
312
|
return rows === 1;
|
|
263
313
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TaskWorker.cjs.js","sources":["../../../../src/entrypoints/scheduler/lib/TaskWorker.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { LoggerService } from '@backstage/backend-plugin-api';\nimport { ConflictError, NotFoundError } from '@backstage/errors';\nimport { CronTime } from 'cron';\nimport { Knex } from 'knex';\nimport { DateTime, Duration } from 'luxon';\nimport { v4 as uuid } from 'uuid';\nimport { DB_TASKS_TABLE, DbTasksRow } from '../database/tables';\nimport { TaskSettingsV2, taskSettingsV2Schema } from './types';\nimport { delegateAbortController, nowPlus, sleep } from './util';\nimport { SchedulerServiceTaskFunction } from '@backstage/backend-plugin-api';\n\nconst DEFAULT_WORK_CHECK_FREQUENCY = Duration.fromObject({ seconds: 5 });\n\n/**\n * Implements tasks that run across worker hosts, with collaborative locking.\n *\n * @private\n */\nexport class TaskWorker {\n constructor(\n private readonly taskId: string,\n private readonly fn: SchedulerServiceTaskFunction,\n private readonly knex: Knex,\n private readonly logger: LoggerService,\n private readonly workCheckFrequency: Duration = DEFAULT_WORK_CHECK_FREQUENCY,\n ) {}\n\n async start(settings: TaskSettingsV2, options: { signal: AbortSignal }) {\n try {\n await this.persistTask(settings);\n } catch (e) {\n throw new Error(`Failed to persist task, ${e}`);\n }\n\n this.logger.info(\n `Task worker starting: ${this.taskId}, ${JSON.stringify(settings)}`,\n );\n\n let workCheckFrequency = this.workCheckFrequency;\n const isDuration = settings?.cadence.startsWith('P');\n if (isDuration) {\n const cadence = Duration.fromISO(settings.cadence);\n if (cadence < workCheckFrequency) {\n workCheckFrequency = cadence;\n }\n }\n\n let attemptNum = 1;\n (async () => {\n for (;;) {\n try {\n if (settings.initialDelayDuration) {\n await sleep(\n Duration.fromISO(settings.initialDelayDuration),\n options.signal,\n );\n }\n\n while (!options.signal.aborted) {\n const runResult = await this.runOnce(options.signal);\n\n if (runResult.result === 'abort') {\n break;\n }\n\n await sleep(workCheckFrequency, options.signal);\n }\n\n this.logger.info(`Task worker finished: ${this.taskId}`);\n attemptNum = 0;\n break;\n } catch (e) {\n attemptNum += 1;\n this.logger.warn(\n `Task worker failed unexpectedly, attempt number ${attemptNum}, ${e}`,\n );\n await sleep(Duration.fromObject({ seconds: 1 }));\n }\n }\n })();\n }\n\n static async trigger(knex: Knex, taskId: string): Promise<void> {\n // check if task exists\n const rows = await knex<DbTasksRow>(DB_TASKS_TABLE)\n .select(knex.raw(1))\n .where('id', '=', taskId);\n if (rows.length !== 1) {\n throw new NotFoundError(`Task ${taskId} does not exist`);\n }\n\n const updatedRows = await knex<DbTasksRow>(DB_TASKS_TABLE)\n .where('id', '=', taskId)\n .whereNull('current_run_ticket')\n .update({\n next_run_start_at: knex.fn.now(),\n });\n if (updatedRows < 1) {\n throw new ConflictError(`Task ${taskId} is currently running`);\n }\n }\n\n /**\n * Makes a single attempt at running the task to completion, if ready.\n *\n * @returns The outcome of the attempt\n */\n private async runOnce(\n signal: AbortSignal,\n ): Promise<\n | { result: 'not-ready-yet' }\n | { result: 'abort' }\n | { result: 'failed' }\n | { result: 'completed' }\n > {\n const findResult = await this.findReadyTask();\n if (\n findResult.result === 'not-ready-yet' ||\n findResult.result === 'abort'\n ) {\n return findResult;\n }\n\n const taskSettings = findResult.settings;\n const ticket = uuid();\n\n const claimed = await this.tryClaimTask(ticket, taskSettings);\n if (!claimed) {\n return { result: 'not-ready-yet' };\n }\n\n // Abort the task execution either if the worker is stopped, or if the\n // task timeout is hit\n const taskAbortController = delegateAbortController(signal);\n const timeoutHandle = setTimeout(() => {\n taskAbortController.abort();\n }, Duration.fromISO(taskSettings.timeoutAfterDuration).as('milliseconds'));\n\n try {\n await this.fn(taskAbortController.signal);\n taskAbortController.abort(); // releases resources\n } catch (e) {\n this.logger.error(e);\n await this.tryReleaseTask(ticket, taskSettings);\n return { result: 'failed' };\n } finally {\n clearTimeout(timeoutHandle);\n }\n\n await this.tryReleaseTask(ticket, taskSettings);\n return { result: 'completed' };\n }\n\n /**\n * Perform the initial store of the task info\n */\n async persistTask(settings: TaskSettingsV2) {\n // Perform an initial parse to ensure that we will definitely be able to\n // read it back again.\n taskSettingsV2Schema.parse(settings);\n\n const isManual = settings?.cadence === 'manual';\n const isDuration = settings?.cadence.startsWith('P');\n const isCron = !isManual && !isDuration;\n\n let startAt: Knex.Raw | undefined;\n let nextStartAt: Knex.Raw | undefined;\n if (settings.initialDelayDuration) {\n startAt = nowPlus(\n Duration.fromISO(settings.initialDelayDuration),\n this.knex,\n );\n }\n\n if (isCron) {\n const time = new CronTime(settings.cadence)\n .sendAt()\n .minus({ seconds: 1 }) // immediately, if \"* * * * * *\"\n .toUTC();\n\n nextStartAt = this.nextRunAtRaw(time);\n startAt ||= nextStartAt;\n } else if (isManual) {\n nextStartAt = this.knex.raw('null');\n startAt ||= nextStartAt;\n } else {\n startAt ||= this.knex.fn.now();\n nextStartAt = nowPlus(Duration.fromISO(settings.cadence), this.knex);\n }\n\n this.logger.debug(`task: ${this.taskId} configured to run at: ${startAt}`);\n\n // It's OK if the task already exists; if it does, just replace its\n // settings with the new value and start the loop as usual.\n const settingsJson = JSON.stringify(settings);\n await this.knex<DbTasksRow>(DB_TASKS_TABLE)\n .insert({\n id: this.taskId,\n settings_json: settingsJson,\n next_run_start_at: startAt,\n })\n .onConflict('id')\n .merge(\n this.knex.client.config.client.includes('mysql')\n ? {\n settings_json: settingsJson,\n next_run_start_at: this.knex.raw(\n `CASE WHEN ?? < ?? THEN ?? ELSE ?? END`,\n [\n nextStartAt,\n 'next_run_start_at',\n nextStartAt,\n 'next_run_start_at',\n ],\n ),\n }\n : {\n settings_json: this.knex.ref('excluded.settings_json'),\n next_run_start_at: this.knex.raw(\n `CASE WHEN ?? < ?? THEN ?? ELSE ?? END`,\n [\n nextStartAt,\n `${DB_TASKS_TABLE}.next_run_start_at`,\n nextStartAt,\n `${DB_TASKS_TABLE}.next_run_start_at`,\n ],\n ),\n },\n );\n }\n\n /**\n * Check if the task is ready to run\n */\n async findReadyTask(): Promise<\n | { result: 'not-ready-yet' }\n | { result: 'abort' }\n | { result: 'ready'; settings: TaskSettingsV2 }\n > {\n const [row] = await this.knex<DbTasksRow>(DB_TASKS_TABLE)\n .where('id', '=', this.taskId)\n .select({\n settingsJson: 'settings_json',\n ready: this.knex.raw(\n `CASE\n WHEN next_run_start_at <= ? AND current_run_ticket IS NULL THEN TRUE\n ELSE FALSE\n END`,\n [this.knex.fn.now()],\n ),\n });\n\n if (!row) {\n this.logger.info(\n 'No longer able to find task; aborting and assuming that it has been unregistered or expired',\n );\n return { result: 'abort' };\n } else if (!row.ready) {\n return { result: 'not-ready-yet' };\n }\n\n try {\n const obj = JSON.parse(row.settingsJson);\n const settings = taskSettingsV2Schema.parse(obj);\n return { result: 'ready', settings };\n } catch (e) {\n this.logger.info(\n `Task \"${this.taskId}\" is no longer able to parse task settings; aborting and assuming that a ` +\n `newer version of the task has been issued and being handled by other workers, ${e}`,\n );\n return { result: 'abort' };\n }\n }\n\n /**\n * Attempts to claim a task that's ready for execution, on this worker's\n * behalf. We should not attempt to perform the work unless the claim really\n * goes through.\n *\n * @param ticket - A globally unique string that changes for each invocation\n * @param settings - The settings of the task to claim\n * @returns True if it was successfully claimed\n */\n async tryClaimTask(\n ticket: string,\n settings: TaskSettingsV2,\n ): Promise<boolean> {\n const startedAt = this.knex.fn.now();\n const expiresAt = settings.timeoutAfterDuration\n ? nowPlus(Duration.fromISO(settings.timeoutAfterDuration), this.knex)\n : this.knex.raw('null');\n\n const rows = await this.knex<DbTasksRow>(DB_TASKS_TABLE)\n .where('id', '=', this.taskId)\n .whereNull('current_run_ticket')\n .update({\n current_run_ticket: ticket,\n current_run_started_at: startedAt,\n current_run_expires_at: expiresAt,\n });\n\n return rows === 1;\n }\n\n async tryReleaseTask(\n ticket: string,\n settings: TaskSettingsV2,\n ): Promise<boolean> {\n const isManual = settings?.cadence === 'manual';\n const isDuration = settings?.cadence.startsWith('P');\n const isCron = !isManual && !isDuration;\n\n let nextRun: Knex.Raw;\n if (isCron) {\n const time = new CronTime(settings.cadence).sendAt().toUTC();\n this.logger.debug(`task: ${this.taskId} will next occur around ${time}`);\n\n nextRun = this.nextRunAtRaw(time);\n } else if (isManual) {\n nextRun = this.knex.raw('null');\n } else {\n const dt = Duration.fromISO(settings.cadence).as('seconds');\n this.logger.debug(\n `task: ${this.taskId} will next occur around ${DateTime.now().plus({\n seconds: dt,\n })}`,\n );\n\n if (this.knex.client.config.client.includes('sqlite3')) {\n nextRun = this.knex.raw(\n `max(datetime(next_run_start_at, ?), datetime('now'))`,\n [`+${dt} seconds`],\n );\n } else if (this.knex.client.config.client.includes('mysql')) {\n nextRun = this.knex.raw(\n `greatest(next_run_start_at + interval ${dt} second, now())`,\n );\n } else {\n nextRun = this.knex.raw(\n `greatest(next_run_start_at + interval '${dt} seconds', now())`,\n );\n }\n }\n\n const rows = await this.knex<DbTasksRow>(DB_TASKS_TABLE)\n .where('id', '=', this.taskId)\n .where('current_run_ticket', '=', ticket)\n .update({\n next_run_start_at: nextRun,\n current_run_ticket: this.knex.raw('null'),\n current_run_started_at: this.knex.raw('null'),\n current_run_expires_at: this.knex.raw('null'),\n });\n\n return rows === 1;\n }\n\n private nextRunAtRaw(time: DateTime): Knex.Raw {\n if (this.knex.client.config.client.includes('sqlite3')) {\n return this.knex.raw('datetime(?)', [time.toISO()]);\n } else if (this.knex.client.config.client.includes('mysql')) {\n return this.knex.raw(`?`, [time.toSQL({ includeOffset: false })]);\n }\n return this.knex.raw(`?`, [time.toISO()]);\n }\n}\n"],"names":["Duration","sleep","DB_TASKS_TABLE","NotFoundError","ConflictError","uuid","delegateAbortController","taskSettingsV2Schema","nowPlus","CronTime","DateTime"],"mappings":";;;;;;;;;;AA2BA,MAAM,+BAA+BA,cAAS,CAAA,UAAA,CAAW,EAAE,OAAA,EAAS,GAAG,CAAA;AAOhE,MAAM,UAAW,CAAA;AAAA,EACtB,YACmB,MACA,EAAA,EAAA,EACA,IACA,EAAA,MAAA,EACA,qBAA+B,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O,IAAS,KAAA,CAAA;AAAA;AAClB,EAEQ,aAAa,IAA0B,EAAA;AAC7C,IAAA,IAAI,KAAK,IAAK,CAAA,MAAA,CAAO,OAAO,MAAO,CAAA,QAAA,CAAS,SAAS,CAAG,EAAA;AACtD,MAAO,OAAA,IAAA,CAAK,KAAK,GAAI,CAAA,aAAA,EAAe,CAAC,IAAK,CAAA,KAAA,EAAO,CAAC,CAAA;AAAA,KACpD,MAAA,IAAW,KAAK,IAAK,CAAA,MAAA,CAAO,OAAO,MAAO,CAAA,QAAA,CAAS,OAAO,CAAG,EAAA;AAC3D,MAAA,OAAO,IAAK,CAAA,IAAA,CAAK,GAAI,CAAA,CAAA,CAAA,CAAA,EAAK,CAAC,IAAA,CAAK,KAAM,CAAA,EAAE,aAAe,EAAA,KAAA,EAAO,CAAC,CAAC,CAAA;AAAA;AAElE,IAAO,OAAA,IAAA,CAAK,KAAK,GAAI,CAAA,CAAA,CAAA,CAAA,EAAK,CAAC,IAAK,CAAA,KAAA,EAAO,CAAC,CAAA;AAAA;AAE5C;;;;"}
|
|
1
|
+
{"version":3,"file":"TaskWorker.cjs.js","sources":["../../../../src/entrypoints/scheduler/lib/TaskWorker.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { LoggerService } from '@backstage/backend-plugin-api';\nimport { ConflictError, NotFoundError } from '@backstage/errors';\nimport { CronTime } from 'cron';\nimport { Knex } from 'knex';\nimport { DateTime, Duration } from 'luxon';\nimport { v4 as uuid } from 'uuid';\nimport { DB_TASKS_TABLE, DbTasksRow } from '../database/tables';\nimport {\n TaskSettingsV2,\n taskSettingsV2Schema,\n TaskApiTasksResponse,\n} from './types';\nimport {\n delegateAbortController,\n nowPlus,\n sleep,\n dbTime,\n serializeError,\n} from './util';\nimport { SchedulerServiceTaskFunction } from '@backstage/backend-plugin-api';\n\nconst DEFAULT_WORK_CHECK_FREQUENCY = Duration.fromObject({ seconds: 5 });\n\n/**\n * Implements tasks that run across worker hosts, with collaborative locking.\n *\n * @private\n */\nexport class TaskWorker {\n #workerState: TaskApiTasksResponse['workerState'] = {\n status: 'idle',\n };\n\n constructor(\n private readonly taskId: string,\n private readonly fn: SchedulerServiceTaskFunction,\n private readonly knex: Knex,\n private readonly logger: LoggerService,\n private readonly workCheckFrequency: Duration = DEFAULT_WORK_CHECK_FREQUENCY,\n ) {}\n\n async start(settings: TaskSettingsV2, options: { signal: AbortSignal }) {\n try {\n await this.persistTask(settings);\n } catch (e) {\n throw new Error(`Failed to persist task, ${e}`);\n }\n\n this.logger.info(\n `Task worker starting: ${this.taskId}, ${JSON.stringify(settings)}`,\n );\n\n let workCheckFrequency = this.workCheckFrequency;\n const isDuration = settings?.cadence.startsWith('P');\n if (isDuration) {\n const cadence = Duration.fromISO(settings.cadence);\n if (cadence < workCheckFrequency) {\n workCheckFrequency = cadence;\n }\n }\n\n (async () => {\n let attemptNum = 1;\n for (;;) {\n try {\n await this.performInitialWait(settings, options.signal);\n\n while (!options.signal.aborted) {\n const runResult = await this.runOnce(options.signal);\n if (runResult.result === 'abort') {\n break;\n }\n await sleep(workCheckFrequency, options.signal);\n }\n\n this.logger.info(`Task worker finished: ${this.taskId}`);\n attemptNum = 0;\n break;\n } catch (e) {\n attemptNum += 1;\n this.logger.warn(\n `Task worker failed unexpectedly, attempt number ${attemptNum}, ${e}`,\n );\n await sleep(Duration.fromObject({ seconds: 1 }));\n }\n }\n })();\n }\n\n /**\n * Does the once-at-startup initial wait, if configured.\n */\n private async performInitialWait(\n settings: TaskSettingsV2,\n signal: AbortSignal,\n ): Promise<void> {\n if (settings.initialDelayDuration) {\n this.#workerState = {\n status: 'initial-wait',\n };\n await sleep(Duration.fromISO(settings.initialDelayDuration), signal);\n }\n this.#workerState = {\n status: 'idle',\n };\n }\n\n static async trigger(knex: Knex, taskId: string): Promise<void> {\n // check if task exists\n const rows = await knex<DbTasksRow>(DB_TASKS_TABLE)\n .select(knex.raw(1))\n .where('id', '=', taskId);\n if (rows.length !== 1) {\n throw new NotFoundError(`Task ${taskId} does not exist`);\n }\n\n const updatedRows = await knex<DbTasksRow>(DB_TASKS_TABLE)\n .where('id', '=', taskId)\n .whereNull('current_run_ticket')\n .update({\n next_run_start_at: knex.fn.now(),\n });\n if (updatedRows < 1) {\n throw new ConflictError(`Task ${taskId} is currently running`);\n }\n }\n\n static async taskStates(\n knex: Knex,\n ): Promise<Map<string, TaskApiTasksResponse['taskState']>> {\n const rows = await knex<DbTasksRow>(DB_TASKS_TABLE);\n return new Map(\n rows.map(row => {\n const startedAt = row.current_run_started_at\n ? dbTime(row.current_run_started_at).toISO()!\n : undefined;\n const timesOutAt = row.current_run_expires_at\n ? dbTime(row.current_run_expires_at).toISO()!\n : undefined;\n const startsAt = row.next_run_start_at\n ? dbTime(row.next_run_start_at).toISO()!\n : undefined;\n const lastRunEndedAt = row.last_run_ended_at\n ? dbTime(row.last_run_ended_at).toISO()!\n : undefined;\n const lastRunError = row.last_run_error_json || undefined;\n\n return [\n row.id,\n startedAt\n ? {\n status: 'running',\n startedAt,\n timesOutAt,\n lastRunEndedAt,\n lastRunError,\n }\n : {\n status: 'idle',\n startsAt,\n lastRunEndedAt,\n lastRunError,\n },\n ];\n }),\n );\n }\n\n workerState(): TaskApiTasksResponse['workerState'] {\n return this.#workerState;\n }\n\n /**\n * Makes a single attempt at running the task to completion, if ready.\n *\n * @returns The outcome of the attempt\n */\n private async runOnce(\n signal: AbortSignal,\n ): Promise<\n | { result: 'not-ready-yet' }\n | { result: 'abort' }\n | { result: 'failed' }\n | { result: 'completed' }\n > {\n const findResult = await this.findReadyTask();\n if (\n findResult.result === 'not-ready-yet' ||\n findResult.result === 'abort'\n ) {\n return findResult;\n }\n\n const taskSettings = findResult.settings;\n const ticket = uuid();\n\n const claimed = await this.tryClaimTask(ticket, taskSettings);\n if (!claimed) {\n return { result: 'not-ready-yet' };\n }\n\n // Abort the task execution either if the worker is stopped, or if the\n // task timeout is hit\n const taskAbortController = delegateAbortController(signal);\n const timeoutHandle = setTimeout(() => {\n taskAbortController.abort();\n }, Duration.fromISO(taskSettings.timeoutAfterDuration).as('milliseconds'));\n\n try {\n this.#workerState = {\n status: 'running',\n };\n await this.fn(taskAbortController.signal);\n taskAbortController.abort(); // releases resources\n } catch (e) {\n this.logger.error(e);\n await this.tryReleaseTask(ticket, taskSettings, e);\n return { result: 'failed' };\n } finally {\n this.#workerState = {\n status: 'idle',\n };\n clearTimeout(timeoutHandle);\n }\n\n await this.tryReleaseTask(ticket, taskSettings);\n return { result: 'completed' };\n }\n\n /**\n * Perform the initial store of the task info\n */\n async persistTask(settings: TaskSettingsV2) {\n // Perform an initial parse to ensure that we will definitely be able to\n // read it back again.\n taskSettingsV2Schema.parse(settings);\n\n const isManual = settings?.cadence === 'manual';\n const isDuration = settings?.cadence.startsWith('P');\n const isCron = !isManual && !isDuration;\n\n let startAt: Knex.Raw | undefined;\n let nextStartAt: Knex.Raw | undefined;\n if (settings.initialDelayDuration) {\n startAt = nowPlus(\n Duration.fromISO(settings.initialDelayDuration),\n this.knex,\n );\n }\n\n if (isCron) {\n const time = new CronTime(settings.cadence)\n .sendAt()\n .minus({ seconds: 1 }) // immediately, if \"* * * * * *\"\n .toUTC();\n\n nextStartAt = this.nextRunAtRaw(time);\n startAt ||= nextStartAt;\n } else if (isManual) {\n nextStartAt = this.knex.raw('null');\n startAt ||= nextStartAt;\n } else {\n startAt ||= this.knex.fn.now();\n nextStartAt = nowPlus(Duration.fromISO(settings.cadence), this.knex);\n }\n\n this.logger.debug(`task: ${this.taskId} configured to run at: ${startAt}`);\n\n // It's OK if the task already exists; if it does, just replace its\n // settings with the new value and start the loop as usual.\n const settingsJson = JSON.stringify(settings);\n await this.knex<DbTasksRow>(DB_TASKS_TABLE)\n .insert({\n id: this.taskId,\n settings_json: settingsJson,\n next_run_start_at: startAt,\n })\n .onConflict('id')\n .merge(\n this.knex.client.config.client.includes('mysql')\n ? {\n settings_json: settingsJson,\n next_run_start_at: this.knex.raw(\n `CASE WHEN ?? < ?? THEN ?? ELSE ?? END`,\n [\n nextStartAt,\n 'next_run_start_at',\n nextStartAt,\n 'next_run_start_at',\n ],\n ),\n }\n : {\n settings_json: this.knex.ref('excluded.settings_json'),\n next_run_start_at: this.knex.raw(\n `CASE WHEN ?? < ?? THEN ?? ELSE ?? END`,\n [\n nextStartAt,\n `${DB_TASKS_TABLE}.next_run_start_at`,\n nextStartAt,\n `${DB_TASKS_TABLE}.next_run_start_at`,\n ],\n ),\n },\n );\n }\n\n /**\n * Check if the task is ready to run\n */\n async findReadyTask(): Promise<\n | { result: 'not-ready-yet' }\n | { result: 'abort' }\n | { result: 'ready'; settings: TaskSettingsV2 }\n > {\n const [row] = await this.knex<DbTasksRow>(DB_TASKS_TABLE)\n .where('id', '=', this.taskId)\n .select({\n settingsJson: 'settings_json',\n ready: this.knex.raw(\n `CASE\n WHEN next_run_start_at <= ? AND current_run_ticket IS NULL THEN TRUE\n ELSE FALSE\n END`,\n [this.knex.fn.now()],\n ),\n });\n\n if (!row) {\n this.logger.info(\n 'No longer able to find task; aborting and assuming that it has been unregistered or expired',\n );\n return { result: 'abort' };\n } else if (!row.ready) {\n return { result: 'not-ready-yet' };\n }\n\n try {\n const obj = JSON.parse(row.settingsJson);\n const settings = taskSettingsV2Schema.parse(obj);\n return { result: 'ready', settings };\n } catch (e) {\n this.logger.info(\n `Task \"${this.taskId}\" is no longer able to parse task settings; aborting and assuming that a ` +\n `newer version of the task has been issued and being handled by other workers, ${e}`,\n );\n return { result: 'abort' };\n }\n }\n\n /**\n * Attempts to claim a task that's ready for execution, on this worker's\n * behalf. We should not attempt to perform the work unless the claim really\n * goes through.\n *\n * @param ticket - A globally unique string that changes for each invocation\n * @param settings - The settings of the task to claim\n * @returns True if it was successfully claimed\n */\n async tryClaimTask(\n ticket: string,\n settings: TaskSettingsV2,\n ): Promise<boolean> {\n const startedAt = this.knex.fn.now();\n const expiresAt = settings.timeoutAfterDuration\n ? nowPlus(Duration.fromISO(settings.timeoutAfterDuration), this.knex)\n : this.knex.raw('null');\n\n const rows = await this.knex<DbTasksRow>(DB_TASKS_TABLE)\n .where('id', '=', this.taskId)\n .whereNull('current_run_ticket')\n .update({\n current_run_ticket: ticket,\n current_run_started_at: startedAt,\n current_run_expires_at: expiresAt,\n });\n\n return rows === 1;\n }\n\n async tryReleaseTask(\n ticket: string,\n settings: TaskSettingsV2,\n error?: Error,\n ): Promise<boolean> {\n const isManual = settings?.cadence === 'manual';\n const isDuration = settings?.cadence.startsWith('P');\n const isCron = !isManual && !isDuration;\n\n let nextRun: Knex.Raw;\n if (isCron) {\n const time = new CronTime(settings.cadence).sendAt().toUTC();\n this.logger.debug(`task: ${this.taskId} will next occur around ${time}`);\n\n nextRun = this.nextRunAtRaw(time);\n } else if (isManual) {\n nextRun = this.knex.raw('null');\n } else {\n const dt = Duration.fromISO(settings.cadence).as('seconds');\n this.logger.debug(\n `task: ${this.taskId} will next occur around ${DateTime.now().plus({\n seconds: dt,\n })}`,\n );\n\n if (this.knex.client.config.client.includes('sqlite3')) {\n nextRun = this.knex.raw(\n `max(datetime(next_run_start_at, ?), datetime('now'))`,\n [`+${dt} seconds`],\n );\n } else if (this.knex.client.config.client.includes('mysql')) {\n nextRun = this.knex.raw(\n `greatest(next_run_start_at + interval ${dt} second, now())`,\n );\n } else {\n nextRun = this.knex.raw(\n `greatest(next_run_start_at + interval '${dt} seconds', now())`,\n );\n }\n }\n\n const rows = await this.knex<DbTasksRow>(DB_TASKS_TABLE)\n .where('id', '=', this.taskId)\n .where('current_run_ticket', '=', ticket)\n .update({\n next_run_start_at: nextRun,\n current_run_ticket: this.knex.raw('null'),\n current_run_started_at: this.knex.raw('null'),\n current_run_expires_at: this.knex.raw('null'),\n last_run_ended_at: this.knex.fn.now(),\n last_run_error_json: error\n ? serializeError(error)\n : this.knex.raw('null'),\n });\n\n return rows === 1;\n }\n\n private nextRunAtRaw(time: DateTime): Knex.Raw {\n if (this.knex.client.config.client.includes('sqlite3')) {\n return this.knex.raw('datetime(?)', [time.toISO()]);\n } else if (this.knex.client.config.client.includes('mysql')) {\n return this.knex.raw(`?`, [time.toSQL({ includeOffset: false })]);\n }\n return this.knex.raw(`?`, [time.toISO()]);\n }\n}\n"],"names":["Duration","sleep","DB_TASKS_TABLE","NotFoundError","ConflictError","dbTime","uuid","delegateAbortController","taskSettingsV2Schema","nowPlus","CronTime","DateTime","serializeError"],"mappings":";;;;;;;;;;AAqCA,MAAM,+BAA+BA,cAAS,CAAA,UAAA,CAAW,EAAE,OAAA,EAAS,GAAG,CAAA;AAOhE,MAAM,UAAW,CAAA;AAAA,EAKtB,YACmB,MACA,EAAA,EAAA,EACA,IACA,EAAA,MAAA,EACA,qBAA+B,4BAChD,EAAA;AALiB,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AACA,IAAA,IAAA,CAAA,EAAA,GAAA,EAAA;AACA,IAAA,IAAA,CAAA,IAAA,GAAA,IAAA;AACA,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AACA,IAAA,IAAA,CAAA,kBAAA,GAAA,kBAAA;AAAA;AAChB,EAVH,YAAoD,GAAA;AAAA,IAClD,MAAQ,EAAA;AAAA,GACV;AAAA,EAUA,MAAM,KAAM,CAAA,QAAA,EAA0B,OAAkC,EAAA;AACtE,IAAI,IAAA;AACF,MAAM,MAAA,IAAA,CAAK,YAAY,QAAQ,CAAA;AAAA,aACxB,CAAG,EAAA;AACV,MAAA,MAAM,IAAI,KAAA,CAAM,CAA2B,wBAAA,EAAA,CAAC,CAAE,CAAA,CAAA;AAAA;AAGhD,IAAA,IAAA,CAAK,MAAO,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C7C,IAAA,IAAI,KAAK,IAAK,CAAA,MAAA,CAAO,OAAO,MAAO,CAAA,QAAA,CAAS,SAAS,CAAG,EAAA;AACtD,MAAO,OAAA,IAAA,CAAK,KAAK,GAAI,CAAA,aAAA,EAAe,CAAC,IAAK,CAAA,KAAA,EAAO,CAAC,CAAA;AAAA,KACpD,MAAA,IAAW,KAAK,IAAK,CAAA,MAAA,CAAO,OAAO,MAAO,CAAA,QAAA,CAAS,OAAO,CAAG,EAAA;AAC3D,MAAA,OAAO,IAAK,CAAA,IAAA,CAAK,GAAI,CAAA,CAAA,CAAA,CAAA,EAAK,CAAC,IAAA,CAAK,KAAM,CAAA,EAAE,aAAe,EAAA,KAAA,EAAO,CAAC,CAAC,CAAA;AAAA;AAElE,IAAO,OAAA,IAAA,CAAK,KAAK,GAAI,CAAA,CAAA,CAAA,CAAA,EAAK,CAAC,IAAK,CAAA,KAAA,EAAO,CAAC,CAAA;AAAA;AAE5C;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.cjs.js","sources":["../../../../src/entrypoints/scheduler/lib/types.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { CronTime } from 'cron';\nimport { Duration } from 'luxon';\nimport { z } from 'zod';\n\nfunction isValidOptionalDurationString(d: string | undefined): boolean {\n try {\n return !d || Duration.fromISO(d).isValid;\n } catch {\n return false;\n }\n}\n\nfunction isValidCronFormat(c: string | undefined): boolean {\n try {\n if (!c) {\n return false;\n }\n // parse cron format to ensure it's a valid format.\n // eslint-disable-next-line no-new\n new CronTime(c);\n return true;\n } catch {\n return false;\n }\n}\n\nfunction isValidTrigger(t: string): boolean {\n return t === 'manual';\n}\n\nexport const taskSettingsV1Schema = z.object({\n version: z.literal(1),\n initialDelayDuration: z\n .string()\n .optional()\n .refine(isValidOptionalDurationString, {\n message: 'Invalid duration, expecting ISO Period',\n }),\n recurringAtMostEveryDuration: z\n .string()\n .refine(isValidOptionalDurationString, {\n message: 'Invalid duration, expecting ISO Period',\n }),\n timeoutAfterDuration: z.string().refine(isValidOptionalDurationString, {\n message: 'Invalid duration, expecting ISO Period',\n }),\n});\n\n/**\n * The properties that control a scheduled task (version 1).\n */\nexport type TaskSettingsV1 = z.infer<typeof taskSettingsV1Schema>;\n\nexport const taskSettingsV2Schema = z.object({\n version: z.literal(2),\n cadence: z\n .string()\n .refine(isValidCronFormat, { message: 'Invalid cron' })\n .or(\n z.string().refine(isValidTrigger, {\n message: \"Invalid trigger, expecting 'manual'\",\n }),\n )\n .or(\n z.string().refine(isValidOptionalDurationString, {\n message: 'Invalid duration, expecting ISO Period',\n }),\n ),\n timeoutAfterDuration: z.string().refine(isValidOptionalDurationString, {\n message: 'Invalid duration, expecting ISO Period',\n }),\n initialDelayDuration: z\n .string()\n .optional()\n .refine(isValidOptionalDurationString, {\n message: 'Invalid duration, expecting ISO Period',\n }),\n});\n\n/**\n * The properties that control a scheduled task (version 2).\n */\nexport type TaskSettingsV2 = z.infer<typeof taskSettingsV2Schema>;\n"],"names":["Duration","CronTime","z"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"types.cjs.js","sources":["../../../../src/entrypoints/scheduler/lib/types.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonObject } from '@backstage/types';\nimport { CronTime } from 'cron';\nimport { Duration } from 'luxon';\nimport { z } from 'zod';\n\nfunction isValidOptionalDurationString(d: string | undefined): boolean {\n try {\n return !d || Duration.fromISO(d).isValid;\n } catch {\n return false;\n }\n}\n\nfunction isValidCronFormat(c: string | undefined): boolean {\n try {\n if (!c) {\n return false;\n }\n // parse cron format to ensure it's a valid format.\n // eslint-disable-next-line no-new\n new CronTime(c);\n return true;\n } catch {\n return false;\n }\n}\n\nfunction isValidTrigger(t: string): boolean {\n return t === 'manual';\n}\n\nexport const taskSettingsV1Schema = z.object({\n version: z.literal(1),\n initialDelayDuration: z\n .string()\n .optional()\n .refine(isValidOptionalDurationString, {\n message: 'Invalid duration, expecting ISO Period',\n }),\n recurringAtMostEveryDuration: z\n .string()\n .refine(isValidOptionalDurationString, {\n message: 'Invalid duration, expecting ISO Period',\n }),\n timeoutAfterDuration: z.string().refine(isValidOptionalDurationString, {\n message: 'Invalid duration, expecting ISO Period',\n }),\n});\n\n/**\n * The properties that control a scheduled task (version 1).\n */\nexport type TaskSettingsV1 = z.infer<typeof taskSettingsV1Schema>;\n\nexport const taskSettingsV2Schema = z.object({\n version: z.literal(2),\n cadence: z\n .string()\n .refine(isValidCronFormat, { message: 'Invalid cron' })\n .or(\n z.string().refine(isValidTrigger, {\n message: \"Invalid trigger, expecting 'manual'\",\n }),\n )\n .or(\n z.string().refine(isValidOptionalDurationString, {\n message: 'Invalid duration, expecting ISO Period',\n }),\n ),\n timeoutAfterDuration: z.string().refine(isValidOptionalDurationString, {\n message: 'Invalid duration, expecting ISO Period',\n }),\n initialDelayDuration: z\n .string()\n .optional()\n .refine(isValidOptionalDurationString, {\n message: 'Invalid duration, expecting ISO Period',\n }),\n});\n\n/**\n * The properties that control a scheduled task (version 2).\n */\nexport type TaskSettingsV2 = z.infer<typeof taskSettingsV2Schema>;\n\n/**\n * The shape of a task definition as returned by the service's REST API.\n */\nexport interface TaskApiTasksResponse {\n taskId: string;\n pluginId: string;\n scope: 'global' | 'local';\n settings: { version: number } & JsonObject;\n taskState:\n | {\n status: 'running';\n startedAt: string;\n timesOutAt?: string;\n lastRunError?: string;\n lastRunEndedAt?: string;\n }\n | {\n status: 'idle';\n startsAt?: string;\n lastRunError?: string;\n lastRunEndedAt?: string;\n }\n | null;\n workerState:\n | {\n status: 'initial-wait';\n }\n | {\n status: 'idle';\n }\n | {\n status: 'running';\n }\n | null;\n}\n"],"names":["Duration","CronTime","z"],"mappings":";;;;;;AAqBA,SAAS,8BAA8B,CAAgC,EAAA;AACrE,EAAI,IAAA;AACF,IAAA,OAAO,CAAC,CAAA,IAAKA,cAAS,CAAA,OAAA,CAAQ,CAAC,CAAE,CAAA,OAAA;AAAA,GAC3B,CAAA,MAAA;AACN,IAAO,OAAA,KAAA;AAAA;AAEX;AAEA,SAAS,kBAAkB,CAAgC,EAAA;AACzD,EAAI,IAAA;AACF,IAAA,IAAI,CAAC,CAAG,EAAA;AACN,MAAO,OAAA,KAAA;AAAA;AAIT,IAAA,IAAIC,cAAS,CAAC,CAAA;AACd,IAAO,OAAA,IAAA;AAAA,GACD,CAAA,MAAA;AACN,IAAO,OAAA,KAAA;AAAA;AAEX;AAEA,SAAS,eAAe,CAAoB,EAAA;AAC1C,EAAA,OAAO,CAAM,KAAA,QAAA;AACf;AAEoCC,MAAE,MAAO,CAAA;AAAA,EAC3C,OAAA,EAASA,KAAE,CAAA,OAAA,CAAQ,CAAC,CAAA;AAAA,EACpB,sBAAsBA,KACnB,CAAA,MAAA,GACA,QAAS,EAAA,CACT,OAAO,6BAA+B,EAAA;AAAA,IACrC,OAAS,EAAA;AAAA,GACV,CAAA;AAAA,EACH,4BAA8B,EAAAA,KAAA,CAC3B,MAAO,EAAA,CACP,OAAO,6BAA+B,EAAA;AAAA,IACrC,OAAS,EAAA;AAAA,GACV,CAAA;AAAA,EACH,oBAAsB,EAAAA,KAAA,CAAE,MAAO,EAAA,CAAE,OAAO,6BAA+B,EAAA;AAAA,IACrE,OAAS,EAAA;AAAA,GACV;AACH,CAAC;AAOY,MAAA,oBAAA,GAAuBA,MAAE,MAAO,CAAA;AAAA,EAC3C,OAAA,EAASA,KAAE,CAAA,OAAA,CAAQ,CAAC,CAAA;AAAA,EACpB,OAAA,EAASA,KACN,CAAA,MAAA,EACA,CAAA,MAAA,CAAO,mBAAmB,EAAE,OAAA,EAAS,cAAe,EAAC,CACrD,CAAA,EAAA;AAAA,IACCA,KAAE,CAAA,MAAA,EAAS,CAAA,MAAA,CAAO,cAAgB,EAAA;AAAA,MAChC,OAAS,EAAA;AAAA,KACV;AAAA,GAEF,CAAA,EAAA;AAAA,IACCA,KAAE,CAAA,MAAA,EAAS,CAAA,MAAA,CAAO,6BAA+B,EAAA;AAAA,MAC/C,OAAS,EAAA;AAAA,KACV;AAAA,GACH;AAAA,EACF,oBAAsB,EAAAA,KAAA,CAAE,MAAO,EAAA,CAAE,OAAO,6BAA+B,EAAA;AAAA,IACrE,OAAS,EAAA;AAAA,GACV,CAAA;AAAA,EACD,sBAAsBA,KACnB,CAAA,MAAA,GACA,QAAS,EAAA,CACT,OAAO,6BAA+B,EAAA;AAAA,IACrC,OAAS,EAAA;AAAA,GACV;AACL,CAAC;;;;"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var errors = require('@backstage/errors');
|
|
4
|
-
require('luxon');
|
|
4
|
+
var luxon = require('luxon');
|
|
5
5
|
|
|
6
6
|
const TRACER_ID = "backstage-service-scheduler";
|
|
7
7
|
function validateId(id) {
|
|
@@ -9,6 +9,12 @@ function validateId(id) {
|
|
|
9
9
|
throw new errors.InputError(`${id} is not a valid ID, expected non-empty string`);
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
|
+
function dbTime(t) {
|
|
13
|
+
if (typeof t === "string") {
|
|
14
|
+
return luxon.DateTime.fromSQL(t);
|
|
15
|
+
}
|
|
16
|
+
return luxon.DateTime.fromJSDate(t);
|
|
17
|
+
}
|
|
12
18
|
function nowPlus(duration, knex) {
|
|
13
19
|
const seconds = duration?.as("seconds") ?? 0;
|
|
14
20
|
if (!seconds) {
|
|
@@ -57,10 +63,19 @@ function delegateAbortController(parent) {
|
|
|
57
63
|
}
|
|
58
64
|
return delegate;
|
|
59
65
|
}
|
|
66
|
+
function serializeError(error) {
|
|
67
|
+
return JSON.stringify(
|
|
68
|
+
errors.serializeError(error, {
|
|
69
|
+
includeStack: process.env.NODE_ENV === "development"
|
|
70
|
+
})
|
|
71
|
+
);
|
|
72
|
+
}
|
|
60
73
|
|
|
61
74
|
exports.TRACER_ID = TRACER_ID;
|
|
75
|
+
exports.dbTime = dbTime;
|
|
62
76
|
exports.delegateAbortController = delegateAbortController;
|
|
63
77
|
exports.nowPlus = nowPlus;
|
|
78
|
+
exports.serializeError = serializeError;
|
|
64
79
|
exports.sleep = sleep;
|
|
65
80
|
exports.validateId = validateId;
|
|
66
81
|
//# sourceMappingURL=util.cjs.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.cjs.js","sources":["../../../../src/entrypoints/scheduler/lib/util.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {
|
|
1
|
+
{"version":3,"file":"util.cjs.js","sources":["../../../../src/entrypoints/scheduler/lib/util.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n InputError,\n serializeError as internalSerializeError,\n} from '@backstage/errors';\nimport { Knex } from 'knex';\nimport { DateTime, Duration } from 'luxon';\n\nexport const TRACER_ID = 'backstage-service-scheduler';\n\n// Keep the IDs compatible with e.g. Prometheus labels\nexport function validateId(id: string) {\n if (typeof id !== 'string' || !id.trim()) {\n throw new InputError(`${id} is not a valid ID, expected non-empty string`);\n }\n}\n\nexport function dbTime(t: Date | string): DateTime {\n if (typeof t === 'string') {\n return DateTime.fromSQL(t);\n }\n return DateTime.fromJSDate(t);\n}\n\nexport function nowPlus(duration: Duration | undefined, knex: Knex) {\n const seconds = duration?.as('seconds') ?? 0;\n if (!seconds) {\n return knex.fn.now();\n }\n\n if (knex.client.config.client.includes('sqlite3')) {\n return knex.raw(`datetime('now', ?)`, [`${seconds} seconds`]);\n }\n\n if (knex.client.config.client.includes('mysql')) {\n return knex.raw(`now() + interval ${seconds} second`);\n }\n\n return knex.raw(`now() + interval '${seconds} seconds'`);\n}\n\n/**\n * Sleep for the given duration, but return sooner if the abort signal\n * triggers.\n *\n * @param duration - The amount of time to sleep, at most\n * @param abortSignal - An optional abort signal that short circuits the wait\n */\nexport async function sleep(\n duration: Duration,\n abortSignal?: AbortSignal,\n): Promise<void> {\n if (abortSignal?.aborted) {\n return;\n }\n\n await new Promise<void>(resolve => {\n let timeoutHandle: NodeJS.Timeout | undefined = undefined;\n\n const done = () => {\n if (timeoutHandle) {\n clearTimeout(timeoutHandle);\n }\n abortSignal?.removeEventListener('abort', done);\n resolve();\n };\n\n timeoutHandle = setTimeout(done, duration.as('milliseconds'));\n abortSignal?.addEventListener('abort', done);\n });\n}\n\n/**\n * Creates a new AbortController that, in addition to working as a regular\n * standalone controller, also gets aborted if the given parent signal\n * reaches aborted state.\n *\n * @param parent - The \"parent\" signal that can trigger the delegate\n */\nexport function delegateAbortController(parent?: AbortSignal): AbortController {\n const delegate = new AbortController();\n\n if (parent) {\n if (parent.aborted) {\n delegate.abort();\n } else {\n const onParentAborted = () => {\n delegate.abort();\n };\n\n const onChildAborted = () => {\n parent.removeEventListener('abort', onParentAborted);\n };\n\n parent.addEventListener('abort', onParentAborted, { once: true });\n delegate.signal.addEventListener('abort', onChildAborted, { once: true });\n }\n }\n\n return delegate;\n}\n\nexport function serializeError(error: Error): string {\n return JSON.stringify(\n internalSerializeError(error, {\n includeStack: process.env.NODE_ENV === 'development',\n }),\n );\n}\n"],"names":["InputError","DateTime","internalSerializeError"],"mappings":";;;;;AAuBO,MAAM,SAAY,GAAA;AAGlB,SAAS,WAAW,EAAY,EAAA;AACrC,EAAA,IAAI,OAAO,EAAO,KAAA,QAAA,IAAY,CAAC,EAAA,CAAG,MAAQ,EAAA;AACxC,IAAA,MAAM,IAAIA,iBAAA,CAAW,CAAG,EAAA,EAAE,CAA+C,6CAAA,CAAA,CAAA;AAAA;AAE7E;AAEO,SAAS,OAAO,CAA4B,EAAA;AACjD,EAAI,IAAA,OAAO,MAAM,QAAU,EAAA;AACzB,IAAO,OAAAC,cAAA,CAAS,QAAQ,CAAC,CAAA;AAAA;AAE3B,EAAO,OAAAA,cAAA,CAAS,WAAW,CAAC,CAAA;AAC9B;AAEgB,SAAA,OAAA,CAAQ,UAAgC,IAAY,EAAA;AAClE,EAAA,MAAM,OAAU,GAAA,QAAA,EAAU,EAAG,CAAA,SAAS,CAAK,IAAA,CAAA;AAC3C,EAAA,IAAI,CAAC,OAAS,EAAA;AACZ,IAAO,OAAA,IAAA,CAAK,GAAG,GAAI,EAAA;AAAA;AAGrB,EAAA,IAAI,KAAK,MAAO,CAAA,MAAA,CAAO,MAAO,CAAA,QAAA,CAAS,SAAS,CAAG,EAAA;AACjD,IAAA,OAA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AAA,MAAM,iBAAiB,MAAM;AAC3B,QAAO,MAAA,CAAA,mBAAA,CAAoB,SAAS,eAAe,CAAA;AAAA,OACrD;AAEA,MAAA,MAAA,CAAO,iBAAiB,OAAS,EAAA,eAAA,EAAiB,EAAE,IAAA,EAAM,MAAM,CAAA;AAChE,MAAA,QAAA,CAAS,OAAO,gBAAiB,CAAA,OAAA,EAAS,gBAAgB,EAAE,IAAA,EAAM,MAAM,CAAA;AAAA;AAC1E;AAGF,EAAO,OAAA,QAAA;AACT;AAEO,SAAS,eAAe,KAAsB,EAAA;AACnD,EAAA,OAAO,IAAK,CAAA,SAAA;AAAA,IACVC,sBAAuB,KAAO,EAAA;AAAA,MAC5B,YAAA,EAAc,OAAQ,CAAA,GAAA,CAAI,QAAa,KAAA;AAAA,KACxC;AAAA,GACH;AACF;;;;;;;;;;"}
|
|
@@ -8,10 +8,24 @@ const schedulerServiceFactory = backendPluginApi.createServiceFactory({
|
|
|
8
8
|
deps: {
|
|
9
9
|
database: backendPluginApi.coreServices.database,
|
|
10
10
|
logger: backendPluginApi.coreServices.logger,
|
|
11
|
-
rootLifecycle: backendPluginApi.coreServices.rootLifecycle
|
|
11
|
+
rootLifecycle: backendPluginApi.coreServices.rootLifecycle,
|
|
12
|
+
httpRouter: backendPluginApi.coreServices.httpRouter,
|
|
13
|
+
pluginMetadata: backendPluginApi.coreServices.pluginMetadata
|
|
12
14
|
},
|
|
13
|
-
async factory({
|
|
14
|
-
|
|
15
|
+
async factory({
|
|
16
|
+
database,
|
|
17
|
+
logger,
|
|
18
|
+
rootLifecycle,
|
|
19
|
+
httpRouter,
|
|
20
|
+
pluginMetadata
|
|
21
|
+
}) {
|
|
22
|
+
return DefaultSchedulerService.DefaultSchedulerService.create({
|
|
23
|
+
database,
|
|
24
|
+
logger,
|
|
25
|
+
rootLifecycle,
|
|
26
|
+
httpRouter,
|
|
27
|
+
pluginMetadata
|
|
28
|
+
});
|
|
15
29
|
}
|
|
16
30
|
});
|
|
17
31
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schedulerServiceFactory.cjs.js","sources":["../../../src/entrypoints/scheduler/schedulerServiceFactory.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n coreServices,\n createServiceFactory,\n} from '@backstage/backend-plugin-api';\nimport { DefaultSchedulerService } from './lib/DefaultSchedulerService';\n\n/**\n * Scheduling of distributed background tasks.\n *\n * See {@link @backstage/code-plugin-api#SchedulerService}\n * and {@link https://backstage.io/docs/backend-system/core-services/scheduler | the service docs}\n * for more information.\n *\n * @public\n */\nexport const schedulerServiceFactory = createServiceFactory({\n service: coreServices.scheduler,\n deps: {\n database: coreServices.database,\n logger: coreServices.logger,\n rootLifecycle: coreServices.rootLifecycle,\n },\n async factory({
|
|
1
|
+
{"version":3,"file":"schedulerServiceFactory.cjs.js","sources":["../../../src/entrypoints/scheduler/schedulerServiceFactory.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n coreServices,\n createServiceFactory,\n} from '@backstage/backend-plugin-api';\nimport { DefaultSchedulerService } from './lib/DefaultSchedulerService';\n\n/**\n * Scheduling of distributed background tasks.\n *\n * See {@link @backstage/code-plugin-api#SchedulerService}\n * and {@link https://backstage.io/docs/backend-system/core-services/scheduler | the service docs}\n * for more information.\n *\n * @public\n */\nexport const schedulerServiceFactory = createServiceFactory({\n service: coreServices.scheduler,\n deps: {\n database: coreServices.database,\n logger: coreServices.logger,\n rootLifecycle: coreServices.rootLifecycle,\n httpRouter: coreServices.httpRouter,\n pluginMetadata: coreServices.pluginMetadata,\n },\n async factory({\n database,\n logger,\n rootLifecycle,\n httpRouter,\n pluginMetadata,\n }) {\n return DefaultSchedulerService.create({\n database,\n logger,\n rootLifecycle,\n httpRouter,\n pluginMetadata,\n });\n },\n});\n"],"names":["createServiceFactory","coreServices","DefaultSchedulerService"],"mappings":";;;;;AA+BO,MAAM,0BAA0BA,qCAAqB,CAAA;AAAA,EAC1D,SAASC,6BAAa,CAAA,SAAA;AAAA,EACtB,IAAM,EAAA;AAAA,IACJ,UAAUA,6BAAa,CAAA,QAAA;AAAA,IACvB,QAAQA,6BAAa,CAAA,MAAA;AAAA,IACrB,eAAeA,6BAAa,CAAA,aAAA;AAAA,IAC5B,YAAYA,6BAAa,CAAA,UAAA;AAAA,IACzB,gBAAgBA,6BAAa,CAAA;AAAA,GAC/B;AAAA,EACA,MAAM,OAAQ,CAAA;AAAA,IACZ,QAAA;AAAA,IACA,MAAA;AAAA,IACA,aAAA;AAAA,IACA,UAAA;AAAA,IACA;AAAA,GACC,EAAA;AACD,IAAA,OAAOC,gDAAwB,MAAO,CAAA;AAAA,MACpC,QAAA;AAAA,MACA,MAAA;AAAA,MACA,aAAA;AAAA,MACA,UAAA;AAAA,MACA;AAAA,KACD,CAAA;AAAA;AAEL,CAAC;;;;"}
|