81 lines
1.9 KiB
TypeScript
81 lines
1.9 KiB
TypeScript
export type Schedule =
|
|
| "sunday" | "monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday"
|
|
| "week" | "day" | "midnight" | "noon" | "hour"
|
|
| "30 minutes" | "15 minutes" | "5 minutes" | "1 minute"
|
|
| "30minutes" | "15minutes" | "5minutes" | "1minute"
|
|
| 30 | 15 | 5 | 1
|
|
|
|
export type CronJob = {
|
|
id: string // "appname:filename"
|
|
app: string
|
|
name: string // filename without .ts
|
|
file: string // full path
|
|
schedule: Schedule
|
|
cronExpr: string
|
|
state: 'idle' | 'running' | 'disabled'
|
|
lastRun?: number
|
|
lastDuration?: number
|
|
lastExitCode?: number
|
|
lastError?: string
|
|
nextRun?: number
|
|
}
|
|
|
|
export const SCHEDULES = [
|
|
'1 minute',
|
|
'5 minutes',
|
|
'15 minutes',
|
|
'30 minutes',
|
|
'hour',
|
|
'noon',
|
|
'midnight',
|
|
'day',
|
|
'week',
|
|
'sunday',
|
|
'monday',
|
|
'tuesday',
|
|
'wednesday',
|
|
'thursday',
|
|
'friday',
|
|
'saturday',
|
|
] as const
|
|
|
|
const SCHEDULE_MAP: Record<string, string> = {
|
|
sunday: '0 0 * * 0',
|
|
monday: '0 0 * * 1',
|
|
tuesday: '0 0 * * 2',
|
|
wednesday: '0 0 * * 3',
|
|
thursday: '0 0 * * 4',
|
|
friday: '0 0 * * 5',
|
|
saturday: '0 0 * * 6',
|
|
week: '0 0 * * 0',
|
|
day: '0 0 * * *',
|
|
midnight: '0 0 * * *',
|
|
noon: '0 12 * * *',
|
|
hour: '0 * * * *',
|
|
'30 minutes': '0,30 * * * *',
|
|
'15 minutes': '0,15,30,45 * * * *',
|
|
'5 minutes': '*/5 * * * *',
|
|
'1 minute': '* * * * *',
|
|
'30minutes': '0,30 * * * *',
|
|
'15minutes': '0,15,30,45 * * * *',
|
|
'5minutes': '*/5 * * * *',
|
|
'1minute': '* * * * *',
|
|
}
|
|
|
|
export function toCronExpr(schedule: Schedule): string {
|
|
if (typeof schedule === 'number') {
|
|
return SCHEDULE_MAP[`${schedule}minutes`]!
|
|
}
|
|
return SCHEDULE_MAP[schedule]!
|
|
}
|
|
|
|
export function isValidSchedule(value: unknown): value is Schedule {
|
|
if (typeof value === 'number') {
|
|
return [1, 5, 15, 30].includes(value)
|
|
}
|
|
if (typeof value === 'string') {
|
|
return value in SCHEDULE_MAP
|
|
}
|
|
return false
|
|
}
|