Refill notices on LinkSpin (Fuel Your Offers stage 1, Marty approved 2026-10-24): refill.js copied from InstantAdPay. Low (80% of budget delivered, refillLowPct), out, and idle (7 days after an unanswered out notice, 100+ credits free, nothing running): one email + one on-site notice per owner per pass, naming each ad and what it delivered, then the relaunch line (100+ credits free) or the /credits link, then the packages from the chain catalog and a streak-line hook for stage 2. Email skips drip unsubscribes and carries the unsubscribe link; the notice (kind notice) always goes. Every send is watched 45 days for purchase / relaunch / resumed / topup. marks.js: one-time claims shared by both app copies during a deploy (event_marks with a DB, exclusive file create on the shared volume without one); every send is claimed first. Settings refillNotices '1' (default on) + refillLowPct 80; admin GET /api/admin/refill, POST /run and /reset, an Overview card (admin-refill.js). qa/refill.test.cjs (12 checks incl. two app copies on one volume) + run.sh fuel mode.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
// 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 };
|
||||
Reference in New Issue
Block a user