From 60df8a58b4ae6a515b71f7dbadf39cd225ee7ff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Tue, 8 Mar 2022 16:43:44 +0100 Subject: [PATCH] change to nested object for cron MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .changeset/perfect-moose-appear.md | 2 +- .github/styles/vocab.txt | 1 + packages/backend-tasks/README.md | 2 +- packages/backend-tasks/api-report.md | 6 +- packages/backend-tasks/package.json | 5 -- .../src/tasks/PluginTaskSchedulerImpl.test.ts | 2 +- .../src/tasks/PluginTaskSchedulerImpl.ts | 4 +- .../src/tasks/TaskScheduler.test.ts | 2 +- packages/backend-tasks/src/tasks/types.ts | 59 ++++++++++--------- .../EntityAirbrakeWidget.test.tsx | 2 +- 10 files changed, 44 insertions(+), 41 deletions(-) diff --git a/.changeset/perfect-moose-appear.md b/.changeset/perfect-moose-appear.md index 5666fd3630..9c5c356e4a 100644 --- a/.changeset/perfect-moose-appear.md +++ b/.changeset/perfect-moose-appear.md @@ -2,4 +2,4 @@ '@backstage/backend-tasks': patch --- -Add support for cron syntax to configure task frequency - `TaskScheduleDefinition.frequency` can now be both a `Duration` and a string, where the latter is expected to be on standard cron format (e.g. `'0 */2 * * *'`). +Add support for cron syntax to configure task frequency - `TaskScheduleDefinition.frequency` can now be both a `Duration` and an object on the form `{ cron: string }`, where the latter is expected to be on standard crontab format (e.g. `'0 */2 * * *'`). diff --git a/.github/styles/vocab.txt b/.github/styles/vocab.txt index 2614dd6b50..ac98d176b8 100644 --- a/.github/styles/vocab.txt +++ b/.github/styles/vocab.txt @@ -61,6 +61,7 @@ const cookiecutter cron cronjobs +crontab css Datadog dataflow diff --git a/packages/backend-tasks/README.md b/packages/backend-tasks/README.md index b0bb292313..1bfab8a89b 100644 --- a/packages/backend-tasks/README.md +++ b/packages/backend-tasks/README.md @@ -22,7 +22,7 @@ const scheduler = TaskScheduler.fromConfig(rootConfig).forPlugin('my-plugin'); await scheduler.scheduleTask({ id: 'refresh_things', - cadence: '*/5 * * * *', // every 5 minutes + frequency: { cron: '*/5 * * * *' }, // every 5 minutes, also supports Duration timeout: Duration.fromObject({ minutes: 15 }), fn: async () => { await entityProvider.run(); diff --git a/packages/backend-tasks/api-report.md b/packages/backend-tasks/api-report.md index 8a299f924b..0093616d8f 100644 --- a/packages/backend-tasks/api-report.md +++ b/packages/backend-tasks/api-report.md @@ -36,7 +36,11 @@ export interface TaskRunner { // @public export interface TaskScheduleDefinition { - frequency: string | Duration; + frequency: + | { + cron: string; + } + | Duration; initialDelay?: Duration; timeout: Duration; } diff --git a/packages/backend-tasks/package.json b/packages/backend-tasks/package.json index 4821d39348..39aa3067a1 100644 --- a/packages/backend-tasks/package.json +++ b/packages/backend-tasks/package.json @@ -48,14 +48,9 @@ "zod": "^3.9.5" }, "devDependencies": { -<<<<<<< HEAD "@backstage/backend-test-utils": "^0.1.21-next.0", "@backstage/cli": "^0.15.2-next.0", -======= - "@backstage/backend-test-utils": "^0.1.20", - "@backstage/cli": "^0.15.0", "@types/cron": "^1.7.3", ->>>>>>> ab18600147 (Add cron support to `@backstage/backend-tasks`) "jest": "^26.0.1", "wait-for-expect": "^3.0.2" }, diff --git a/packages/backend-tasks/src/tasks/PluginTaskSchedulerImpl.test.ts b/packages/backend-tasks/src/tasks/PluginTaskSchedulerImpl.test.ts index 700bfda872..646a2ce737 100644 --- a/packages/backend-tasks/src/tasks/PluginTaskSchedulerImpl.test.ts +++ b/packages/backend-tasks/src/tasks/PluginTaskSchedulerImpl.test.ts @@ -68,7 +68,7 @@ describe('PluginTaskManagerImpl', () => { await manager.scheduleTask({ id: 'task2', timeout: Duration.fromMillis(5000), - frequency: '* * * * * *', + frequency: { cron: '* * * * * *' }, fn, }); diff --git a/packages/backend-tasks/src/tasks/PluginTaskSchedulerImpl.ts b/packages/backend-tasks/src/tasks/PluginTaskSchedulerImpl.ts index 2008e3beec..8c3166b441 100644 --- a/packages/backend-tasks/src/tasks/PluginTaskSchedulerImpl.ts +++ b/packages/backend-tasks/src/tasks/PluginTaskSchedulerImpl.ts @@ -46,8 +46,8 @@ export class PluginTaskSchedulerImpl implements PluginTaskScheduler { { version: 2, cadence: - typeof task.frequency === 'string' - ? task.frequency + 'cron' in task.frequency + ? task.frequency.cron : task.frequency.toISO(), initialDelayDuration: task.initialDelay?.toISO(), timeoutAfterDuration: task.timeout.toISO(), diff --git a/packages/backend-tasks/src/tasks/TaskScheduler.test.ts b/packages/backend-tasks/src/tasks/TaskScheduler.test.ts index c5c349f593..2a5deddb19 100644 --- a/packages/backend-tasks/src/tasks/TaskScheduler.test.ts +++ b/packages/backend-tasks/src/tasks/TaskScheduler.test.ts @@ -69,7 +69,7 @@ describe('TaskScheduler', () => { await manager.scheduleTask({ id: 'task2', timeout: Duration.fromMillis(5000), - frequency: '* * * * * *', + frequency: { cron: '* * * * * *' }, fn, }); diff --git a/packages/backend-tasks/src/tasks/types.ts b/packages/backend-tasks/src/tasks/types.ts index 9855da3744..acd651adcf 100644 --- a/packages/backend-tasks/src/tasks/types.ts +++ b/packages/backend-tasks/src/tasks/types.ts @@ -38,18 +38,8 @@ export type TaskFunction = */ export interface TaskScheduleDefinition { /** - * The maximum amount of time that a single task invocation can take, before - * it's considered timed out and gets "released" such that a new invocation - * is permitted to take place (possibly, then, on a different worker). - * - * This is a required field. - */ - timeout: Duration; - - /** - * The amount of time that should pass between task invocation starts. - * Essentially, this equals roughly how often you want the task to run. - * The system does its best to avoid overlapping invocations. + * How often you want the task to run. The system does its best to avoid + * overlapping invocations. * * This is a best effort value; under some circumstances there can be * deviations. For example, if the task runtime is longer than the frequency @@ -57,24 +47,37 @@ export interface TaskScheduleDefinition { * invocation of this task will be delayed until after the previous one * finishes. * - * This value can be a crontab style string (see below), or an ISO period - * string (e.g. 'PT1M'). - * * This is a required field. - * - * Cron expressions help: - * - * ┌────────────── second (optional) - * │ ┌──────────── minute - * │ │ ┌────────── hour - * │ │ │ ┌──────── day of month - * │ │ │ │ ┌────── month - * │ │ │ │ │ ┌──── day of week - * │ │ │ │ │ │ - * │ │ │ │ │ │ - * * * * * * * */ - frequency: string | Duration; + frequency: + | { + /** + * A crontab style string. + * + * Overview: + * + * ``` + * ┌────────────── second (optional) + * │ ┌──────────── minute + * │ │ ┌────────── hour + * │ │ │ ┌──────── day of month + * │ │ │ │ ┌────── month + * │ │ │ │ │ ┌──── day of week + * │ │ │ │ │ │ + * │ │ │ │ │ │ + * * * * * * * + * ``` + */ + cron: string; + } + | Duration; + + /** + * The maximum amount of time that a single task invocation can take, before + * it's considered timed out and gets "released" such that a new invocation + * is permitted to take place (possibly, then, on a different worker). + */ + timeout: Duration; /** * The amount of time that should pass before the first invocation happens. diff --git a/plugins/airbrake/src/components/EntityAirbrakeWidget/EntityAirbrakeWidget.test.tsx b/plugins/airbrake/src/components/EntityAirbrakeWidget/EntityAirbrakeWidget.test.tsx index e8d09b5e65..a7243f5f0f 100644 --- a/plugins/airbrake/src/components/EntityAirbrakeWidget/EntityAirbrakeWidget.test.tsx +++ b/plugins/airbrake/src/components/EntityAirbrakeWidget/EntityAirbrakeWidget.test.tsx @@ -45,7 +45,7 @@ describe('EntityAirbrakeWidget', () => { expect(exampleData.groups.length).toBeGreaterThan(0); for (const group of exampleData.groups) { expect( - await widget.getByText(group.errors[0].message), + await widget.findByText(group.errors[0].message), ).toBeInTheDocument(); } });