(dir?: string)
| 89 | * blocks the whole file. |
| 90 | */ |
| 91 | export async function readCronTasks(dir?: string): Promise<CronTask[]> { |
| 92 | const fs = getFsImplementation() |
| 93 | let raw: string |
| 94 | try { |
| 95 | raw = await fs.readFile(getCronFilePath(dir), { encoding: 'utf-8' }) |
| 96 | } catch (e: unknown) { |
| 97 | if (isFsInaccessible(e)) return [] |
| 98 | logError(e) |
| 99 | return [] |
| 100 | } |
| 101 | |
| 102 | const parsed = safeParseJSON(raw, false) |
| 103 | if (!parsed || typeof parsed !== 'object') return [] |
| 104 | const file = parsed as Partial<CronFile> |
| 105 | if (!Array.isArray(file.tasks)) return [] |
| 106 | |
| 107 | const out: CronTask[] = [] |
| 108 | for (const t of file.tasks) { |
| 109 | if ( |
| 110 | !t || |
| 111 | typeof t.id !== 'string' || |
| 112 | typeof t.cron !== 'string' || |
| 113 | typeof t.prompt !== 'string' || |
| 114 | typeof t.createdAt !== 'number' |
| 115 | ) { |
| 116 | logForDebugging( |
| 117 | `[ScheduledTasks] skipping malformed task: ${jsonStringify(t)}`, |
| 118 | ) |
| 119 | continue |
| 120 | } |
| 121 | if (!parseCronExpression(t.cron)) { |
| 122 | logForDebugging( |
| 123 | `[ScheduledTasks] skipping task ${t.id} with invalid cron '${t.cron}'`, |
| 124 | ) |
| 125 | continue |
| 126 | } |
| 127 | out.push({ |
| 128 | id: t.id, |
| 129 | cron: t.cron, |
| 130 | prompt: t.prompt, |
| 131 | createdAt: t.createdAt, |
| 132 | ...(typeof t.lastFiredAt === 'number' |
| 133 | ? { lastFiredAt: t.lastFiredAt } |
| 134 | : {}), |
| 135 | ...(t.recurring ? { recurring: true } : {}), |
| 136 | ...(t.permanent ? { permanent: true } : {}), |
| 137 | }) |
| 138 | } |
| 139 | return out |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Sync check for whether the cron file has any valid tasks. Used by |
no test coverage detected