Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bree Workers Won't Follow closeWorkerAfterMs rule #227

Open
mhofwell opened this issue Oct 3, 2023 · 0 comments
Open

Bree Workers Won't Follow closeWorkerAfterMs rule #227

mhofwell opened this issue Oct 3, 2023 · 0 comments

Comments

@mhofwell
Copy link

mhofwell commented Oct 3, 2023

Hello,

I am using Bree to execute Cron 1 which ingests data from an API to my DB. Cron 2 then executes and uses DB data to spawn several workers operating Cron 3 with different data for each worker. Cron 3 runs on an infinite loop for each worker, even though it has a closeWorkerAfterMs, process.exit(0) command and the parentPort.postMessage('done').

Cron 2: "cronFactory.js"

`
import path from 'path';
import ms from 'ms';
import Bree from 'bree';
import { PrismaClient } from '@prisma/client';

const cronFactory = async () => {
const prisma = new PrismaClient();

const fixtures = await prisma.fixtures.findMany();
const appDir =
    '/Users/bigviking/Documents/GitHub/Projects/blackjack/server/';

let newCronJobs = [];

fixtures.forEach((fixture) => {
    newCronJobs.push({
        name: `gw-worker-${fixture.kickoff_time}`,
        path: path.join(appDir + '/jobs', 'updateGoals.js'),
        interval: '1s',
        closeWorkerAfterMs: ms('2s'),
        outputWorkerMetadata: true,
        worker: {
            workerData: {
                kickoff_time: fixture.kickoff_time,
                numberOfFixtures: fixture.number_of_fixtures,
            },
        },
    });
});

console.log(newCronJobs);

const cron = new Bree({
    root: false,
    jobs: newCronJobs,
    closeWorkerAfterMs: ms('2s'),
    errorHandler: (error, workerMetadata) => {
        // workerMetadata will be populated with extended worker information only if
        // Bree instance is initialized with parameter `workerMetadata: true`
        if (workerMetadata.threadId) {
            console.log(
                `There was an error while running a worker ${workerMetadata.name} with thread ID: ${workerMetadata.threadId}`
            );
        } else {
            console.log(
                `There was an error while running a worker ${workerMetadata.name}`
            );
        }
        console.error(error);
        errorService.captureException(error);
    },
});
await cron.start();

};

cronFactory();
`

Cron 3: goalUpdate.js

`import { parentPort, workerData } from 'worker_threads';

const goalUpdate = async () => {
if (parentPort) parentPort.postMessage('done');
else process.exit(0);
};

goalUpdate();`

The console output runs infinitely, threadID's keep climbing. Help?
Screenshot 2023-10-03 at 2 49 27 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant