Always return cached value when thegraph errors

This commit is contained in:
Marco van Dijk 2022-03-21 15:14:38 +01:00
parent e0795f8bf2
commit d6bfdc2551

View File

@ -22,15 +22,15 @@ import { request, gql } from 'graphql-request';
let CoinMarketCap = require('coinmarketcap-api'); let CoinMarketCap = require('coinmarketcap-api');
let cmcClient = new CoinMarketCap(API_CMC); let cmcClient = new CoinMarketCap(API_CMC);
let cmcEnabled = false; let cmcEnabled = false;
if(!CONF_DISABLE_CMC){ if (!CONF_DISABLE_CMC) {
if(API_CMC == ""){ if (API_CMC == "") {
console.log("Please provide a CMC api key"); console.log("Please provide a CMC api key");
}else { } else {
CoinMarketCap = require('coinmarketcap-api'); CoinMarketCap = require('coinmarketcap-api');
cmcClient = new CoinMarketCap(API_CMC); cmcClient = new CoinMarketCap(API_CMC);
cmcEnabled = true; cmcEnabled = true;
} }
}else{ } else {
console.log("Running without CMC api"); console.log("Running without CMC api");
} }
// Gets blockchain data // Gets blockchain data
@ -373,10 +373,10 @@ const handleSync = async function () {
syncEvents(); syncEvents();
while (isEventSyncing || isTicketSyncing) { while (isEventSyncing || isTicketSyncing) {
await sleep(3000); await sleep(3000);
if (isEventSyncing){ if (isEventSyncing) {
console.log("Parsed " + lastBlockEvents + " out of " + latestBlockInChain + " blocks for Event sync"); console.log("Parsed " + lastBlockEvents + " out of " + latestBlockInChain + " blocks for Event sync");
} }
if (isTicketSyncing){ if (isTicketSyncing) {
console.log("Parsed " + lastBlockTickets + " out of " + latestBlockInChain + " blocks for Ticket sync"); console.log("Parsed " + lastBlockTickets + " out of " + latestBlockInChain + " blocks for Ticket sync");
} }
} }
@ -412,7 +412,7 @@ if (!isEventSyncing && !CONF_SIMPLE_MODE && !CONF_DISABLE_SYNC) {
// Splits of raw CMC object into coin quote data // Splits of raw CMC object into coin quote data
const parseCmc = async function () { const parseCmc = async function () {
try { try {
if (!cmcEnabled){ if (!cmcEnabled) {
return; return;
} }
cmcCache = await cmcClient.getTickers({ limit: 200 }); cmcCache = await cmcClient.getTickers({ limit: 200 });
@ -585,6 +585,7 @@ apiRouter.get("/getTickets", async (req, res) => {
// Gets info on a given Orchestrator // Gets info on a given Orchestrator
const parseOrchestrator = async function (reqAddr) { const parseOrchestrator = async function (reqAddr) {
try {
reqAddr = reqAddr.toLowerCase(); reqAddr = reqAddr.toLowerCase();
const now = new Date().getTime(); const now = new Date().getTime();
// Default assume it's the first time we request this Orchestrator // Default assume it's the first time we request this Orchestrator
@ -668,6 +669,19 @@ const parseOrchestrator = async function (reqAddr) {
} }
} }
return orchestratorObj; return orchestratorObj;
} catch (err) {
if (wasCached) {
console.log("Thegraph is probably acting up. Returning cached value...");
for (var idx = 0; idx < orchestratorCache.length; idx++) {
if (orchestratorCache[idx].id == reqAddr) {
return orchestratorCache[idx];
}
}
} else{
console.log("Thegraph is probably acting up, but there is no cached value. Returning null...");
return {};
}
}
} }
// Exports info on a given Orchestrator // Exports info on a given Orchestrator
@ -678,7 +692,7 @@ apiRouter.get("/getOrchestrator", async (req, res) => {
reqOrch = CONF_DEFAULT_ORCH; reqOrch = CONF_DEFAULT_ORCH;
} }
const reqObj = await parseOrchestrator(reqOrch); const reqObj = await parseOrchestrator(reqOrch);
res.send(JSON.stringify(reqObj)); res.send(reqObj);
} catch (err) { } catch (err) {
console.log(err); console.log(err);
res.status(400).send(err); res.status(400).send(err);
@ -687,7 +701,7 @@ apiRouter.get("/getOrchestrator", async (req, res) => {
apiRouter.get("/getOrchestrator/:orch", async (req, res) => { apiRouter.get("/getOrchestrator/:orch", async (req, res) => {
try { try {
const reqObj = await parseOrchestrator(req.params.orch); const reqObj = await parseOrchestrator(req.params.orch);
res.send(JSON.stringify(reqObj)); res.send(reqObj);
} catch (err) { } catch (err) {
console.log(err); console.log(err);
res.status(400).send(err); res.status(400).send(err);
@ -696,7 +710,7 @@ apiRouter.get("/getOrchestrator/:orch", async (req, res) => {
apiRouter.post("/getOrchestrator", async (req, res) => { apiRouter.post("/getOrchestrator", async (req, res) => {
try { try {
const reqObj = await parseOrchestrator(req.body.orchAddr); const reqObj = await parseOrchestrator(req.body.orchAddr);
res.send(JSON.stringify(reqObj)); res.send(reqObj);
} catch (err) { } catch (err) {
console.log(err); console.log(err);
res.status(400).send(err); res.status(400).send(err);
@ -966,7 +980,7 @@ const getEnsDomain = async function (addr) {
for (const thisAddr of ensDomainCache) { for (const thisAddr of ensDomainCache) {
if (thisAddr.address === addr) { if (thisAddr.address === addr) {
// Check timeout // Check timeout
if (now - thisAddr.timestamp < CONF_TIMEOUT_ENS_DOMAIN ){ if (now - thisAddr.timestamp < CONF_TIMEOUT_ENS_DOMAIN) {
return thisAddr.domain; return thisAddr.domain;
} }
wasInCache = true; wasInCache = true;
@ -975,7 +989,7 @@ const getEnsDomain = async function (addr) {
// Else get it and cache it // Else get it and cache it
const ensDomain = await provider.lookupAddress(addr.toLowerCase()); const ensDomain = await provider.lookupAddress(addr.toLowerCase());
let ensObj; let ensObj;
if (!ensDomain){ if (!ensDomain) {
ensObj = { ensObj = {
domain: null, domain: null,
address: addr, address: addr,
@ -988,7 +1002,7 @@ const getEnsDomain = async function (addr) {
timestamp: now timestamp: now
}; };
} }
if (wasInCache){ if (wasInCache) {
for (var idx = 0; idx < ensDomainCache.length; idx++) { for (var idx = 0; idx < ensDomainCache.length; idx++) {
if (ensDomainCache[idx].address == addr) { if (ensDomainCache[idx].address == addr) {
console.log("Updating outdated domain " + ensObj.domain + " owned by " + ensObj.address + " @ " + ensObj.timestamp); console.log("Updating outdated domain " + ensObj.domain + " owned by " + ensObj.address + " @ " + ensObj.timestamp);
@ -1010,7 +1024,7 @@ const getEnsInfo = async function (addr) {
for (const thisAddr of ensInfoCache) { for (const thisAddr of ensInfoCache) {
if (thisAddr.domain === addr) { if (thisAddr.domain === addr) {
// Check timeout // Check timeout
if (now - thisAddr.timestamp < CONF_TIMEOUT_ENS_INFO ){ if (now - thisAddr.timestamp < CONF_TIMEOUT_ENS_INFO) {
return thisAddr; return thisAddr;
} }
wasInCache = true; wasInCache = true;
@ -1028,7 +1042,7 @@ const getEnsInfo = async function (addr) {
avatar, avatar,
timestamp: now timestamp: now
}; };
if (wasInCache){ if (wasInCache) {
for (var idx = 0; idx < ensInfoCache.length; idx++) { for (var idx = 0; idx < ensInfoCache.length; idx++) {
if (ensInfoCache[idx].domain == addr) { if (ensInfoCache[idx].domain == addr) {
console.log("Updating outdated info " + ensObj.domain + " @ " + ensObj.timestamp); console.log("Updating outdated info " + ensObj.domain + " @ " + ensObj.timestamp);
@ -1048,8 +1062,8 @@ apiRouter.get("/getENS/:orch", async (req, res) => {
try { try {
// First resolve addr => domain name // First resolve addr => domain name
const ensDomain = await getEnsDomain(req.params.orch); const ensDomain = await getEnsDomain(req.params.orch);
if (!ensDomain){ if (!ensDomain) {
res.send({domain: null}); res.send({ domain: null });
return; return;
} }
// Then resolve address to info // Then resolve address to info