Closed mszekiel closed 4 years ago
Even if I try hackish way and do something like fire and forget of dispose it throws at the dispose.
setTimeout(() => {
powershell.dispose();
}, 10000);
I wrote a program that used node-powershell and it had to do many operations that were asynchronous. What I landed on was that the only way I could do things as quick as I wanted while resource efficient was to only create a single PS object with const powershell = new Shell, never dispose of it, and always be listening for the output of Powershell. Because I was doing LDAP lookups and things I had to be on the lookout for thing coming back in the wrong order or even mashed together.
//one powershell to rule them all
powershell.on('output', output =>{
let data;
try{
data = JSON.parse(output);
console.log("PowerShell returning data: " + output);
evaluatePSOutput(data);
}
catch(err){
console.log('EOI error');
let outputChunks = output.split(`EOI`);
outputChunks.forEach(chunk => {
let myData=JSON.parse(chunk);
evaluatePSOutput(myData);
});
}
@BenjaminMichael okay, but what if I trigger multiple scripts/cmds all the time app is running? For eg. I start long script to install VM but concurrently run other commands in PS.
You could wrap your script/cmds inside a scriptblock and use start-job to run it async but you don't have to do it this way.
You can have multiple node-powershell objects running at once and be able to .dispose() of each with a callback but in your case your script is returning exit code 0 which disposes of it automatically. Note that node-powershell handles EOI (end of input) differently from the process closing and if the process closes and it exited with code 0 then it automatically disposes.
this._proc.on('close', code => {
this.emit('end', code);
let exitMsg = `Process ${this._proc.pid} exited with code ${code}\n`;
if(hasError) {
// PS process failed to start
this._print(ERROR_MSG(exitMsg));
// this._print(ERROR_MSG(Buffer.concat(output.stdout).toString()));
throw new Error(Buffer.concat(output.stdout).toString().replace(/\0/g, ''));
} else {
// dispose
if(code !== 0) {
this._print(ERROR_MSG(exitMsg));
this._reject(ERROR_MSG(`script exit ${code}`));
} else {
this._print(OK_MSG(exitMsg));
this._resolve(`script exit ${code}`);
}
}
});
tl;dr take the Exit keyword out of your script and it probably wouldnt behave this way
I would add: what if you had to do a hundred tasks? I ran into this with recursive functions and found out I didn't really want to try and open up to 100 powershell instances at once. The way I tried to deal with it was to open 1 powershell objects and use better-queue to queue up powershell commands 1 at a time but you can push a new task into the queue asynchronously no problem.
PSCulster is coming :)
I am using node-powershell with some script files of PS. One of them I use for isntalling VM. It is quite big script. I call all my scripts via execute function.
I use dispose after every single command. That makes app lightweight and max of 70MB of RAM usage. Calling simple commands is quite easy and works, no errors. But if I call my large script I have problem with
dispose()
. It returns me the following error.