From 0cf317ee370471c3167596e85d1d251980533731 Mon Sep 17 00:00:00 2001 From: Leon Date: Thu, 6 Oct 2022 11:47:12 +0200 Subject: [PATCH 1/8] Add a delay to the fact retrievers to prevent errors Signed-off-by: Leon --- .../src/service/fact/FactRetrieverEngine.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugins/tech-insights-backend/src/service/fact/FactRetrieverEngine.ts b/plugins/tech-insights-backend/src/service/fact/FactRetrieverEngine.ts index 2d738b7de8..a316b984ed 100644 --- a/plugins/tech-insights-backend/src/service/fact/FactRetrieverEngine.ts +++ b/plugins/tech-insights-backend/src/service/fact/FactRetrieverEngine.ts @@ -126,6 +126,9 @@ export class DefaultFactRetrieverEngine implements FactRetrieverEngine { frequency: { cron: cronExpression }, fn: this.createFactRetrieverHandler(factRetriever, lifecycle), timeout: timeLimit, + // We add a delay in order to prevent errors due to the + // fact that the backend is not yet online in a cold-start scenario + initialDelay: Duration.fromObject({ seconds: 5 }), }); newRegs.push(factRetriever.id); } catch (e) { From 06cf8f1cf22590c370e179d4c6f142f3a2b31809 Mon Sep 17 00:00:00 2001 From: Leon Date: Thu, 6 Oct 2022 12:07:24 +0200 Subject: [PATCH 2/8] Changeset Signed-off-by: Leon --- .changeset/tame-ads-appear.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/tame-ads-appear.md diff --git a/.changeset/tame-ads-appear.md b/.changeset/tame-ads-appear.md new file mode 100644 index 0000000000..00eb4a7dcd --- /dev/null +++ b/.changeset/tame-ads-appear.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-tech-insights-backend': minor +--- + +Add a default delay to the fact retrievers to prevent cold-start errors From 7f93a25ac7e0825e29d17a3f8c4b960287f2a021 Mon Sep 17 00:00:00 2001 From: Leon van Ginneken Date: Tue, 18 Oct 2022 13:46:40 +0200 Subject: [PATCH 3/8] Update .changeset/tame-ads-appear.md Co-authored-by: Johan Haals Signed-off-by: Leon van Ginneken --- .changeset/tame-ads-appear.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/tame-ads-appear.md b/.changeset/tame-ads-appear.md index 00eb4a7dcd..3f7a564c71 100644 --- a/.changeset/tame-ads-appear.md +++ b/.changeset/tame-ads-appear.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-tech-insights-backend': minor +'@backstage/plugin-tech-insights-backend': patch --- Add a default delay to the fact retrievers to prevent cold-start errors From 8106afc7fdbd9e4f64b611a6b442a8ce2b206b6d Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 21 Oct 2022 15:09:34 +0200 Subject: [PATCH 4/8] feat: add initialDelay as option Signed-off-by: Leon --- .../src/service/fact/FactRetrieverEngine.ts | 10 ++++++++-- plugins/tech-insights-node/src/facts.ts | 7 +++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/plugins/tech-insights-backend/src/service/fact/FactRetrieverEngine.ts b/plugins/tech-insights-backend/src/service/fact/FactRetrieverEngine.ts index a316b984ed..9078a72fba 100644 --- a/plugins/tech-insights-backend/src/service/fact/FactRetrieverEngine.ts +++ b/plugins/tech-insights-backend/src/service/fact/FactRetrieverEngine.ts @@ -76,6 +76,7 @@ export class DefaultFactRetrieverEngine implements FactRetrieverEngine { private readonly scheduler: PluginTaskScheduler, private readonly defaultCadence?: string, private readonly defaultTimeout?: Duration, + private readonly defaultInitialDelay?: Duration, ) {} static async create(options: { @@ -85,6 +86,7 @@ export class DefaultFactRetrieverEngine implements FactRetrieverEngine { scheduler: PluginTaskScheduler; defaultCadence?: string; defaultTimeout?: Duration; + defaultInitialDelay?: Duration; }) { const { repository, @@ -93,6 +95,7 @@ export class DefaultFactRetrieverEngine implements FactRetrieverEngine { scheduler, defaultCadence, defaultTimeout, + defaultInitialDelay, } = options; const retrievers = await factRetrieverRegistry.listRetrievers(); @@ -106,6 +109,7 @@ export class DefaultFactRetrieverEngine implements FactRetrieverEngine { scheduler, defaultCadence, defaultTimeout, + defaultInitialDelay, ); } @@ -115,11 +119,13 @@ export class DefaultFactRetrieverEngine implements FactRetrieverEngine { await Promise.all( registrations.map(async registration => { - const { factRetriever, cadence, lifecycle, timeout } = registration; + const { factRetriever, cadence, lifecycle, timeout, initialDelay } = registration; const cronExpression = cadence || this.defaultCadence || randomDailyCron(); const timeLimit = timeout || this.defaultTimeout || Duration.fromObject({ minutes: 5 }); + const initialDelaySetting = + initialDelay || this.defaultInitialDelay || Duration.fromObject({ seconds: 5 }); try { await this.scheduler.scheduleTask({ id: factRetriever.id, @@ -128,7 +134,7 @@ export class DefaultFactRetrieverEngine implements FactRetrieverEngine { timeout: timeLimit, // We add a delay in order to prevent errors due to the // fact that the backend is not yet online in a cold-start scenario - initialDelay: Duration.fromObject({ seconds: 5 }), + initialDelay: initialDelaySetting, }); newRegs.push(factRetriever.id); } catch (e) { diff --git a/plugins/tech-insights-node/src/facts.ts b/plugins/tech-insights-node/src/facts.ts index 41db302fd5..1eb58b08ce 100644 --- a/plugins/tech-insights-node/src/facts.ts +++ b/plugins/tech-insights-node/src/facts.ts @@ -279,4 +279,11 @@ export type FactRetrieverRegistration = { * If defined this value will be used to determine expired items which will deleted when this fact retriever is run */ lifecycle?: FactLifecycle; + + /** + * A duration to determine the initial delay for the fact retriever. Useful for cold start scenarios when e.g. the + * catalog backend is not yet available. Defaults to 5 seconds. + * + */ + initialDelay?: Duration | HumanDuration; }; From 4fc30a3eb9aa4ef826d4d8084e2107106143edd1 Mon Sep 17 00:00:00 2001 From: Leon van Ginneken Date: Fri, 21 Oct 2022 15:13:19 +0200 Subject: [PATCH 5/8] Update tame-ads-appear.md Signed-off-by: Leon --- .changeset/tame-ads-appear.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changeset/tame-ads-appear.md b/.changeset/tame-ads-appear.md index 3f7a564c71..887cc73252 100644 --- a/.changeset/tame-ads-appear.md +++ b/.changeset/tame-ads-appear.md @@ -1,5 +1,6 @@ --- '@backstage/plugin-tech-insights-backend': patch +'@backstage/plugin-tech-insights-node': patch --- Add a default delay to the fact retrievers to prevent cold-start errors From 4aeb9638cdaab15127b004fe67d7e1937b7413fd Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 21 Oct 2022 15:20:17 +0200 Subject: [PATCH 6/8] feat: add option to helper fn Signed-off-by: Leon --- .../src/service/fact/createFactRetriever.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/tech-insights-backend/src/service/fact/createFactRetriever.ts b/plugins/tech-insights-backend/src/service/fact/createFactRetriever.ts index aaba29ecb1..6d00219e45 100644 --- a/plugins/tech-insights-backend/src/service/fact/createFactRetriever.ts +++ b/plugins/tech-insights-backend/src/service/fact/createFactRetriever.ts @@ -35,6 +35,7 @@ export type FactRetrieverRegistrationOptions = { factRetriever: FactRetriever; lifecycle?: FactLifecycle; timeout?: Duration | HumanDuration; + initialDelay?: Duration | HumanDuration; }; /** @@ -45,6 +46,7 @@ export type FactRetrieverRegistrationOptions = { * @param factRetriever - Implementation of fact retriever consisting of at least id, version, schema and handler * @param lifecycle - Optional lifecycle definition indicating the cleanup logic of facts when this retriever is run * @param timeout - Optional duration to determine how long the fact retriever should be allowed to run, defaults to 5 minutes + * @param initialDelay - Optional initial delay to determine how long the fact retriever should wait before the initial run, defaults to 5 seconds * * * @remarks @@ -68,11 +70,12 @@ export type FactRetrieverRegistrationOptions = { export function createFactRetrieverRegistration( options: FactRetrieverRegistrationOptions, ): FactRetrieverRegistration { - const { cadence, factRetriever, lifecycle, timeout } = options; + const { cadence, factRetriever, lifecycle, timeout, initialDelay } = options; return { cadence, factRetriever, lifecycle, timeout, + initialDelay, }; } From 6f6ffa5fe0bac5d522a5e3f8f4264d29e87413dc Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 21 Oct 2022 15:33:08 +0200 Subject: [PATCH 7/8] chore: api-report Signed-off-by: Leon --- plugins/tech-insights-backend/api-report.md | 1 + plugins/tech-insights-node/api-report.md | 1 + 2 files changed, 2 insertions(+) diff --git a/plugins/tech-insights-backend/api-report.md b/plugins/tech-insights-backend/api-report.md index 94ac35ad9f..8951a81abf 100644 --- a/plugins/tech-insights-backend/api-report.md +++ b/plugins/tech-insights-backend/api-report.md @@ -60,6 +60,7 @@ export type FactRetrieverRegistrationOptions = { factRetriever: FactRetriever; lifecycle?: FactLifecycle; timeout?: Duration | HumanDuration; + initialDelay?: Duration | HumanDuration; }; // @public (undocumented) diff --git a/plugins/tech-insights-node/api-report.md b/plugins/tech-insights-node/api-report.md index a57cf3ec4a..4c3df0cc80 100644 --- a/plugins/tech-insights-node/api-report.md +++ b/plugins/tech-insights-node/api-report.md @@ -75,6 +75,7 @@ export type FactRetrieverRegistration = { cadence?: string; timeout?: Duration | HumanDuration; lifecycle?: FactLifecycle; + initialDelay?: Duration | HumanDuration; }; // @public From 5c3ff9c09c5010eee45a28ae5a0d273fd058996a Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 21 Oct 2022 15:37:38 +0200 Subject: [PATCH 8/8] chore: prettier Signed-off-by: Leon --- .../src/service/fact/FactRetrieverEngine.ts | 7 +++++-- plugins/tech-insights-node/src/facts.ts | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/plugins/tech-insights-backend/src/service/fact/FactRetrieverEngine.ts b/plugins/tech-insights-backend/src/service/fact/FactRetrieverEngine.ts index 9078a72fba..e223604829 100644 --- a/plugins/tech-insights-backend/src/service/fact/FactRetrieverEngine.ts +++ b/plugins/tech-insights-backend/src/service/fact/FactRetrieverEngine.ts @@ -119,13 +119,16 @@ export class DefaultFactRetrieverEngine implements FactRetrieverEngine { await Promise.all( registrations.map(async registration => { - const { factRetriever, cadence, lifecycle, timeout, initialDelay } = registration; + const { factRetriever, cadence, lifecycle, timeout, initialDelay } = + registration; const cronExpression = cadence || this.defaultCadence || randomDailyCron(); const timeLimit = timeout || this.defaultTimeout || Duration.fromObject({ minutes: 5 }); const initialDelaySetting = - initialDelay || this.defaultInitialDelay || Duration.fromObject({ seconds: 5 }); + initialDelay || + this.defaultInitialDelay || + Duration.fromObject({ seconds: 5 }); try { await this.scheduler.scheduleTask({ id: factRetriever.id, diff --git a/plugins/tech-insights-node/src/facts.ts b/plugins/tech-insights-node/src/facts.ts index 1eb58b08ce..0916189bc7 100644 --- a/plugins/tech-insights-node/src/facts.ts +++ b/plugins/tech-insights-node/src/facts.ts @@ -285,5 +285,5 @@ export type FactRetrieverRegistration = { * catalog backend is not yet available. Defaults to 5 seconds. * */ - initialDelay?: Duration | HumanDuration; + initialDelay?: Duration | HumanDuration; };