Saturday 29 April 2017

Running into EMFILE error when using jsmediatags

I use jsmediatags to read a tags of a huge amount of files (>20.000). My code throws an EMFILE Error (ERROR fs Error: EMFILE: too many open files, open "path-to-file") because I exceed the maximum amount of open files. I cannot use anything like graceful-js, because fs is built into the module. So my question is: How do I avoid the EMFILE error?

I can think of 2 ways: Using something to read only batches of data at once or somehow replacing the fs dependency with graceful-js. It would be very helpful if somebody could point me in the right direction.

var addToDatabase = (files) => {
    for(let i=0;i<files.length;i++){
        if(files[i].endsWith(".mp3")){
            new jsmediatags.Reader(files[i])
            .setTagsToRead(["title", "track", "artist", "album", "year"])
            .read({
                onSuccess: function(tag) {
                    database.insert({path: files[i], title: tag.tags.title, track: tag.tags.track, artist: tag.tags.artist, album: tag.tags.album, year: tag.tags.year});
                },
                onError: function(error) {
                    console.log("ERROR", error.type, error.info, files[i]);
                }
            });
        }
    }
}

require('node-dir').files(_some_dir, function(err, files) {
    if (err) dispatch(rebuildDbRejected("ERROR while reading the database directory"));
    addToDatabase(files);
});



via Nash

No comments:

Post a Comment