ab4f1693df
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
63 lines
3.1 KiB
JavaScript
63 lines
3.1 KiB
JavaScript
// One-time claims shared by every running copy of the app (same idea as InstantAdPay's marks.js, 2026-10-24).
|
|
//
|
|
// Why: a Coolify deploy starts the new container and only stops the old one once the new one is healthy, so for
|
|
// about half a minute two copies of this app run side by side. Anything deduped in process memory or in a JSON
|
|
// file that each copy rewrites from its own memory can fire twice (InstantAdPay, 2026-09-25: two emails and a
|
|
// doubled Friday bonus). `claim(key)` is true for exactly one caller across all copies, false for everyone else.
|
|
//
|
|
// With a database: a row in event_marks that only one INSERT can win. Without one (LinkSpin production runs on
|
|
// the JSON store): an exclusive file create (open with 'wx') in DATA_DIR/marks/. Both copies mount the same
|
|
// volume, and O_EXCL creation is atomic on it, so exactly one copy gets the file. Marks older than 60 days are
|
|
// pruned: every event they guard has long since been handled.
|
|
'use strict';
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const crypto = require('crypto');
|
|
|
|
let db = null, DIR = null;
|
|
const local = new Set();
|
|
|
|
function init(deps) {
|
|
db = deps && deps.db;
|
|
DIR = deps && deps.dataDir ? path.join(deps.dataDir, 'marks') : null;
|
|
if (DIR) { try { fs.mkdirSync(DIR, { recursive: true }); } catch (e) {} }
|
|
}
|
|
const dbOn = () => !!(db && db.enabled && db.enabled());
|
|
|
|
async function ensureTable() {
|
|
if (!dbOn()) return;
|
|
await db.q('CREATE TABLE IF NOT EXISTS event_marks (k VARCHAR(160) PRIMARY KEY, ts BIGINT NOT NULL) ENGINE=InnoDB');
|
|
}
|
|
const fileOf = key => path.join(DIR, crypto.createHash('sha1').update(key).digest('hex') + '.m');
|
|
|
|
// true = this caller owns the key; false = someone (possibly another copy of the app) already does.
|
|
async function claim(key) {
|
|
key = String(key).slice(0, 160);
|
|
if (dbOn()) {
|
|
try {
|
|
const r = await db.q('INSERT IGNORE INTO event_marks (k, ts) VALUES (?, ?)', [key, Date.now()]);
|
|
return !!(r && r.affectedRows === 1);
|
|
} catch (e) { console.error('marks: claim fell back to the file for ' + key + ': ' + e.message); }
|
|
}
|
|
if (DIR) {
|
|
try { const fd = fs.openSync(fileOf(key), 'wx'); try { fs.writeSync(fd, key + '\n' + Date.now()); } finally { fs.closeSync(fd); } return true; }
|
|
catch (e) { if (e.code === 'EEXIST') return false; console.error('marks: file claim failed for ' + key + ': ' + e.message); }
|
|
}
|
|
if (local.has(key)) return false; local.add(key); return true;
|
|
}
|
|
// whether a key was already claimed (read only, never claims)
|
|
async function has(key) {
|
|
key = String(key).slice(0, 160);
|
|
if (dbOn()) { try { const r = await db.q('SELECT 1 FROM event_marks WHERE k=?', [key]); return r.length > 0; } catch (e) {} }
|
|
if (DIR) return fs.existsSync(fileOf(key));
|
|
return local.has(key);
|
|
}
|
|
|
|
async function prune() {
|
|
const cut = Date.now() - 60 * 86400000;
|
|
if (dbOn()) { try { await db.q('DELETE FROM event_marks WHERE ts < ?', [cut]); } catch (e) {} }
|
|
if (DIR) { try { for (const f of fs.readdirSync(DIR)) { const p = path.join(DIR, f); try { if (fs.statSync(p).mtimeMs < cut) fs.unlinkSync(p); } catch (e) {} } } catch (e) {} }
|
|
}
|
|
|
|
module.exports = { init, ensureTable, claim, has, prune };
|