GithubHelp home page GithubHelp logo

Comments (5)

kwiatkk1 avatar kwiatkk1 commented on July 16, 2024 1

I think that I came up with the slightly better idea of solving this issue:

  • we can take an advantage of that environment variables set in a master process are visible to its child processes (see: https://nodejs.org/api/process.html#process_process_env)
  • so instead of hitting the filesystem and creating tmp files, we can simply use env to pass config data

So the code should look like this:

  • in a master process, before spawning workers, create an env variable:
const WorkerNodes = require('worker-nodes');

async function run() {

    // setup env variable
    process.env.WORKER_OPTIONS = JSON.stringify({
        spam: 'eggs',
        ham: 'cheese',
        cacheSize: 100
    });

    // start processes...
    const workers = new WorkerNodes(require.resolve('./worker-source.js'), {
        autoStart: true,
        lazyStart: false,
        minWorkers: 4,
        maxWorkers: 4,
        maxTasksPerWorker: 1
    });

    // ...and wait for all the workers to be ready before proceeding
    await workers.ready();
}

run().then(() => console.log('ready'));
  • in a worker's code (worker-source.js) read the env variable:
// receive config
const workerCustomOptions = JSON.parse(process.env.WORKER_OPTIONS);

console.log(`worker ${process.pid} up! custom options: ${JSON.stringify(workerCustomOptions)}`);

from node-worker-nodes.

csilvers avatar csilvers commented on July 16, 2024 1

Doh! -- a great approach. This is, like, what the environment is for...

from node-worker-nodes.

kwiatkk1 avatar kwiatkk1 commented on July 16, 2024

Hi! Unfortunately there is no elegant solution for now to do this.

I think that it would be an useful option to pass some data on start bas you suggested. But for now, as workers don't receive any custom options during initialization phase, we can take advantage of how workers loads (they simply report ready if the source is loaded), and make some workaround:

  • in a master process, before spawning workers, create a config file:
const WorkerNodes = require('worker-nodes');
const fs = require('fs');
const os = require('os');
const path = require('path');
const util = require('util');
const writeFileAsync = util.promisify(fs.writeFile);

async function run() {
    const tmpFilePath = path.join(os.tmpdir(), 'worker-startup-params.json');
    const workerInitOptions = {
        foo: 'bar',
        cacheSize: 100
    };

    // save worker config (hopefully using fast tmpfs access)
    await writeFileAsync(tmpFilePath, JSON.stringify(workerInitOptions));

    // start
    const workers = new WorkerNodes(require.resolve('./worker-source.js'), {
        autoStart: true,
        lazyStart: false,
        minWorkers: 4,
        maxWorkers: 4,
        maxTasksPerWorker: 1
    });

    // wait for all workers to be ready
    await workers.ready();
}

run().then(() => console.log('ready'));
  • in a worker's code (worker-source.js) read the config:
const os = require('os');
const path = require('path');

const workerCustomOptions = require(path.join(os.tmpdir(), 'worker-startup-params.json'));

console.log(`worker ${process.pid} up! custom options: ${JSON.stringify(workerCustomOptions)}`);

This should produce output similar to the following:

> node index.js
worker 57621 up! custom options: {"foo":"bar","cacheSize":100}
worker 57622 up! custom options: {"foo":"bar","cacheSize":100}
worker 57624 up! custom options: {"foo":"bar","cacheSize":100}
worker 57623 up! custom options: {"foo":"bar","cacheSize":100}
ready

I hope it helps in your use case.

But if passing a config via tmp file is not an acceptable solution, please wait for a final one (a new option like initData):

const workers = new WorkerNodes(require.resolve('./worker-source.js'), {
    [...]
    initData: {
        cacheSize: 100
    }
});

from node-worker-nodes.

csilvers avatar csilvers commented on July 16, 2024

Thanks for the thoughtful reply! I ended up making a temporary file, just like you did, but I did it for the entire entrypoint, not just for the config reading, which ended up being simpler. My code is like this:

    const template = `${__dirname}/render_cache_${cacheSize}_XXXXXX.js`;
    const tmpobj = tmp.fileSync({template: template, discardDescriptor: true});
    const filename = tmpobj.name;
    const contents = `
const cache = require("./cache.js");
cache.init(${cacheSize});
module.exports = require("./render.js");
`;
    fs.writeFileSync(filename, contents);

    return new workerNodes(filename, options);

from node-worker-nodes.

spencermountain avatar spencermountain commented on July 16, 2024

thanks craig and Krzysztof, if anyone is still reading this, I was able to pass a function into the worker using jsonfn.

This way you can execute arbitrary stuff, whenever you'd like.
cheers

from node-worker-nodes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.