MCPcopy Create free account
hub / github.com/Noumena-Network/code / readCronTasks

Function readCronTasks

src/utils/cronTasks.ts:109–158  ·  view source on GitHub ↗
(dir?: string)

Source from the content-addressed store, hash-verified

107 * blocks the whole file.
108 */
109export async function readCronTasks(dir?: string): Promise<CronTask[]> {
110 const fs = getFsImplementation()
111 let raw: string
112 try {
113 raw = await fs.readFile(getReadableCronFilePath(dir), { encoding: 'utf-8' })
114 } catch (e: unknown) {
115 if (isFsInaccessible(e)) return []
116 logError(e)
117 return []
118 }
119
120 const parsed = safeParseJSON(raw, false)
121 if (!parsed || typeof parsed !== 'object') return []
122 const file = parsed as Partial<CronFile>
123 if (!Array.isArray(file.tasks)) return []
124
125 const out: CronTask[] = []
126 for (const t of file.tasks) {
127 if (
128 !t ||
129 typeof t.id !== 'string' ||
130 typeof t.cron !== 'string' ||
131 typeof t.prompt !== 'string' ||
132 typeof t.createdAt !== 'number'
133 ) {
134 logForDebugging(
135 `[ScheduledTasks] skipping malformed task: ${jsonStringify(t)}`,
136 )
137 continue
138 }
139 if (!parseCronExpression(t.cron)) {
140 logForDebugging(
141 `[ScheduledTasks] skipping task ${t.id} with invalid cron '${t.cron}'`,
142 )
143 continue
144 }
145 out.push({
146 id: t.id,
147 cron: t.cron,
148 prompt: t.prompt,
149 createdAt: t.createdAt,
150 ...(typeof t.lastFiredAt === 'number'
151 ? { lastFiredAt: t.lastFiredAt }
152 : {}),
153 ...(t.recurring ? { recurring: true } : {}),
154 ...(t.permanent ? { permanent: true } : {}),
155 })
156 }
157 return out
158}
159
160/**
161 * Sync check for whether the cron file has any valid tasks. Used by

Callers 6

addCronTaskFunction · 0.85
removeCronTasksFunction · 0.85
markCronTasksFiredFunction · 0.85
listAllCronTasksFunction · 0.85
loadFunction · 0.85

Calls 8

getFsImplementationFunction · 0.85
getReadableCronFilePathFunction · 0.85
isFsInaccessibleFunction · 0.85
jsonStringifyFunction · 0.85
parseCronExpressionFunction · 0.85
readFileMethod · 0.80
logErrorFunction · 0.70
logForDebuggingFunction · 0.70

Tested by

no test coverage detected