Skip to content

Commit

Permalink
Put debug logs regarding worker processes managed by the main program…
Browse files Browse the repository at this point in the history
… behind a flag;
  • Loading branch information
jairajdev committed Sep 18, 2024
1 parent 5aa213c commit b79d692
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export interface Config {
apiServerPort: number
txCronSchedule: string
}
workerProcessesDebugLog: boolean // To enable debug logs for worker processes managed by the main process
}

let config: Config = {
Expand Down Expand Up @@ -182,6 +183,7 @@ let config: Config = {
apiServerPort: 8084,
txCronSchedule: '*/5 * * * *',
},
workerProcessesDebugLog: false,
}
// Override default config params from config file, env vars, and cli args
export async function overrideDefaultConfig(file: string): Promise<void> {
Expand Down
37 changes: 22 additions & 15 deletions src/primary-process/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export const setupWorkerProcesses = (cluster: Cluster): void => {
worker.kill()
}
if (receiptLoadTraker < config.receiptLoadTrakerLimit) {
console.log(`Receipt load is below the limit: ${receiptLoadTraker}/${config.receiptLoadTrakerLimit}`)
if (config.workerProcessesDebugLog)
console.log(`Receipt load is below the limit: ${receiptLoadTraker}/${config.receiptLoadTrakerLimit}`)

Check warning

Code scanning / CodeQL

Log injection Medium

Log entry depends on a
user-provided value
.
// Kill the extra workers from the end of the array
for (let i = workers.length - 1; i >= 0; i--) {
// console.log(`Killing worker ${workers[i].process.pid} with index ${i}`);
Expand All @@ -62,10 +63,12 @@ export const setupWorkerProcesses = (cluster: Cluster): void => {
let neededWorkers = Math.ceil(receiptLoadTraker / config.receiptLoadTrakerLimit)
if (neededWorkers > MAX_WORKERS) neededWorkers = MAX_WORKERS
let currentWorkers = workers.length
console.log(`Needed workers: ${neededWorkers}`, `Current workers: ${currentWorkers}`)
if (config.workerProcessesDebugLog)
console.log(`Needed workers: ${neededWorkers}`, `Current workers: ${currentWorkers}`)
if (neededWorkers > currentWorkers) {
if (extraWorkers.size > 0) {
console.log(`Extra workers available: ${extraWorkers.size}, moving them to workers list`)
if (config.workerProcessesDebugLog)
console.log(`Extra workers available: ${extraWorkers.size}, moving them to workers list`)
// Move the extra workers to the workers list
for (const [pid, worker] of extraWorkers) {
workers.push(worker)
Expand All @@ -92,11 +95,12 @@ export const setupWorkerProcesses = (cluster: Cluster): void => {
}
}
}
console.log(
`Adjusted worker count to ${
workers.length + newWorkers.size
}, based on ${receiptLoadTraker} receipts received.`
)
if (config.workerProcessesDebugLog)
console.log(
`Adjusted worker count to ${
workers.length + newWorkers.size
}, based on ${receiptLoadTraker} receipts received.`
)
receiptLoadTraker = 0 // Reset the count
}, config.receiptLoadTrakerInterval)
}
Expand Down Expand Up @@ -124,10 +128,11 @@ const setupWorkerListeners = (worker: Worker): void => {
break
}
case 'child_close':
console.log(`Worker ${workerId} is requesting to close`)
if (config.workerProcessesDebugLog) console.log(`Worker ${workerId} is requesting to close`)
// Check if the worker is in the extraWorkers map
if (extraWorkers.has(workerId)) {
console.log(`Worker ${workerId} is in extraWorkers, killing it now`)
if (config.workerProcessesDebugLog)
console.log(`Worker ${workerId} is in extraWorkers, killing it now`)
const worker = extraWorkers.get(workerId)
if (worker) worker.kill()
} else {
Expand All @@ -144,7 +149,7 @@ const setupWorkerListeners = (worker: Worker): void => {
}
break
case 'child_ready':
console.log(`Worker ${workerId} is ready for the duty`)
if (config.workerProcessesDebugLog) console.log(`Worker ${workerId} is ready for the duty`)
// Check if the worker is in the newWorkers map
if (newWorkers.has(workerId)) {
console.log(`Worker ${workerId} is in newWorkers, moving it to the workers list`)
Expand Down Expand Up @@ -172,7 +177,8 @@ const setupWorkerListeners = (worker: Worker): void => {
console.log(`Worker ${worker.process.pid} died with code ${code} and signal ${signal}`)
let isExtraWorker = false
if (extraWorkers.has(workerId)) {
console.log(`Worker ${workerId} is in extraWorkers, removing it now`)
if (config.workerProcessesDebugLog)
console.log(`Worker ${workerId} is in extraWorkers, removing it now`)
isExtraWorker = true
extraWorkers.get(workerId)?.kill()
extraWorkers.delete(workerId)
Expand Down Expand Up @@ -246,8 +252,8 @@ export const offloadReceipt = async (
}
if (workers.length === 0) {
mainProcessReceiptTracker++
console.log('Verifying on the main program 1', txId, timestamp)
verificationResult = await verifyArchiverReceipt(receipt, requiredSignatures)
if (config.workerProcessesDebugLog) console.log('Verifying on the main program 1', txId, timestamp)
verificationResult = await verifyArchiverReceipt(receipt, requiredSignatures)
mainProcessReceiptTracker--
} else {
mainProcessReceiptTracker = 0
Expand Down Expand Up @@ -276,7 +282,8 @@ export const offloadReceipt = async (
verificationResult = await verifyArchiverReceipt(receipt, requiredSignatures)
}
} else {
console.log('Verifying on the worker process 1', txId, timestamp, worker.process.pid)
if (config.workerProcessesDebugLog)
console.log('Verifying on the worker process 1', txId, timestamp, worker.process.pid)
const cloneReceipt = Utils.deepCopy(receipt)
delete cloneReceipt.tx.originalTxData
delete cloneReceipt.executionShardKey
Expand Down

0 comments on commit b79d692

Please sign in to comment.