nodejs - дождитесь завершения fs.stat

1

У меня синхронный процесс, который я переношу с С# на nodejs, который ежедневно проверяет каталог для определенных файлов. Если эти файлы существуют, они добавляют их в файл TAR и записывают этот TAR в другой каталог. Проверяя любые соответствующие файлы с помощью цикла forEach, я изо всех сил forEach, чтобы мой процесс дождался завершения цикла, прежде чем перейти к следующей функции, чтобы создать файл TAR.

Я попытался использовать async модуль, как предлагается здесь, и обещает, как предлагается здесь. Без особого успеха.

Используя async модуль, я надеюсь прекратить выполнение команд, чтобы мой цикл мог завершиться до fileList массива fileList. Поскольку он в настоящее время стоит, я получаю TypeError: Cannot read property 'undefined' of undefined.

Мой вопрос: будет ли async завершение выполнения, пока мой цикл не завершится, если да, то что я делаю неправильно?

Спасибо, что посмотрели, пожалуйста, см. Мой код ниже.

var fs = require('fs'), // access the file system.
    tar = require('tar'), // archiving tools.
    async = require('async'), // async tool to wait for the process 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 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 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 name matches the search parameter...
        if (item.match(searchParameter)) {
            // ...and it 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.');
    }
};
Теги:
foreach

2 ответа

1
Лучший ответ

Async не нужен, использование Promises предложенное @I Blue Blue Ba Ba Dee и fs.statSync как было предложено компанией @Cheloid, выполнило мои требования. Для тех, кто может извлечь выгоду из этого результата, см. Мой код ниже.

var fs = require('fs'), // access the file system.
    tar = require('tar'), // archiving tools.
    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 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.
    fileList = [], // create an empty array to hold relevant file names.
    slack = require('./slack.js'); // import Slack notification functionality.

// 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 through each file that is found...
    checkFilesPromise(files).then(function (response) {
        console.log('>>> File(s) detected. Starting archiveFilesPromise.');

        // Archive any relevant files.
        archiveFilesPromise(fileList).then(function (response) {
            console.log('>>> TAR file written.');

            // Send a Slack notification when complete.
            slack('TAR file written.', 'good', response);
        }, function (error) {
            console.log('>>> archiveFilesPromise error: ' + error);
            slack('archiveFilesPromise error:' + error, 'Warning', error);
        });
    }, function (error) {
        console.log('>>> CheckFilesPromise error ' + error);
        slack('CheckFilesPromise error: ' + error, 'Warning', error);
    });
});

var checkFilesPromise = function (files) {
    return new Promise(function (resolve, reject) {
        files.forEach(function (item) {
            // ...check it matches the search parameter...
            if (item.match(searchParameter)) {
                var stats = fs.statSync(item);
                var fileDate = moment(stats.mtime).format('YYYYMMDD');

                // ...and current date e.g. today date.
                if (fileDate === date) {
                    // Add file to an array of file names.
                    console.log('>>> Date match successful, pushing: ' + item);
                    fileList.push(item);
                    resolve('Success');
                 } else {
                    reject('Failure');
                }
            }
        });
    });
};

var archiveFilesPromise = function (list) {
    return new Promise(function (resolve, reject) {

        if (list.length > 0) {
            // Tar the files in the array to another directory.
            tar.c({}, [list[0], list[1]]).pipe(fs.createWriteStream(destination + date + archiveName));
            resolve('Success');
        } else {
            reject('Failure');
        }
    });
};
0

Вы можете использовать обычный for цикла и на последней итерации вызова функции обратного вызова.

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

    var fileList = []; // Create an empty array to hold relevant file names.
    for (var i = 0, n = files.length; i < n; ++i)
        // ...if the current file name matches the search parameter...
        if (item.match(searchParameter)) {
            // ...and it 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);
                }
            });
        }
    if (i === n + 1) {
        callback(fileList);
        console.log('>>> CheckFiles process finished.');
    }
};

Редактировать:

Используйте рекурсивные обратные вызовы, я не уверен, что этот код будет работать, но я надеюсь, что вы получите эту идею.

fs.stats является асинхронным, и поэтому цикл не ждет его... вы можете использовать обратные вызовы для "ожидания" для него.

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

    var arrIndex = 0;
    var fileList = [];

    recursiveCallback(fileList, callback); //callling our callback

    function recursiveCallback(array, callback) { //recursive callback inside our function

        var item = files[arrIndex++];

        if (item.match(searchParameter)) {
            // ...and it 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.
                    array.push(item);
                    console.log('>>> Date match successful: ' + item);
                } else {
                    console.log('>>> Date match not successful:' + item);
                }
                if (files.length < arrIndex) //when last item, use the main callback to retrieve the array
                    callback(array);
                else    //when not last item , recursion
                    recursiveCallback(item, array, callback);
            });
        } else if (files.length < arrIndex) //when last item, use the main callback to retrieve the array
            callback(array);
        else    //when not last item , recursion
            recursiveCallback(item, array, callback);
    }
}
  • 0
    Привет @Cheloide, спасибо за ваш ответ. Я попробовал ваше предложение и переработал его несколько раз. Однако я обнаружил, что операция fileList.push выполняется после того, как цикл переместился и выполнил обратный вызов. Либо так, либо мой fileList.push вообще не работает?
  • 0
    @MattLindsay, я изменил свой ответ. из-за асинхронной природы fs.stats мой последний ответ никогда не будет работать (возможно, будет работать синхронная версия fs.stats). У меня не было времени сегодня, иначе я бы ответил раньше, надеюсь, это поможет.
Показать ещё 2 комментария

Ещё вопросы

Сообщество Overcoder
Наверх
Меню