1

我正在使用归档器在 aws lambda 中压缩一些文件。我看到当没有更多的磁盘空间可以写入时,归档器仍然保存(或泄漏)一些数据。我一直在尝试执行 'archive.close()' 或 'archive.destroy()' 但我不断收到 archive.close/(archive.destroy) 不是函数错误。想知道是否有人遇到过或解决了它。这是我的代码的样子:

exports.makeZip = function(source, destination) {
    return new Promise((resolve, reject) => {
        console.info("started zipping from source: %s to destination: %s", source, destination);
        let output, archive;
        try {
            output = fs.createWriteStream(destination);
            archive = archiver('zip');
            output.on('close', function () {
                output.destroy();
                //archive.close();
                console.log("Completed Zipping!");
                resolve();
            });
            output.on('error', function(err) {
                output.destroy();
                //archive.close();
                console.error("something went wrong while zipping! ", err);
                reject(new Error(err));
            });
            archive.on('error', function(err) {
                //archive.close();
                output.destroy();
                console.error("something went wrong while zipping! ", err);
                reject(new Error(err));
            });
            archive.pipe(output);
            archive.directory(source, false);
            archive.finalize();
        } catch (error) {
            if (typeof output !== 'undefined') {
                output.destroy();
            }
            if (typeof archive !== 'undefined') {
                archive.close();
            }
            console.error("Zipping failed. Reason: %s", error.message)
            reject(new Error(error));
        }
    });
};

我知道这是导致内存泄漏的方式,因为我在 Lambda 调用的开始和结束时删除了文件之前和之后打印了磁盘空间。我看到有一些空间没有清理干净。

Before cleanup
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/root        8191416 6107036   2067996  75% /
/dev/vdb         1490800   34896   1439520   3% /dev
/dev/vdd          538424  526588         0 100% /tmp
After cleanup
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/root        8191416 6107036   2067996  75% /
/dev/vdb         1490800   34896   1439520   3% /dev
/dev/vdd          538424   11068    515520   3% /tmp
4

0 回答 0