Skip to content

Commit

Permalink
Reconnect on failure
Browse files Browse the repository at this point in the history
	modified:   package.json
	modified:   src/apply.ts
  • Loading branch information
abernatskiy committed Sep 10, 2024
1 parent 1ae28f4 commit 0e6b5f4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@subsquid/hasura-configuration",
"description": "Hasura configuration tool for use in SQD indexers",
"version": "1.0.1",
"version": "1.0.2",
"license": "GPL-3.0-or-later",
"repository": {
"type": "git",
Expand Down
30 changes: 27 additions & 3 deletions src/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,35 @@ import {CONFIG_PATH, HASURA_GRAPHQL_ENDPOINT, getHasuraHttpHeaders} from "./comm
const HASURA_HTTP_HEADERS = getHasuraHttpHeaders()


const hasuraApiUrl: string = `${HASURA_GRAPHQL_ENDPOINT}/v1/metadata`


runProgram(async () => {
program.description(`Apply the configuration at ${CONFIG_PATH}`).parse()

await registerTsNodeIfRequired()

const config: any = JSON.parse(fs.readFileSync(CONFIG_PATH).toString())
const hasuraApiUrl: string = `${HASURA_GRAPHQL_ENDPOINT}/v1/metadata`

await axios.post(
let status: string | undefined
for (let timeout of [0, 1000, 3000]) {
await sleep(timeout)
status = await replaceHasuraMetadata(config)
if (status === 'OK') break
}

if (status !== 'OK') {
console.error('Failed to connect to Hasura after three attempts')
process.exit(1)
}
})

function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function replaceHasuraMetadata(config: any): Promise<string | undefined> {
return await axios.post(
hasuraApiUrl,
{
type: 'replace_metadata',
Expand All @@ -32,20 +52,24 @@ runProgram(async () => {
.then(res => {
if (res.status === 200) {
console.log('Hasura configuration replaced successfully')
return 'OK'
}
else {
console.error(`Got HTTP ${res.status}: ${res.data.error}`)
if (res.data.error === 'cannot continue due to inconsistent metadata') {
console.error('Hint: this can be caused by database schema not being up to date')
}
return `${res.status}`
}
})
.catch(e => {
if (e.code === 'ECONNREFUSED') {
console.error(`Could not connect to Hasura at ${hasuraApiUrl}`)
return 'ECONNREFUSED'
}
else {
console.error(`Unknown Axios error`, e)
return undefined
}
})
})
}

0 comments on commit 0e6b5f4

Please sign in to comment.