diff --git a/.changeset/ninety-otters-report.md b/.changeset/ninety-otters-report.md new file mode 100644 index 0000000000..2b70822042 --- /dev/null +++ b/.changeset/ninety-otters-report.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-lighthouse-backend': major +--- + +Updated Ligthouse schedule configuration to fix crashes when using custom confinguration diff --git a/plugins/lighthouse-backend/README.md b/plugins/lighthouse-backend/README.md index 28845d3c73..cde9135c23 100644 --- a/plugins/lighthouse-backend/README.md +++ b/plugins/lighthouse-backend/README.md @@ -19,13 +19,13 @@ import { PluginEnvironment } from '../types'; import { CatalogClient } from '@backstage/catalog-client'; export default async function createPlugin(env: PluginEnvironment) { - const { logger, scheduler, config } = env; + const { logger, scheduler, config, tokenManager } = env; const catalogClient = new CatalogClient({ discoveryApi: env.discovery, }); - await createScheduler({ logger, scheduler, config, catalogClient }); + await createScheduler({ logger, scheduler, config, catalogClient tokenManager }); } ``` @@ -80,5 +80,8 @@ You can define how often and when the scheduler should run the audits: ```yaml lighthouse: schedule: - days: 1 + frequency: + hours: 12 # Default: days 1 + timeout: + minutes: 30 # Default: null ``` diff --git a/plugins/lighthouse-backend/src/config.ts b/plugins/lighthouse-backend/src/config.ts index bea1c35661..91b4fb1a52 100644 --- a/plugins/lighthouse-backend/src/config.ts +++ b/plugins/lighthouse-backend/src/config.ts @@ -14,52 +14,48 @@ * limitations under the License. */ +import { readTaskScheduleDefinitionFromConfig } from '@backstage/backend-tasks'; import { Config } from '@backstage/config'; import { HumanDuration as HumanDuration } from '@backstage/types'; export interface LighthouseAuditScheduleConfig { - schedule: HumanDuration; + frequency: HumanDuration; timeout: HumanDuration; auditDetail: HumanDuration; } /** @public */ export type LighthouseAuditSchedule = { - getSchedule: () => HumanDuration; + getFrequency: () => HumanDuration; getTimeout: () => HumanDuration; }; /** @public */ export class LighthouseAuditScheduleImpl implements LighthouseAuditSchedule { - static fromConfig(config: Config) { - const lighthouse = config.getOptionalConfig('lighthouse'); + static fromConfig(config: Config): LighthouseAuditScheduleImpl { + // const lighthouse = config.getOptionalConfig('lighthouse'); + const lighthouse = config.has('lighthouse.schedule') + ? readTaskScheduleDefinitionFromConfig( + config.getConfig('lighthouse.schedule'), + ) + : { + frequency: { days: 1 }, + timeout: {}, + }; - let schedule: HumanDuration = { days: 1 }; - let timeout: HumanDuration = {}; + const frequency = lighthouse.frequency as HumanDuration; + const timeout = lighthouse.timeout as HumanDuration; - if (lighthouse) { - const scheduleConfig = lighthouse.getOptionalConfig('schedule'); - const timeoutConfig = lighthouse.getOptionalConfig('timeout'); - - if (scheduleConfig) { - schedule = scheduleConfig as HumanDuration; - } - - if (timeoutConfig) { - timeout = timeoutConfig as HumanDuration; - } - } - - return new LighthouseAuditScheduleImpl(schedule, timeout); + return new LighthouseAuditScheduleImpl(frequency, timeout); } constructor( - private schedule: HumanDuration, + private frequency: HumanDuration, private timeout: HumanDuration, ) {} - getSchedule(): HumanDuration { - return this.schedule; + getFrequency(): HumanDuration { + return this.frequency; } getTimeout(): HumanDuration { diff --git a/plugins/lighthouse-backend/src/service/createScheduler.ts b/plugins/lighthouse-backend/src/service/createScheduler.ts index 498e459a09..33df1226b7 100644 --- a/plugins/lighthouse-backend/src/service/createScheduler.ts +++ b/plugins/lighthouse-backend/src/service/createScheduler.ts @@ -56,14 +56,14 @@ export async function createScheduler( logger.info( `Running with Scheduler Config ${JSON.stringify( - lighthouseAuditConfig.getSchedule(), + lighthouseAuditConfig.getFrequency(), )} and timeout ${JSON.stringify(lighthouseAuditConfig.getTimeout())}`, ); if (scheduler) { await scheduler.scheduleTask({ id: 'lighthouse_audit', - frequency: lighthouseAuditConfig.getSchedule(), + frequency: lighthouseAuditConfig.getFrequency(), timeout: lighthouseAuditConfig.getTimeout(), initialDelay: { minutes: 15 }, fn: async () => {