LinkSpin data in MySQL (Marty 2026-11-13: 'everything database-driven'). docstore.js: every .json/.jsonl data document under DATA_DIR lives in MySQL table docs (DOCSTORE_URL), served to the modules through their own fs calls from an in-memory copy, written through in order; one writer at a time (docs_writer), so an older copy during a deploy can never overwrite newer data; first boot imports each file and sets it aside (.imported-<date>). Spin-link clicks are rows in rot_hits (one insert per click instead of rewriting a 20 MB document; every click kept). Kept as files on purpose: chain-index cache, uploads, geo db, kits, marks, queues. Admin sessions load after the store. Rehearsed on a copy: 52/54 documents byte-identical (rotator.json minus hits, kb.json seeds refreshed), 97,579 clicks moved, links + clicks work after restart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-11-13 09:25:59 -06:00
parent d1c9d62801
commit 1554c262a6
3 changed files with 196 additions and 4 deletions
+6 -2
View File
@@ -101,6 +101,7 @@ const ROOT = __dirname;
const PUBLIC_DIR = path.join(ROOT, 'public');
const DATA_DIR = process.env.DATA_DIR || path.join(ROOT, 'data');
require('./safefs').install(DATA_DIR); // every .json data file is written atomically (2026-11-13 rotator.json loss)
const docstore = require('./docstore'); docstore.install(DATA_DIR); // data documents live in MySQL when DOCSTORE_URL is set (2026-11-13, database is the standard)
const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD || 'changeme';
const ADMIN_EMAIL = String(process.env.ADMIN_EMAIL || '').trim().toLowerCase();
const { SITE_HOST, SITE_URL } = require('./host');
@@ -110,7 +111,7 @@ const { SITE_HOST, SITE_URL } = require('./host');
const ADMIN_SESS_FILE = path.join(DATA_DIR, 'admin-sessions.json');
const ADMIN_TTL = 12 * 60 * 60 * 1000;
let adminSessions = {};
try { adminSessions = JSON.parse(fs.readFileSync(ADMIN_SESS_FILE, 'utf8')) || {}; } catch (e) { adminSessions = {}; }
if (!process.env.DOCSTORE_URL) { try { adminSessions = JSON.parse(fs.readFileSync(ADMIN_SESS_FILE, 'utf8')) || {}; } catch (e) { adminSessions = {}; } } // with MySQL: loaded right after docstore.init
function saveAdminSessions() {
const now = Date.now();
for (const k of Object.keys(adminSessions)) if (!adminSessions[k] || adminSessions[k].expires < now) delete adminSessions[k];
@@ -628,6 +629,9 @@ async function editAd(id, b, who) {
}
async function boot() {
await db.init({ dataDir: DATA_DIR }); // no-op without DATABASE_URL (JSON mode)
await docstore.init({ splits: [{ doc: 'rotator.json', field: 'hits', table: 'rot_hits', key: 'id', ts: 'ts' }] }); // before any module reads its data
process.on('SIGTERM', () => { docstore.drain(8000).finally(() => process.exit(0)); });
if (process.env.DOCSTORE_URL) { try { adminSessions = JSON.parse(fs.readFileSync(ADMIN_SESS_FILE, 'utf8')) || {}; } catch (e) { adminSessions = {}; } }
marks.init({ db, dataDir: DATA_DIR });
marks.ensureTable().catch(e => console.error('marks table', e.message));
setInterval(() => marks.prune().catch(() => {}), 6 * 3600 * 1000).unref();
@@ -4499,6 +4503,6 @@ const server = http.createServer(async (req, res) => {
}
});
boot().then(() => {
server.listen(PORT, () => console.log(`LinkSpin site on :${PORT} — chain: ${chain.getConfig().chainName} — store: ${db.enabled() ? 'MySQL' : 'volume JSON'}`
server.listen(PORT, () => console.log(`LinkSpin site on :${PORT} — chain: ${chain.getConfig().chainName} — store: ${db.enabled() ? 'MySQL' : docstore.isOn() ? 'MySQL documents' : 'volume JSON'}`
+ ` — outbound mail: ${OUTBOUND_MAIL ? 'ON' : 'OFF'} — sign-ups: ${SIGNUPS_OPEN ? 'open' : 'CLOSED'}`));
}).catch(e => { console.error('boot failed:', e.message); process.exit(1); });