0

I need to pass command-line arguments or params or execargv which should come in process params while loading environment for a child process which will be launched by a bull for process a job.

is it possible? if yes is there any way to do it?

I can identify the child process is launched for bull usings args[1] which contains /bull/lib/process but I want to pass custom param to node process.

Karma
  • 1
  • 1
  • 3
  • 9
Stephen
  • 834
  • 7
  • 13
  • Do you mean this from node.js documentation? https://nodejs.dev/learn/accept-input-from-the-command-line-in-nodejs – Mohammad Hossein Dolatabadi Dec 31 '21 at 08:11
  • No. while creating worker in bull – Stephen Jan 03 '22 at 06:46
  • it is not clear when and where you want to pass parameters. you may write a `config.js` nd do `import config from "./config"` or `const args = process.argv`, and then use these to pass to your bull functions. – Yılmaz Durmaz Jan 04 '22 at 16:24
  • @YılmazDurmaz Those are all normal process for forking a child process, I want to do when i span a new child process for a worker in bull and during that time I'm loading environment. Here i can get process arguments like argv array and process title kind of. Similarly i want to pass custom param. Hope it will clear your doubt – Stephen Jan 05 '22 at 02:35
  • Can you provide a simple example maybe with code snippets? – Amir Jan 05 '22 at 11:54

1 Answers1

0

when a worker script runs, it reads the environment and keeps it until you shut it down.

If you need variable parameters to the function the worker should use, then it is best you send them in your queue.

queue.js

queue.add("foo", {params:"parameters you need", payload:{ foo: "bar" }});

worker.js

const worker = new Worker("foo",
  async (job) => {
    await your_function(job.data.params, job.data.payload);
  }
);

const your_function = async (params, payload) => {
    require("fs").writeFileSync("runner.json", JSON.stringify(payload), "utf8");
  await require("child_process").fork("runner.js", params.split(" "));
};

runner.js

console.log(process.argv);
const fs = require("fs");
fs.readFile("runner.json", "utf8", function (err, data) {console.log("data: ", JSON.parse(data));});
Yılmaz Durmaz
  • 2,374
  • 12
  • 26
  • I am launching child process for worker to process a job. While forking a child process the environment will be loaded for it. During that time i have only process(https://nodejs.org/api/process.html) object which has the details of only file to be executed , and file initiated this child process, In bull -> process.argv[1] has 'bull/lib/process/master.js' and process.mainModule contains details of file to run. I could not able to find this param you passed here in process object. Will it come in process object itself?? or any other way to find? – Stephen Jan 06 '22 at 09:20
  • 1
    `bull/lib/process/master.js` is a source file for `bull.js` you don't tamper with it. node's `process` is also kinda static after you run your script. there are few ways you can run a child process but they mostly need you to hard code your parameters. but the messaging system used here gives you ability to add parameters later by this queue/qorker logic. you set up your service to accept parameters to run a child process and then you pass them through the messaging system. here `worker.js` is your service, and `queue.js` runs anywhere in the world that has access to your messaging system – Yılmaz Durmaz Jan 06 '22 at 10:23
  • @Stephen, if the above is not answering your problem, you need to provide more info about what you are trying to achieve. – Yılmaz Durmaz Jan 06 '22 at 10:28
  • @Stephen check the additional code I have added, how I passed parameters to `fork` from https://nodejs.org/api/child_process.html, and how I processed the payload. both parameters and payload are coming outside by a queue – Yılmaz Durmaz Jan 06 '22 at 12:14
  • Thanks for detaile answer for the approach of having own service which accepts and launching child process for worker with our custom param. I guess this will work. My question is, how we are passing the param while forking child process (Ex. fork(file, params) ) here likewise Can we able to pass while bull launching child process. if yes, how to pass it? – Stephen Jan 06 '22 at 13:27
  • @Stephen `params` is just regular array child process need and `params.split(" ")` is an example I pass that splits my example entry string from queue. you can shape is however you like before passing into child process, such as combining other things you set when you start&run your worker. – Yılmaz Durmaz Jan 06 '22 at 13:57