Fixed crash when leveraging custom Lighthouse config

Signed-off-by: Florian JUDITH <florian.judith.b@gmail.com>
This commit is contained in:
Florian JUDITH
2023-11-21 23:50:25 -05:00
parent f28c5a0b66
commit 7f0dbfd35a
4 changed files with 32 additions and 28 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-lighthouse-backend': major
---
Updated Ligthouse schedule configuration to fix crashes when using custom confinguration
+6 -3
View File
@@ -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
```
+19 -23
View File
@@ -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 {
@@ -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 () => {