From 0e6b5f4fce3f0ed318321575f61bc0e23529d648 Mon Sep 17 00:00:00 2001 From: abernatskiy Date: Wed, 11 Sep 2024 05:50:51 +0900 Subject: [PATCH] Reconnect on failure modified: package.json modified: src/apply.ts --- package.json | 2 +- src/apply.ts | 30 +++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 16d2599..eb094bd 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/apply.ts b/src/apply.ts index 3e09ac0..f03985c 100644 --- a/src/apply.ts +++ b/src/apply.ts @@ -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 { + return await axios.post( hasuraApiUrl, { type: 'replace_metadata', @@ -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 } }) -}) +}