MCPcopy Create free account
hub / github.com/QodeXcli/QodeX / tick

Function tick

src/schedule/runner.ts:33–80  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

31}
32
33export async function tick(): Promise<TickResult> {
34 await fs.mkdir(QODEX_HOME, { recursive: true });
35 await fs.mkdir(RUN_LOG_DIR, { recursive: true });
36
37 // Acquire exclusive lock via O_CREAT|O_EXCL. If it exists and is < 10 minutes
38 // old we assume another tick is alive; otherwise it's stale and we steal it.
39 let lockFd: fsSync.promises.FileHandle | null = null;
40 try {
41 lockFd = await fs.open(LOCK_PATH, 'wx');
42 } catch (e: any) {
43 if (e.code === 'EEXIST') {
44 const stat = await fs.stat(LOCK_PATH).catch(() => null);
45 const ageMs = stat ? Date.now() - stat.mtimeMs : Infinity;
46 if (ageMs < 10 * 60 * 1000) {
47 return { ranIds: [], skipped: [], failed: [], acquired: false };
48 }
49 // Stale — steal it
50 try { await fs.unlink(LOCK_PATH); } catch {}
51 lockFd = await fs.open(LOCK_PATH, 'wx').catch(() => null);
52 }
53 }
54 if (!lockFd) {
55 return { ranIds: [], skipped: [], failed: [], acquired: false };
56 }
57 await lockFd.writeFile(`pid=${process.pid}\nstarted=${new Date().toISOString()}\n`);
58
59 const result: TickResult = { ranIds: [], skipped: [], failed: [], acquired: true };
60
61 try {
62 const store = getScheduleStore();
63 const due = store.dueAsOf(new Date());
64
65 for (const entry of due) {
66 try {
67 await runOne(entry);
68 result.ranIds.push(entry.id);
69 } catch (e: any) {
70 logger.warn('schedule run failed', { id: entry.id, name: entry.name, err: e.message });
71 result.failed.push(entry.id);
72 }
73 }
74 } finally {
75 try { await lockFd.close(); } catch {}
76 try { await fs.unlink(LOCK_PATH); } catch {}
77 }
78
79 return result;
80}
81
82async function runOne(entry: ScheduleEntry): Promise<void> {
83 const store = getScheduleStore();

Callers 1

index.tsFile · 0.85

Calls 5

getScheduleStoreFunction · 0.85
dueAsOfMethod · 0.80
warnMethod · 0.80
runOneFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected