Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
Apply 4 whitespace tabs and remove buildx/qemu from build workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
kgapos committed Oct 27, 2023
1 parent 20a7541 commit 5432aa7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 74 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
Expand All @@ -41,10 +41,6 @@ jobs:
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up QEMU dependency
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build docker image
run: ./scripts/build.sh
- name: Run docker image
Expand Down
14 changes: 7 additions & 7 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
set -e

retry_command() {
local max_attempts=5
local delay=3
local attempt=0
local max_attempts=5
local delay=3
local attempt=0

until [ $attempt -ge $max_attempts ]; do
until [ $attempt -ge $max_attempts ]; do
"$@"
local exit_code=$?
if [ $exit_code -eq 0 ]; then
return 0
return 0
fi

attempt=$((attempt + 1))
echo "Command failed (Attempt $attempt/$max_attempts). Retrying in $delay seconds..." >&2
sleep "$delay"
done
done

echo "Command failed $max_attempts times. Giving up." >&2
echo "Command failed $max_attempts times. Giving up." >&2
}

response=$(retry_command curl -s http://localhost:11012/healthcheck)
Expand Down
124 changes: 62 additions & 62 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,94 +10,94 @@ const nodeHealthcheckToleranceInSeconds = process.env.NODE_HEALTHCHECK_TOLERANCE
const register = new prometheus.Registry();

const lastBlockTimestampGauge = new prometheus.Gauge({
name: 'node_last_block_timestamp',
help: 'Timestamp of the last block'
name: 'node_last_block_timestamp',
help: 'Timestamp of the last block'
});

const secondsSinceLastBlockGauge = new prometheus.Gauge({
name: 'node_seconds_since_last_block',
help: 'Seconds since the last block'
name: 'node_seconds_since_last_block',
help: 'Seconds since the last block'
});

const healthcheckStatusGauge = new prometheus.Gauge({
name: 'node_healthcheck_status',
help: 'Healthcheck status of the node'
name: 'node_healthcheck_status',
help: 'Healthcheck status of the node'
});

register.registerMetric(lastBlockTimestampGauge);
register.registerMetric(secondsSinceLastBlockGauge);
register.registerMetric(healthcheckStatusGauge);

function writeLog(message) {
console.log(`[${new Date().toISOString()}] ${message}`);
console.log(`[${new Date().toISOString()}] ${message}`);
}

async function requestLogger(req, res, next) {
writeLog(`Request - ${req.method}: ${req.url}`);
next();
writeLog(`Request - ${req.method}: ${req.url}`);
next();
}

async function responseLogger(req, res, next) {
writeLog(`Respond - ${res.statusCode}: ${JSON.stringify(req.node)}`)
next();
writeLog(`Respond - ${res.statusCode}: ${JSON.stringify(req.node)}`)
next();
}

async function healthChecker(req, res, next) {
try {
req.timestamp = await axios.get(nodeLastBlockUri)
.then(response => {
const lastBlockTimestamp = response.data.timestamp;
const secondsSinceLastBlock = Math.floor(Date.now() / 1000) - lastBlockTimestamp;
const isHealthy = Math.abs(secondsSinceLastBlock) < nodeHealthcheckToleranceInSeconds
req.node = {
lastBlockTimestamp: lastBlockTimestamp,
secondsSinceLastBlock: secondsSinceLastBlock,
isHealthy: isHealthy
};
lastBlockTimestampGauge.set(lastBlockTimestamp);
secondsSinceLastBlockGauge.set(secondsSinceLastBlock);
healthcheckStatusGauge.set(isHealthy ? 1 : 0);
});
next();
} catch (error) {
writeLog(`Error - ${error}`);
res.statusCode = 500;
res.end(`Error: ${error}`);
}
try {
req.timestamp = await axios.get(nodeLastBlockUri)
.then(response => {
const lastBlockTimestamp = response.data.timestamp;
const secondsSinceLastBlock = Math.floor(Date.now() / 1000) - lastBlockTimestamp;
const isHealthy = Math.abs(secondsSinceLastBlock) < nodeHealthcheckToleranceInSeconds
req.node = {
lastBlockTimestamp: lastBlockTimestamp,
secondsSinceLastBlock: secondsSinceLastBlock,
isHealthy: isHealthy
};
lastBlockTimestampGauge.set(lastBlockTimestamp);
secondsSinceLastBlockGauge.set(secondsSinceLastBlock);
healthcheckStatusGauge.set(isHealthy ? 1 : 0);
});
next();
} catch (error) {
writeLog(`Error - ${error}`);
res.statusCode = 500;
res.end(`Error: ${error}`);
}
}

async function writeMetrics(req, res) {
const metrics = [
`node_last_block_timestamp ${req.node.lastBlockTimestamp.toString()}`,
`node_seconds_since_last_block ${req.node.secondsSinceLastBlock.toString()}`,
`node_healthcheck_status ${req.node.isHealthy ? '1' : '0'}`
].join('\n');
const metrics = [
`node_last_block_timestamp ${req.node.lastBlockTimestamp.toString()}`,
`node_seconds_since_last_block ${req.node.secondsSinceLastBlock.toString()}`,
`node_healthcheck_status ${req.node.isHealthy ? '1' : '0'}`
].join('\n');

res.setHeader('Content-Type', register.contentType);
res.end(metrics);
res.setHeader('Content-Type', register.contentType);
res.end(metrics);
}

polka()
.use(requestLogger, healthChecker)
.get('/healthcheck', (req, res) => {
if (!req.node.isHealthy) {
res.statusCode = 500;
}
res.end(JSON.stringify(req.node));
})
.get('/metrics', async (req, res) => {
try {
await writeMetrics(req, res);
} catch (error) {
res.statusCode = 500;
res.end(`Error: ${error}`);
}
})
.use(responseLogger)
.listen(nodeHealthcheckPort, () => {
writeLog(`Environment configuration:`)
writeLog(` NODE_LAST_BLOCK_URI: ${nodeLastBlockUri}`);
writeLog(` NODE_HEALTHCHECK_PORT: ${nodeHealthcheckPort}`);
writeLog(` NODE_HEALTHCHECK_TOLERANCE_IN_SECONDS: ${nodeHealthcheckToleranceInSeconds}`);
writeLog(`Running - http://localhost:${nodeHealthcheckPort}`);
});
.use(requestLogger, healthChecker)
.get('/healthcheck', (req, res) => {
if (!req.node.isHealthy) {
res.statusCode = 500;
}
res.end(JSON.stringify(req.node));
})
.get('/metrics', async (req, res) => {
try {
await writeMetrics(req, res);
} catch (error) {
res.statusCode = 500;
res.end(`Error: ${error}`);
}
})
.use(responseLogger)
.listen(nodeHealthcheckPort, () => {
writeLog(`Environment configuration:`)
writeLog(` NODE_LAST_BLOCK_URI: ${nodeLastBlockUri}`);
writeLog(` NODE_HEALTHCHECK_PORT: ${nodeHealthcheckPort}`);
writeLog(` NODE_HEALTHCHECK_TOLERANCE_IN_SECONDS: ${nodeHealthcheckToleranceInSeconds}`);
writeLog(`Running - http://localhost:${nodeHealthcheckPort}`);
});

0 comments on commit 5432aa7

Please sign in to comment.