Skip to content

Commit

Permalink
chore(graphCron): adds logs
Browse files Browse the repository at this point in the history
  • Loading branch information
alber70g committed Jan 21, 2025
1 parent e49d538 commit 81f2c9e
Showing 1 changed file with 65 additions and 29 deletions.
94 changes: 65 additions & 29 deletions packages/apps/tools/src/scripts/graphCron/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ interface ICompletedBlockHeightsResponse {
};
}

function log<T extends unknown>(msg: T, prepend: string = ''): T {
prepend = prepend.length > 0 ? `${prepend}\n` : prepend;
if (typeof msg !== 'string') {
console.log('LOG:', prepend + JSON.stringify(msg, null, 2));
} else {
console.log('LOG:', prepend + msg);
}
return msg;
}

const countHeightOnGraph = async (
props: IEnvProps,
): Promise<{ totalCutHeight: number; lastBlockHeight: number }> => {
Expand All @@ -62,7 +72,8 @@ const countHeightOnGraph = async (
'sec-fetch-site': 'cross-site',
},
body: JSON.stringify({
query: `query graphBlockHeight {
query: log(
`query graphBlockHeight {
completedBlockHeights(heightCount: 1) {
edges {
node {
Expand All @@ -71,6 +82,8 @@ const countHeightOnGraph = async (
}
}
}`,
`Querying Graph ${props.env}:`,
),
variables: {},
extensions: {},
}),
Expand All @@ -79,18 +92,21 @@ const countHeightOnGraph = async (
const completedBlockHeightsResult =
(await result.json()) as ICompletedBlockHeightsResponse;

return {
totalCutHeight:
completedBlockHeightsResult.data.completedBlockHeights.edges.reduce(
(acc, { node: { height } }) => acc + height,
0,
),
lastBlockHeight:
completedBlockHeightsResult.data.completedBlockHeights.edges.reduce(
(maxHeight, { node: { height } }) => Math.max(maxHeight, height),
0,
),
};
return log(
{
totalCutHeight:
completedBlockHeightsResult.data.completedBlockHeights.edges.reduce(
(acc, { node: { height } }) => acc + height,
0,
),
lastBlockHeight:
completedBlockHeightsResult.data.completedBlockHeights.edges.reduce(
(maxHeight, { node: { height } }) => Math.max(maxHeight, height),
0,
),
},
`Graph response ${props.env}:`,
);
};

export const runJobPerEnvironment = async (props: IEnvProps) => {
Expand All @@ -105,41 +121,61 @@ export const runJobPerEnvironment = async (props: IEnvProps) => {
} = await countHeightOnGraph(props);

if (Number.isNaN(totalHeightOnChainWeb)) {
await sendErrorMessage({
title: `${props.env} chainweb.com fail`,
msg: `We were unable to retrieve the blockheights from chainweb. \n There seems to be an issue with ChainWeb (${props.chainweb})`,
});
await sendErrorMessage(
log(
{
title: `${props.env} chainweb.com fail`,
msg: `We were unable to retrieve the blockheights from chainweb. \n There seems to be an issue with ChainWeb (${props.chainweb})`,
},
`Chainweb.com error ${props.env}:`,
),
);
return;
}
if (Number.isNaN(totalCutHeightGraph)) {
await sendErrorMessage({
title: `${props.env} graph fail`,
msg: `We were unable to retrieve the blockheights from the test graph. \n There seems to be an issue with test graph (${props.graphql})`,
});
await sendErrorMessage(
log(
{
title: `${props.env} graph fail`,
msg: `We were unable to retrieve the blockheights from the test graph. \n There seems to be an issue with test graph (${props.graphql})`,
},
`Graph error ${props.env}:`,
),
);
return;
}

if (
Math.abs(totalHeightOnChainWeb - totalCutHeightGraph) >
MAXBLOCKHEIGHT_DIFFERENCE
) {
await sendErrorMessage({
title: `${props.env} chainweb and graph blockheight difference`,
msg: `There is a large difference in the totalCutHeight between ${props.env} chainweb and graph.
await sendErrorMessage(
log(
{
title: `${props.env} chainweb and graph blockheight difference`,
msg: `There is a large difference in the totalCutHeight between ${props.env} chainweb and graph.
Total cut height difference: ${Math.abs(totalHeightOnChainWeb - totalCutHeightGraph)}
[Chainweb lastBlockHeight](${props.chainweb}): ${lastBlockHeightChainweb}
[GraphiQL lastBlockHeight](${props.graphqlRef}): ${lastBlockHeightGraph}
Total height difference: ${lastBlockHeightChainweb - lastBlockHeightGraph}
`,
});
},
`Blockheight difference ${props.env}:`,
),
);
}
} catch (e) {
await sendErrorMessage({
title: `There was a general issue with the ${props.env} graph cron job`,
msg: e,
});
await sendErrorMessage(
log(
{
title: `There was a general issue with the ${props.env} graph cron job`,
msg: JSON.stringify(e),
},
`General error ${props.env}:`,
),
);
}
};

Expand Down

0 comments on commit 81f2c9e

Please sign in to comment.