introduce durationToMilliseconds

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2023-06-16 11:46:13 +02:00
parent cf1376ff88
commit a5c5491ff5
7 changed files with 58 additions and 47 deletions
@@ -16,32 +16,11 @@
import { LifecycleService } from '@backstage/backend-plugin-api';
import { ServiceUnavailableError } from '@backstage/errors';
import { HumanDuration } from '@backstage/types';
import { HumanDuration, durationToMilliseconds } from '@backstage/types';
import { RequestHandler } from 'express';
export const DEFAULT_TIMEOUT = { seconds: 5 };
function durationToMs(duration: HumanDuration): number {
const {
years = 0,
months = 0,
weeks = 0,
days = 0,
hours = 0,
minutes = 0,
seconds = 0,
milliseconds = 0,
} = duration;
const totalDays = years * 365 + months * 30 + weeks * 7 + days;
const totalHours = totalDays * 24 + hours;
const totalMinutes = totalHours * 60 + minutes;
const totalSeconds = totalMinutes * 60 + seconds;
const totalMilliseconds = totalSeconds * 1000 + milliseconds;
return totalMilliseconds;
}
/**
* Options for {@link createLifecycleMiddleware}.
* @public
@@ -102,7 +81,7 @@ export function createLifecycleMiddleware(
waiting.clear();
});
const timeoutMs = durationToMs(startupRequestPauseTimeout);
const timeoutMs = durationToMilliseconds(startupRequestPauseTimeout);
return (_req, _res, next) => {
if (state === 'up') {