Skip to content

Commit

Permalink
Add Dockerfile and replace stdout writes with console.log
Browse files Browse the repository at this point in the history
  • Loading branch information
hsoerensen committed Jul 11, 2024
1 parent 1dadd35 commit a044289
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 13 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/docker-publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Publish Docker image

on:
release:
types: [published]
workflow_dispatch:

jobs:
push_to_registries:
name: Push Docker image to registry
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to the Container registry
uses: docker/#-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: |
ghcr.io/${{ github.repository }}
tags: |
type=semver,pattern={{version}}
- name: Build and push Docker images
uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671
with:
context: .
platforms: linux/amd64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM node:latest

# Create app directory
WORKDIR /app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY scripts ./scripts
COPY include ./include

CMD [ "node", "/app/scripts/block_follower.js", "-f", "/db/proposers.db" ]
28 changes: 15 additions & 13 deletions scripts/block_follower.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,30 @@ async function getHighestStoredBlock() {
let last_block = highestStoredBlock;
while(true) {
if (last_block >= end_block) {
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(`Reached end of chain, sleeping for 10 seconds...`);
console.log(`Reached end of chain, sleeping for 10 seconds...`);
await sleep(10000);
try {
end_block = (await algod.status().do())['last-round'];
}
catch (error) {
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(`Error retrieving end block from API: ${error.message}, retrying.`);
console.log(`Error retrieving end block from API: ${error.message}, retrying.`);
await sleep(10000); // wait 10 seconds before trying again
}
continue;
}
let i = last_block + 1;

process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(`Retrieving block ${i} (${end_block - i} behind)`);
let logInterval = 3;
if ((end_block - i) >= 1000) {
logInterval = 1000;
} else if ((end_block - i) < 100 && (end_block - i) >= 10) {
logInterval = 10;
}

if ((end_block - i) % logInterval === 0 || (end_block - i) < logInterval) {
const toBlock = i + logInterval > end_block ? end_block : i + logInterval - 1;
console.log(`Retrieving block ${i} to ${toBlock} (${end_block - i} behind)`);
}

try {
const timeoutPromise = new Promise((resolve, reject) => {
Expand All @@ -99,12 +103,10 @@ async function getHighestStoredBlock() {
// store this block and its proposer in the database
await storeBlockInDb(i, addr, timestamp);
} catch (error) {
process.stdout.clearLine();
process.stdout.cursorTo(0);
if (error.message === 'Request timed out') {
process.stdout.write(`Error retrieving block ${i} from API: request timed out, retrying.`);
console.log(`Error retrieving block ${i} from API: request timed out, retrying.`);
} else {
process.stdout.write(`Error retrieving block ${i} from API: ${error.message}, retrying.`);
console.log(`Error retrieving block ${i} from API: ${error.message}, retrying.`);
}
await sleep(10000); // wait 10 seconds before trying again
continue;
Expand Down

0 comments on commit a044289

Please # to comment.