Monday, 12 June 2017

nodejs - wait for forEach loop to complete

I have a synchronous process I am migrating from C# to nodejs which checks a directory daily for certain files. If those files exist it adds them to a TAR file and writes that TAR to a different directory. Whilst checking for any relevant files using a forEach loop, I am struggling to get my process to wait for the loop to complete before moving onto the next function, to create the TAR file.

I have tried using the async module as suggested here and promises as suggested here. Without much success.

By making use of the async module I am hoping to halt the execution of commands so that my loop may finish before the fileList array is returned. As it currently stands, I am receiving a TypeError: Cannot read property 'undefined' of undefined.

My question: will async halt execution until my loop completes, if so what am I doing wrong?

Thanks for looking, please see my code below.

var fs = require('fs'), // access the file system.
    tar = require('tar'), // archiving tools.
    async = require('async'), // async tool to wait for the process's loop to finish.
    moment = require('moment'), // date / time tools.
    source = process.env.envA, // environment variable defining the source directory.
    destination = process.env.envB, // environment variable defining the destination directory.
    archiveName = process.env.envArc, // environment variable defining the static part of the TAR file's name.
    searchParameter = process.env.env1, // environment variable defining a file search parameter.
    date = moment().format('YYYYMMDD'); // Create a date object for file date comparison and the archive file name.

// Change working directory the process is running in.
process.chdir(source);

// Read the files within that directory.
fs.readdir(source, function (err, files) {
    // If there is an error display that error.
    if (err) {
        console.log('>>> File System Error: ' + err);
    }

    // **** LOOP ENTRY POINT ****
    // Loop through each file that is found,
    // check it matches the search parameter and current date e.g. today's date.
    CheckFiles(files, function (fileList) {
        // If files are present create a new TAR file...
        if (fileList > 0) {
            console.log('>>> File detected. Starting archiveFiles process.');
            archiveFiles(fileList);
        } else { // ...else exit the application.
            console.log('>>> No file detected, terminating process.');
            //process.exit(0);
        }
    });
});

var CheckFiles = function (files, callback) {
    console.log('>>> CheckFiles process starting.');

    var fileList = []; // Create an empty array to hold relevant file names.

    // **** THE LOOP IN QUESTION **** 
    // Loop through each file in the source directory...
    async.series(files.forEach(function (item) {
        // ...if the current file's name matches the search parameter...
        if (item.match(searchParameter)) {
            // ...and it's modified property is equal to today...
            fs.stat(item, function (err, stats) {
                if (err) {
                    console.log('>>> File Attributes Error: ' + err);
                }
                var fileDate = moment(stats.mtime).format('YYYYMMDD');

                if (fileDate === date) {
                    // ...add to an array of file names.
                    fileList.push(item);
                    console.log('>>> Date match successful: ' + item);
                } else {
                    console.log('>>> Date match not successful:' + item);
                }
            });
        }
    }), callback(fileList)); // Once all the files have been examined, return the list of relevant files.
    // **** END LOOP ****

    console.log('>>> CheckFiles process finished.');
};

var archiveFiles = function (fileList) {
    console.log('>>> Starting archiveFiles process.');

    if (fileList.length > 0) {
        // Tar the files in the array to another directory.
        tar.c({}, [fileList[0], fileList[1]]).pipe(fs.createWriteStream(destination + archiveName));
        // TODO Slack notification.
        console.log('>>> TAR file written.');
    }
};



via Matt Lindsay

No comments:

Post a Comment