mirror of
https://github.com/theodore-song/polymarket-analyst.git
synced 2026-07-28 00:17:46 +00:00
Make cloud state authoritative hourly
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { get, put } from "@vercel/blob";
|
||||
|
||||
const STATE_PATH = process.env.PMA_STATE_PATH || "shared/state.json";
|
||||
const AGENTS_KEY = "pma_agents_v2";
|
||||
|
||||
async function readJsonBlob() {
|
||||
const blob = await get(STATE_PATH, { access: "private" });
|
||||
@@ -9,6 +10,15 @@ async function readJsonBlob() {
|
||||
return text ? JSON.parse(text) : null;
|
||||
}
|
||||
|
||||
function agentStateFromItems(items) {
|
||||
if (!items || !items[AGENTS_KEY]) return null;
|
||||
try {
|
||||
return JSON.parse(items[AGENTS_KEY]);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export default async function handler(req, res) {
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
try {
|
||||
@@ -25,6 +35,16 @@ export default async function handler(req, res) {
|
||||
if (!body || typeof body !== "object" || !body.items || typeof body.items !== "object") {
|
||||
return res.status(400).json({ ok: false, error: "Invalid state payload" });
|
||||
}
|
||||
const current = await readJsonBlob();
|
||||
const currentAgents = agentStateFromItems(current && current.items);
|
||||
const incomingAgents = agentStateFromItems(body.items);
|
||||
if (!body.force && currentAgents && incomingAgents) {
|
||||
const sameHour = currentAgents.last_cycle_hour && currentAgents.last_cycle_hour === incomingAgents.last_cycle_hour;
|
||||
const differentRun = currentAgents.last_run && incomingAgents.last_run && currentAgents.last_run !== incomingAgents.last_run;
|
||||
if (sameHour && differentRun) {
|
||||
return res.status(409).json({ ok: false, error: "This cycle hour already has a cloud result", state: current });
|
||||
}
|
||||
}
|
||||
const state = { version: 1, updated_at: new Date().toISOString(), items: body.items };
|
||||
await put(STATE_PATH, JSON.stringify(state), {
|
||||
access: "private",
|
||||
|
||||
Reference in New Issue
Block a user