-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
watcher: add supervisor to monitor watchers
- Loading branch information
Showing
5 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { ChainName } from '@certusone/wormhole-sdk'; | ||
import { Worker } from 'worker_threads'; | ||
import { HB_INTERVAL, WorkerData } from '../consts'; | ||
import { getLogger } from '../utils/logger'; | ||
import { Environment, getEnvironment } from '@wormhole-foundation/wormhole-monitor-common'; | ||
|
||
interface WorkerInfo { | ||
worker: Worker; | ||
data: WorkerData; | ||
lastHB: number; | ||
} | ||
|
||
const workers: { [key: string]: WorkerInfo } = {}; | ||
const logger = getLogger('supervisor'); | ||
const network: Environment = getEnvironment(); | ||
|
||
function spawnWorker(data: WorkerData) { | ||
const workerName = `${data.chain}Worker`; | ||
logger.info(`Spawning worker ${workerName} on network ${network}...`); | ||
const worker = new Worker('./dist/src/workers/worker.js', { workerData: data }); | ||
|
||
worker.on('message', (message) => { | ||
if (message === 'heartbeat') { | ||
logger.debug(`Worker ${workerName} sent HB`); | ||
workers[workerName].lastHB = Date.now(); | ||
} | ||
}); | ||
|
||
worker.on('exit', (code) => { | ||
logger.warn(`Worker ${workerName} exited with code ${code}`); | ||
if (code !== 0) { | ||
logger.error(`Restarting worker ${workerName}...`); | ||
spawnWorker(data); | ||
} | ||
}); | ||
|
||
workers[workerName] = { worker, data, lastHB: Date.now() }; | ||
logger.debug('Finished spawning worker:', workerName); | ||
} | ||
|
||
function monitorWorkers() { | ||
setInterval(() => { | ||
for (const [workerName, workerInfo] of Object.entries(workers)) { | ||
logger.debug( | ||
`Checking worker ${workerName} with lastHB of ${new Date(workerInfo.lastHB)}...` | ||
); | ||
if (Date.now() - workerInfo.lastHB > HB_INTERVAL) { | ||
logger.error(`Worker ${workerName} missed HB, restarting...`); | ||
workerInfo.worker.terminate(); | ||
spawnWorker(workerInfo.data); | ||
} | ||
} | ||
}, HB_INTERVAL); | ||
} | ||
|
||
export function startSupervisor(supportedChains: ChainName[]) { | ||
supportedChains.forEach((chain) => { | ||
const workerData: WorkerData = { network, chain }; | ||
spawnWorker(workerData); | ||
}); | ||
|
||
monitorWorkers(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { initDb } from '../databases/utils'; | ||
import { makeFinalizedWatcher } from '../watchers/utils'; | ||
import { workerData } from 'worker_threads'; | ||
|
||
initDb(false); | ||
const network = workerData.network; | ||
const chain = workerData.chain; | ||
console.log(`Making watcher for ${network} ${chain}...`); | ||
makeFinalizedWatcher(network, chain).watch(); | ||
console.log(`Watcher for ${network} ${chain} started.`); |