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