From 3c5810234f4ccd984fb5f114d1a159b6986b3f01 Mon Sep 17 00:00:00 2001 From: ttmx Date: Wed, 17 Jul 2024 09:05:00 +0000 Subject: [PATCH] Add helper scripts and SINGLE_RUN params for those scripts. --- .vscode/settings.json | 3 +- gen-all.sh | 75 ++++++++++++++++++++++++++++++++++++++ printer/checkdoublepage.sh | 2 + printer/src/index.ts | 7 ++++ queue-manager/src/index.ts | 3 +- 5 files changed, 88 insertions(+), 2 deletions(-) create mode 100755 gen-all.sh create mode 100755 printer/checkdoublepage.sh diff --git a/.vscode/settings.json b/.vscode/settings.json index af9bf73..60e56bb 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -35,5 +35,6 @@ }, "[jsonc]": { "editor.defaultFormatter": "dbaeumer.vscode-eslint" - } + }, + "eslint.useFlatConfig": false } \ No newline at end of file diff --git a/gen-all.sh b/gen-all.sh new file mode 100755 index 0000000..4b7075d --- /dev/null +++ b/gen-all.sh @@ -0,0 +1,75 @@ +#!/bin/bash + + +cleanup() { + echo "Cleaning up..." + kill $API_PID + kill $STANDALONE_PID + kill -9 $MAIN_API_PID + kill $QUEUE_MANAGER_PID + kill $PRINTER_PID1 + kill $PRINTER_PID2 + exit 1 +} + +# Trap SIGINT signal (Ctrl+C) +trap cleanup SIGINT + +# Start the API server and save its PID +( + cd ../api/parse-network/ && + set -a && + source .env && + set +a && + SINGLE_RUN=true GTFS_URL=$1 npm run start +) && +API_PID=$! + +# Build the renderer +( + cd ./renderer/ && npm run build && cp .next/static .next/standalone/.next/static -r +) && +wait + +# Start the standalone server and save its PID +( + cd ./renderer/ && API_URL=http://localhost:5050 node .next/standalone/server.js +) & +STANDALONE_PID=$! + +# Start the main API server and save its PID +( + cd ../api/server/ && npm run start > /dev/null +) & +MAIN_API_PID=$! + +# Start the queue manager and save its PID +( + cd ./queue-manager/ && sleep 5 && SINGLE_RUN=true npm run start +) & +QUEUE_MANAGER_PID=$! + +rm -rf ./printer/pdfs/* +# Start the printer and save its PID +( + cd ./printer/ && sleep 10 && SINGLE_RUN=true npm run start +) & +PRINTER_PID1=$! +( + cd ./printer/ && sleep 10 && SINGLE_RUN=true npm run start +) & +PRINTER_PID2=$! + +# Wait for the queue manager and both printer processes to finish +wait $QUEUE_MANAGER_PID +wait $PRINTER_PID1 +wait $PRINTER_PID2 + +# Kill the API server, standalone server, and main API server +kill $API_PID +kill $STANDALONE_PID +kill $MAIN_API_PID + +(cd ./printer/pdfs && zip ../../$(date +"%Y-%m-%d")$(basename $1) *.pdf) + + diff --git a/printer/checkdoublepage.sh b/printer/checkdoublepage.sh new file mode 100755 index 0000000..dadc825 --- /dev/null +++ b/printer/checkdoublepage.sh @@ -0,0 +1,2 @@ +#!/bin/sh +grep -c "<= 2 { print $1 }' |xargs -n1 basename|tee ./twopages.txt diff --git a/printer/src/index.ts b/printer/src/index.ts index 91fef15..c980856 100644 --- a/printer/src/index.ts +++ b/printer/src/index.ts @@ -7,6 +7,9 @@ const RENDER_URL = process.env.RENDER_URL || 'http://localhost:3000/schedule'; const PARALLEL = parseInt(process.env.TABS) || 12; const CACHE_SIZE = PARALLEL * 2; const REFRESH_AFTER = parseInt(process.env.REFRESH_AFTER) || 100; + +const SINGLE_RUN = process.env.SINGLE_RUN == 'true' || false; + console.log(`QUEUE_URL: ${QUEUE_URL}`); console.log(`RENDER_URL: ${RENDER_URL}`); console.log(`PARALLEL: ${PARALLEL}`); @@ -103,6 +106,10 @@ async function replenishQueue() { let maybeItem: { finished: boolean, item: string | null } = await response.json(); if (maybeItem.finished) { + if (SINGLE_RUN) { + console.log('Finished processing all items'); + process.exit(0); + } await new Promise(resolve => setTimeout(resolve, 5000)); } else if (maybeItem.item) { queue.push(maybeItem.item); diff --git a/queue-manager/src/index.ts b/queue-manager/src/index.ts index d929756..54fe007 100644 --- a/queue-manager/src/index.ts +++ b/queue-manager/src/index.ts @@ -1,11 +1,11 @@ import 'dotenv/config'; import process from 'process'; import Fastify from 'fastify'; -import { start } from 'repl'; const fastify = Fastify({ logger: false }); const API_URL = process.env.API_URL || 'http://localhost:5050'; const LOG_EVERY = parseInt(process.env.LOG_EVERY) || 100; +const SINGLE_RUN = process.env.SINGLE_RUN == 'true' || false; let updatedAt:string|null = null; let queue = []; @@ -31,6 +31,7 @@ async function main() { startTime = currentTime; } if (queue.length === 0) { + if (SINGLE_RUN && updatedAt != null) setTimeout(() => { process.exit(0); }, 1000); return { finished: true, item: null }; } i++;