Skip to content

Commit

Permalink
refactor: Use workerData to initialise Worker
Browse files Browse the repository at this point in the history
  • Loading branch information
morgsmccauley committed Sep 15, 2023
1 parent 843a402 commit f621411
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
5 changes: 3 additions & 2 deletions runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ void (async function main () {
return;
}

const worker = new Worker('./dist/worker.js');
worker.postMessage({ streamKey });
const worker = new Worker('./dist/worker.js', {
workerData: { streamKey },
});

worker.on('message', (message: Metric) => {
METRICS[message.type].labels(message.labels).set(message.value);
Expand Down
14 changes: 9 additions & 5 deletions runner/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { parentPort } from 'worker_threads';
import { parentPort, workerData } from 'worker_threads';

import Indexer from './indexer';
import RedisClient from './redis-client';

const indexer = new Indexer('mainnet');
const redisClient = new RedisClient();

// eslint-disable-next-line
parentPort?.on('message', async ({ streamKey }) => {
if (parentPort === null) {
throw new Error('No parentPort');
}

void (async function main () {
const { streamKey } = workerData;

console.log('Started processing stream: ', streamKey);

let indexerName = '';

while (true) {
try {
const startTime = performance.now();
Expand Down Expand Up @@ -61,4 +65,4 @@ parentPort?.on('message', async ({ streamKey }) => {
console.log(`Failed: ${indexerName}`, err);
}
}
});
})();

0 comments on commit f621411

Please sign in to comment.