rentner323
rentner323

Reputation: 11

NodeJS - EMFILE Exception - request piped to writeStream

In a NodeJS Server im getting pictures from another WebServer periodicly and store them in a specific folder.

Im doing this with this command (In a function that is polled by a timer every 10 Seconds):

request(cameraRequestOptions).pipe(fs.createWriteStream("./campics/cam" + cameraId + "pic" + latestCameraPictureNumber + ".jpg"));

The name of the target image file is changing due to the "latestCameraPictureNumber".

Im having trouble with a EMFILE Exception, the strange thing is that this is happening only on some Maschines the server runs on and it seems to happen only after a long period of time.

As i already found out, this could mean that im opening too many FileStreams or Sockets. So im not sure if im doing something wrong with my pipe: Do I have to close the WriteStream or the request? And when how?

The Documentation says:

readable.pipe(destination, [options])#

destination Writable Stream The destination for writing data

options Object Pipe options

end Boolean End the writer when the reader ends. Default = true

So i thought it will be closed automatically.

Thanks for any help.

Upvotes: 0

Views: 176

Answers (1)

rentner323
rentner323

Reputation: 11

The problem seems to be more complicated, I added callback functions to determin if all streams and requests are closed properly, it also seems the problem is caused by some other pipes im using. Im using the following stream oerations:

var fsCTmp = fs.createWriteStream("./campics/" + newLogId + "cam"+camId+"pic" + picNum + ".jpg");
fsCTmp.on('finish', wscfTest);
fs.createReadStream("./campics/cam"+camId+"pic" + picNum + ".jpg").pipe(fsCTmp).on('finish', rsfTest);

var fsCP = fs.createWriteStream("./campics/cam" + cameraId + "pic" + latestCameraPictureNumber + ".jpg");
fsCP.on('finish', wsftest);
request(cameraRequestOptions).pipe(fsCP).on('finish', rftest);

var fsWTmp = fs.createWriteStream("./campics/" + activeAlarmPicturesToAddToLog[j].logId + "cam"+cameraId+"pic" + activeAlarmPicturesToAddToLog[j].picNum + ".jpg");
fsWTmp.on('finish', wscfTest);
fs.createReadStream(picturePath).pipe(fsWTmp).on('finish', rsfTest);

And im sending the files to a WebClient via

res.sendfile(<path>);

Im making logs to the console in every callback functions, everything seems to work fine, but so far i could only test it on my own pc, and the exception so far only occours on every maschine except mine after some amount of pipes have been done (about 10-100).

Upvotes: 0

Related Questions