0

I have a little script that executes a childprocess using execFile. This child process is a node script too that runs async operations but it seems like that the async are never ending so the terminal and all the processes are on hold.

This is the main script that runs the execFile for the child process:

fs.readdir(directoryPath, function(err, files) {
if (err) console.log(`Error: ${err}`);

files.map((file) => {
    execFile(`node`, ["updater.js", "BMW", file], (error, stdout, stderr) => {
        if (error) {
            red(`error: ${error.message}`);
            return;
        }

        if (stderr) {
            red(`stderr: ${stderr}`);
            return;
        }

        console.log(stdout);
    });
 });
});

And this is the node script executed as child process:

const args = process.argv.slice(2);
const brand = args[0];
const model = args[1];

const data = readJSON(`./json-export/${brand}/${model}`);
const generations = data.generations;

const generationsDB = await getGenerationsByModelAndBrand(brand, model);
console.log(generationsDB);

generations.map((generation) => {
   const lastModification =
       generation.modifications.modification[
           generation.modifications.modification.length - 1
        ];

  console.log(lastModification);
});

All the code works if I comment the const generationsDB line and the next console.log. If not when execution hits to the async request the execution gets stucked there. Tested the getGenerationsByModelAndBrand on the main script and works with no issue.

The getGenerationsByModelAndBrand runs a query on database and returns a Promise.

This is the getGenerationsByModelAndBrand method code:

export const getGenerationsByModelAndBrand = (brand, model) => {
    return new Promise((resolve, reject) => {
        const sql = `DATABASE SELECT`;
        connection.query(sql, function(error, result) {
            if (error) return reject(error);
            return resolve(result);
        });
    });
};

connection comes from mysql.createConnection method from the mysql package.

I believe that the issue comes from the promise handling on the child process is like I'm missing something bu couldn't find what it is.

Edit: After researching I didn't found a solution or explanation for this issue therefore in the meantime I moved the getGenerationsByModelAndBrand to the parent script and pass the result as parameter.

jcobo1
  • 1,065
  • 1
  • 18
  • 34
  • 1
    There might be situations, where a mysql connection prevents the process from exiting. https://stackoverflow.com/questions/21831493/my-nodejs-script-is-not-exiting-on-its-own-after-successful-execution. BTW if you encounter an error in the callback of `fs.readdir` you should probably stop execution because `files` might be `undefined` – derpirscher Jan 05 '22 at 10:33
  • I'm checking it with help of the `wtfnode` package but if I place the `dump()` method inside the `files.map` I get no response I only get response when I place it outside not sure why. I will keep investigating on this I didn't know this behavior either on the `mysql` package. – jcobo1 Jan 05 '22 at 11:04
  • Can you try adding a `return` in front of `connection.query` and removing the return that is in front of the `reject` and `resolve`? – Tasos Jan 05 '22 at 12:31

0 Answers0