Skip to content

Commit fb4fd8b

Browse files
committed
feat(w3c): auto-refresh groups list every 24 hours
Adds a setInterval that calls the update script daily and reloads the in-memory groups data from disk. Follows the existing pattern from xref's periodic cache invalidation. Also handles missing groups.json gracefully at startup (empty store instead of crash). Closes #395
1 parent 5916d39 commit fb4fd8b

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

routes/w3c/group.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import path from "path";
2-
import { readFileSync } from "fs";
2+
import { readFileSync, existsSync } from "fs";
33

44
import { Request, Response } from "express";
55

66
import { MemCache } from "../../utils/mem-cache.js";
77
import { env, ms, seconds, HTTPError } from "../../utils/misc.js";
8+
import update from "../../scripts/update-w3c-groups-list.js";
89

910
const DATA_DIR = env("DATA_DIR");
1011
const dataSource = path.join(DATA_DIR, "w3c/groups.json");
@@ -18,7 +19,33 @@ type GroupType = "wg" | "cg" | "ig" | "bg" | "other";
1819
export type Groups = Record<string, GroupMeta>;
1920
export type GroupsByType = Record<GroupType, Groups>;
2021

21-
const groups: GroupsByType = JSON.parse(readFileSync(dataSource, "utf-8"));
22+
let groups: GroupsByType = existsSync(dataSource)
23+
? JSON.parse(readFileSync(dataSource, "utf-8"))
24+
: { wg: {}, cg: {}, ig: {}, bg: {}, other: {} };
25+
26+
function reloadGroups() {
27+
try {
28+
if (existsSync(dataSource)) {
29+
groups = JSON.parse(readFileSync(dataSource, "utf-8"));
30+
cache.clear();
31+
}
32+
} catch (error) {
33+
console.error("Failed to reload groups.json:", error);
34+
}
35+
}
36+
37+
async function refreshGroups() {
38+
try {
39+
await update();
40+
reloadGroups();
41+
console.log("W3C groups list refreshed.");
42+
} catch (error) {
43+
console.error("Failed to refresh W3C groups:", error);
44+
}
45+
}
46+
47+
setInterval(refreshGroups, ms("24h"));
48+
if (!existsSync(dataSource)) refreshGroups();
2249

2350
interface Group {
2451
id: number;

0 commit comments

Comments
 (0)