mirror of
https://github.com/multipleof4/KalBot.git
synced 2026-03-17 05:51:02 +00:00
Fix: Setup background task to poll delayed results
This commit is contained in:
52
worker.js
52
worker.js
@@ -25,22 +25,41 @@ async function main() {
|
||||
new ThresholdStrategy({ triggerPct: 65, betSize: 1 })
|
||||
];
|
||||
|
||||
// Ensure each strategy has a paper account initialized
|
||||
for (const s of strategies) {
|
||||
paper._getAccount(s.name);
|
||||
}
|
||||
|
||||
console.log(`[Worker] Loaded ${strategies.length} strategies: ${strategies.map((s) => s.name).join(', ')}`);
|
||||
|
||||
// Settle any orphaned positions from previous runs before starting
|
||||
let latestMarketState = null;
|
||||
|
||||
async function processOrphans() {
|
||||
if (paper._resetting) return;
|
||||
try {
|
||||
await paper.settleOrphans(getMarket);
|
||||
const { settled, expired } = await paper.checkOrphans(getMarket);
|
||||
const allResolved = [...settled, ...expired];
|
||||
if (allResolved.length > 0) {
|
||||
for (const strategy of strategies) {
|
||||
for (const trade of allResolved) {
|
||||
if (trade.strategy === strategy.name) {
|
||||
strategy.onSettlement(trade.result, trade);
|
||||
}
|
||||
}
|
||||
}
|
||||
writeState(latestMarketState, paper, strategies);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[Worker] Orphan settlement error:', e.message);
|
||||
console.error('[Worker] Orphan check error:', e.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Settle delayed positions before continuing
|
||||
await processOrphans();
|
||||
|
||||
// Continuously check open positions every 60s for delayed results
|
||||
setInterval(processOrphans, 60000);
|
||||
|
||||
const tracker = new MarketTracker();
|
||||
let latestMarketState = null;
|
||||
let heartbeatTimer = null;
|
||||
|
||||
writeState(latestMarketState, paper, strategies);
|
||||
@@ -49,8 +68,7 @@ async function main() {
|
||||
latestMarketState = state || null;
|
||||
writeState(latestMarketState, paper, strategies);
|
||||
|
||||
if (!state) return;
|
||||
if (paper._resetting) return;
|
||||
if (!state || paper._resetting) return;
|
||||
|
||||
for (const strategy of strategies) {
|
||||
if (!strategy.enabled) continue;
|
||||
@@ -69,28 +87,29 @@ async function main() {
|
||||
});
|
||||
|
||||
tracker.on('settled', async ({ ticker, result }) => {
|
||||
console.log(`[Worker] Market ${ticker} settled: ${result}`);
|
||||
console.log(`[Worker] Market ${ticker} rotated/closed. Result: ${result || 'pending'}`);
|
||||
|
||||
if (paper._resetting) {
|
||||
console.log(`[Worker] Skipping settlement for ${ticker} — reset in progress`);
|
||||
return;
|
||||
}
|
||||
if (paper._resetting) return;
|
||||
|
||||
// Only attempt to settle and notify if the result is already available
|
||||
if (result) {
|
||||
const settledPositions = await paper.settle(ticker, result);
|
||||
|
||||
if (settledPositions) {
|
||||
for (const strategy of strategies) {
|
||||
if (!settledPositions) continue;
|
||||
for (const trade of settledPositions) {
|
||||
strategy.onSettlement(trade.result, trade);
|
||||
}
|
||||
}
|
||||
|
||||
await notify(
|
||||
`Market ${ticker} settled: ${result?.toUpperCase() || 'unknown'}`,
|
||||
`Market ${ticker} settled: ${result.toUpperCase()}`,
|
||||
'Market Settled',
|
||||
'default',
|
||||
'chart_with_upwards_trend'
|
||||
);
|
||||
}
|
||||
} else {
|
||||
console.log(`[Worker] Result for ${ticker} pending. Background poller will handle it when Kalshi publishes the outcome.`);
|
||||
}
|
||||
|
||||
writeState(latestMarketState, paper, strategies);
|
||||
});
|
||||
@@ -102,7 +121,6 @@ async function main() {
|
||||
writeState(latestMarketState, paper, strategies);
|
||||
}, HEARTBEAT_MS);
|
||||
|
||||
// Check for reset flag periodically
|
||||
setInterval(async () => {
|
||||
try {
|
||||
if (fs.existsSync('/tmp/kalbot-reset-flag')) {
|
||||
|
||||
Reference in New Issue
Block a user