Enabled backward config compatibility
Signed-off-by: Florian JUDITH <florian.judith.b@gmail.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-lighthouse-backend': major
|
||||
'@backstage/plugin-lighthouse-backend': minor
|
||||
---
|
||||
|
||||
Updated Lighthouse schedule configuration to fix crashes when using custom configuration
|
||||
|
||||
@@ -13,52 +13,66 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { LoggerService } from '@backstage/backend-plugin-api';
|
||||
import {
|
||||
TaskScheduleDefinition,
|
||||
readTaskScheduleDefinitionFromConfig,
|
||||
} from '@backstage/backend-tasks';
|
||||
import { Config, readDurationFromConfig } from '@backstage/config';
|
||||
import { HumanDuration } from '@backstage/types';
|
||||
|
||||
import { readTaskScheduleDefinitionFromConfig } from '@backstage/backend-tasks';
|
||||
import { Config } from '@backstage/config';
|
||||
import { HumanDuration as HumanDuration } from '@backstage/types';
|
||||
|
||||
export interface LighthouseAuditScheduleConfig {
|
||||
frequency: HumanDuration;
|
||||
timeout: HumanDuration;
|
||||
auditDetail: HumanDuration;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export type LighthouseAuditSchedule = {
|
||||
getFrequency: () => HumanDuration;
|
||||
getTimeout: () => HumanDuration;
|
||||
type Options = {
|
||||
logger: LoggerService;
|
||||
};
|
||||
|
||||
/** @public */
|
||||
export class LighthouseAuditScheduleImpl implements LighthouseAuditSchedule {
|
||||
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: {},
|
||||
};
|
||||
export class LighthouseAuditScheduleImpl implements TaskScheduleDefinition {
|
||||
/**
|
||||
* Looks at the `lighthouse.schedule` section in the application configuration
|
||||
* and returns a TaskScheduleDefinition.
|
||||
* Defaults to `{ frequency: { days: 1 }, timeout: {}, initialDelay: { minutes; 15 } }`
|
||||
*
|
||||
* @returns a TaskScheduleDefinition
|
||||
*/
|
||||
static fromConfig(config: Config, options: Options): TaskScheduleDefinition {
|
||||
const { logger } = options;
|
||||
|
||||
const frequency = lighthouse.frequency as HumanDuration;
|
||||
const timeout = lighthouse.timeout as HumanDuration;
|
||||
let lighthouse: TaskScheduleDefinition = {
|
||||
frequency: { days: 1 },
|
||||
timeout: {},
|
||||
initialDelay: { minutes: 15 },
|
||||
};
|
||||
|
||||
return new LighthouseAuditScheduleImpl(frequency, timeout);
|
||||
if (config.has('lighthouse.schedule.frequency')) {
|
||||
lighthouse = readTaskScheduleDefinitionFromConfig(
|
||||
config.getConfig('lighthouse.schedule'),
|
||||
);
|
||||
} else if (config.has('lighthouse.schedule')) {
|
||||
logger.warn(
|
||||
`[Deprecation] Please migrate the schedule configuration to 'lighthouse.schedule.frequency' in ${config.config.context}`,
|
||||
);
|
||||
|
||||
lighthouse.frequency = readDurationFromConfig(
|
||||
config.getConfig('lighthouse.schedule'),
|
||||
);
|
||||
}
|
||||
|
||||
if (config.has('lighthouse.timeout')) {
|
||||
logger.warn(
|
||||
`[Deprecation] Please migrate the timeout configuration to 'lighthouse.schedule.timeout' in ${config.config.context}`,
|
||||
);
|
||||
|
||||
lighthouse.timeout = readDurationFromConfig(
|
||||
config.getConfig('lighthouse.timeout'),
|
||||
);
|
||||
}
|
||||
|
||||
return lighthouse;
|
||||
}
|
||||
|
||||
constructor(
|
||||
private frequency: HumanDuration,
|
||||
private timeout: HumanDuration,
|
||||
public frequency: HumanDuration,
|
||||
public timeout: HumanDuration,
|
||||
public initialDelay: HumanDuration,
|
||||
) {}
|
||||
|
||||
getFrequency(): HumanDuration {
|
||||
return this.frequency;
|
||||
}
|
||||
|
||||
getTimeout(): HumanDuration {
|
||||
return this.timeout;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,9 @@ export async function createScheduler(
|
||||
const { logger, scheduler, catalogClient, config, tokenManager } = options;
|
||||
const lighthouseApi = LighthouseRestApi.fromConfig(config);
|
||||
|
||||
const lighthouseAuditConfig = LighthouseAuditScheduleImpl.fromConfig(config);
|
||||
const lighthouseAuditConfig = LighthouseAuditScheduleImpl.fromConfig(config, {
|
||||
logger,
|
||||
});
|
||||
const formFactorToScreenEmulationMap = {
|
||||
// the default is mobile, so no need to override
|
||||
mobile: undefined,
|
||||
@@ -56,16 +58,16 @@ export async function createScheduler(
|
||||
|
||||
logger.info(
|
||||
`Running with Scheduler Config ${JSON.stringify(
|
||||
lighthouseAuditConfig.getFrequency(),
|
||||
)} and timeout ${JSON.stringify(lighthouseAuditConfig.getTimeout())}`,
|
||||
lighthouseAuditConfig.frequency,
|
||||
)} and timeout ${JSON.stringify(lighthouseAuditConfig.timeout)}`,
|
||||
);
|
||||
|
||||
if (scheduler) {
|
||||
await scheduler.scheduleTask({
|
||||
id: 'lighthouse_audit',
|
||||
frequency: lighthouseAuditConfig.getFrequency(),
|
||||
timeout: lighthouseAuditConfig.getTimeout(),
|
||||
initialDelay: { minutes: 15 },
|
||||
frequency: lighthouseAuditConfig.frequency,
|
||||
timeout: lighthouseAuditConfig.timeout,
|
||||
initialDelay: lighthouseAuditConfig.initialDelay,
|
||||
fn: async () => {
|
||||
const filter: Record<string, symbol | string> = {
|
||||
kind: 'Component',
|
||||
|
||||
Reference in New Issue
Block a user