You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.1 KiB
84 lines
2.1 KiB
import {
|
|
LogLevel,
|
|
createCatalogDiagnostics,
|
|
type DiagnosticCatalog,
|
|
type DiagnosticEvent,
|
|
type Diagnostics
|
|
} from '$libs/logr';
|
|
import {
|
|
LOGGER_CATEGORY,
|
|
LOG_MSG_SCHEDULE_AT_PAST,
|
|
TIMER_DIAGNOSTIC_EVENTS,
|
|
TIMER_EVENT_FAILED,
|
|
listenerThrewMessage,
|
|
taskFailedMessage
|
|
} from './consts.ts';
|
|
import type { Logger } from '$libs/logr';
|
|
import type { TimerEvent } from '$libs/timers';
|
|
|
|
export type TimerDiagnosticType =
|
|
(typeof TIMER_DIAGNOSTIC_EVENTS)[keyof typeof TIMER_DIAGNOSTIC_EVENTS];
|
|
|
|
export type TimerDiagnosticMeta =
|
|
| {
|
|
readonly error: unknown;
|
|
readonly event: TimerEvent['type'];
|
|
}
|
|
| {
|
|
readonly error: unknown;
|
|
readonly key: string;
|
|
}
|
|
| {
|
|
readonly key: string;
|
|
readonly dueAt: number;
|
|
readonly now: number;
|
|
};
|
|
|
|
export type TimerDiagnosticEvent = DiagnosticEvent<TimerDiagnosticType, TimerDiagnosticMeta>;
|
|
export type TimerDiagnostics = Diagnostics<TimerDiagnosticEvent>;
|
|
|
|
const TIMER_DIAGNOSTIC_LOGS: DiagnosticCatalog<TimerDiagnosticEvent> = {
|
|
[TIMER_DIAGNOSTIC_EVENTS.LISTENER_THREW]: (event) => ({
|
|
level: LogLevel.ERROR,
|
|
message: listenerThrewMessage(timerEventName(event.meta))
|
|
}),
|
|
[TIMER_DIAGNOSTIC_EVENTS.SCHEDULE_AT_PAST]: {
|
|
level: LogLevel.DEBUG,
|
|
message: LOG_MSG_SCHEDULE_AT_PAST
|
|
},
|
|
[TIMER_DIAGNOSTIC_EVENTS.TASK_FAILED]: (event) => ({
|
|
level: LogLevel.ERROR,
|
|
message: taskFailedMessage(timerKeyName(event.meta))
|
|
})
|
|
};
|
|
|
|
function timerEventName(meta: TimerDiagnosticMeta | undefined): TimerEvent['type'] {
|
|
if (meta !== undefined && 'event' in meta) return meta.event;
|
|
return TIMER_EVENT_FAILED;
|
|
}
|
|
|
|
function timerKeyName(meta: TimerDiagnosticMeta | undefined): string {
|
|
if (meta !== undefined && 'key' in meta) return meta.key;
|
|
return LOGGER_CATEGORY;
|
|
}
|
|
|
|
export function createTimerDiagnostics(logger?: Logger): TimerDiagnostics {
|
|
return createCatalogDiagnostics({
|
|
logger,
|
|
defaultCategory: LOGGER_CATEGORY,
|
|
catalog: TIMER_DIAGNOSTIC_LOGS
|
|
});
|
|
}
|
|
|
|
export function emitTimerDiagnostic(
|
|
diagnostics: TimerDiagnostics,
|
|
type: TimerDiagnosticType,
|
|
meta: TimerDiagnosticMeta
|
|
): void {
|
|
diagnostics.emit({
|
|
artifact: LOGGER_CATEGORY,
|
|
type,
|
|
meta
|
|
});
|
|
}
|