Data safety (2026-11-13 incident: a deploy read a half-written rotator.json, the store fell back to empty and its next save wiped 294 spin links): safefs.js makes every .json write inside DATA_DIR atomic (temp file + rename); LinkSpin's rotator refuses to start empty when rotator.json exists but will not parse
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+11
-1
@@ -18,7 +18,17 @@ const DAY = 86400000;
|
||||
const J = {
|
||||
db: null,
|
||||
FILE: () => path.join(DATA_DIR, 'rotator.json'),
|
||||
load() { try { this.db = JSON.parse(fs.readFileSync(this.FILE(), 'utf8')); } catch (e) { this.db = { v: 1, rotations: [], hits: [], conv: [] }; } if (!this.db.conv) this.db.conv = []; },
|
||||
// a missing file starts empty; a file that is there but will not parse is NEVER replaced with an empty list (2026-11-13: that
|
||||
// fallback wiped 294 spin links when a deploy read a half-written file). Retry a moment, then stop the app so it restarts.
|
||||
load() {
|
||||
let last = null;
|
||||
for (let i = 0; i < 10; i++) {
|
||||
try { this.db = JSON.parse(fs.readFileSync(this.FILE(), 'utf8')); last = null; break; }
|
||||
catch (e) { last = e; if (e.code === 'ENOENT') { this.db = { v: 1, rotations: [], hits: [], conv: [] }; last = null; break; } Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 500); }
|
||||
}
|
||||
if (last) throw new Error('rotator.json exists but cannot be read (' + last.message + '); refusing to start with no spin links');
|
||||
if (!this.db.conv) this.db.conv = [];
|
||||
},
|
||||
save() { try { fs.writeFileSync(this.FILE(), JSON.stringify(this.db)); } catch (e) {} }
|
||||
};
|
||||
function init(opts) { DATA_DIR = opts.dataDir; geo = opts.geo || null; OWN_HOSTS = opts.ownHosts || (() => []); SOURCES = opts.sources || null; if (opts.wantsClickId) WANTS_CLICK_ID = opts.wantsClickId; if (opts.isNetworkHost) IS_NETWORK = opts.isNetworkHost; if (opts.goalRows) GOAL_ROWS = opts.goalRows; J.load(); }
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
// Atomic JSON data files (2026-11-13 incident). Every store here saves with fs.writeFileSync(file.json, ...), which truncates
|
||||
// the file and then writes it. During a deploy the old and new app copies share the volume for a few seconds; the new copy read
|
||||
// rotator.json while the old one was half way through writing it, JSON.parse failed, the store fell back to EMPTY and its next
|
||||
// save wrote that empty list over 294 spin links. This makes every .json write inside DATA_DIR atomic (write a temp file in the
|
||||
// same folder, then rename it over the real one), so a reader only ever sees the old file or the new one, never half of one.
|
||||
// The SAME file ships in InstantAdPay and LinkSpin; server.js installs it right after DATA_DIR is known.
|
||||
'use strict';
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
let installed = false;
|
||||
function install(dataDir) {
|
||||
if (installed) return; installed = true;
|
||||
const root = path.resolve(dataDir) + path.sep;
|
||||
const orig = fs.writeFileSync;
|
||||
fs.writeFileSync = function (file, data, opts) {
|
||||
const flag = opts && typeof opts === 'object' ? opts.flag : null;
|
||||
if (typeof file === 'string' && /\.json$/i.test(file) && (!flag || flag === 'w') && path.resolve(file).startsWith(root)) {
|
||||
const tmp = file + '.tmp-' + process.pid + '-' + Math.random().toString(36).slice(2, 8);
|
||||
try { orig.call(fs, tmp, data, opts); fs.renameSync(tmp, file); }
|
||||
catch (e) { try { fs.unlinkSync(tmp); } catch (x) {} throw e; }
|
||||
return;
|
||||
}
|
||||
return orig.apply(fs, arguments);
|
||||
};
|
||||
}
|
||||
module.exports = { install };
|
||||
@@ -100,6 +100,7 @@ const PORT = Number(process.env.PORT || 3000);
|
||||
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 ADMIN_PASSWORD = process.env.ADMIN_PASSWORD || 'changeme';
|
||||
const ADMIN_EMAIL = String(process.env.ADMIN_EMAIL || '').trim().toLowerCase();
|
||||
const { SITE_HOST, SITE_URL } = require('./host');
|
||||
|
||||
Reference in New Issue
Block a user