53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Trust Score Worker
|
|
*
|
|
* Calculates and updates trust scores for all mints.
|
|
*/
|
|
|
|
import { initDatabase } from '../db/connection.js';
|
|
import { getMintsNeedingTrustScore, calculateTrustScore } from '../services/TrustService.js';
|
|
|
|
const POLL_INTERVAL = 600000; // 10 minutes
|
|
|
|
async function runTrustWorker() {
|
|
console.log('[TrustWorker] Starting...');
|
|
initDatabase();
|
|
|
|
while (true) {
|
|
try {
|
|
const mintsForScore = getMintsNeedingTrustScore();
|
|
|
|
if (mintsForScore.length > 0) {
|
|
console.log(`[TrustWorker] Calculating trust scores for ${mintsForScore.length} mints`);
|
|
|
|
for (const { mint_id } of mintsForScore) {
|
|
try {
|
|
const score = calculateTrustScore(mint_id);
|
|
console.log(`[TrustWorker] ${mint_id}: ${score.score_total} (${score.score_level})`);
|
|
} catch (error) {
|
|
console.error(`[TrustWorker] Error calculating score for ${mint_id}:`, error.message);
|
|
}
|
|
}
|
|
}
|
|
|
|
await sleep(POLL_INTERVAL);
|
|
} catch (error) {
|
|
console.error('[TrustWorker] Error:', error);
|
|
await sleep(POLL_INTERVAL);
|
|
}
|
|
}
|
|
}
|
|
|
|
function sleep(ms) {
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
}
|
|
|
|
// Run if called directly
|
|
if (process.argv[1].includes('trust.js')) {
|
|
runTrustWorker().catch(console.error);
|
|
}
|
|
|
|
export { runTrustWorker };
|
|
|