From a044289f3dd49f3184455132454b140788a77f33 Mon Sep 17 00:00:00 2001 From: Henrik Soerensen Date: Thu, 11 Jul 2024 14:23:33 -0400 Subject: [PATCH] Add Dockerfile and replace stdout writes with console.log --- .github/workflows/docker-publish.yaml | 48 +++++++++++++++++++++++++++ Dockerfile | 19 +++++++++++ scripts/block_follower.js | 28 ++++++++-------- 3 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/docker-publish.yaml create mode 100644 Dockerfile diff --git a/.github/workflows/docker-publish.yaml b/.github/workflows/docker-publish.yaml new file mode 100644 index 0000000..c64945a --- /dev/null +++ b/.github/workflows/docker-publish.yaml @@ -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/login-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 }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fa52a72 --- /dev/null +++ b/Dockerfile @@ -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" ] \ No newline at end of file diff --git a/scripts/block_follower.js b/scripts/block_follower.js index 6790d85..e57ffbf 100644 --- a/scripts/block_follower.js +++ b/scripts/block_follower.js @@ -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) => { @@ -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;