* Created new npm module: temptmp: This replaces node-temp usage & solves global temp file cleanup issue with concept of temp "sessions"

This commit is contained in:
Bryan Ashby 2017-01-29 19:56:46 -07:00
parent e10d085cab
commit 9525afddd3
5 changed files with 33 additions and 39 deletions

View File

@ -9,6 +9,7 @@ const FileEntry = require('./file_entry.js');
const FileDb = require('./database.js').dbs.file;
const ArchiveUtil = require('./archive_util.js');
const CRC32 = require('./crc.js').CRC32;
const Log = require('./logger.js').log;
// deps
const _ = require('lodash');
@ -16,7 +17,7 @@ const async = require('async');
const fs = require('fs');
const crypto = require('crypto');
const paths = require('path');
const temp = require('temp').track(); // track() cleans up temp dir/files for us
const temptmp = require('temptmp').createTrackedSession('file_area');
const iconv = require('iconv-lite');
exports.isInternalArea = isInternalArea;
@ -298,7 +299,7 @@ function populateFileEntryWithArchive(fileEntry, filePath, stepInfo, iterator, c
return callback(null, [] );
}
temp.mkdir('enigextract-', (err, tempDir) => {
temptmp.mkdir( { prefix : 'enigextract-' }, (err, tempDir) => {
if(err) {
return callback(err);
}
@ -338,14 +339,11 @@ function populateFileEntryWithArchive(fileEntry, filePath, stepInfo, iterator, c
return next(null);
});
}, () => {
// cleanup, but don't wait...
/*
:TODO: fix global temp cleanup issue!!!
temp.cleanup( err => {
// :TODO: Log me!
});*/
// cleanup but don't wait
temptmp.cleanup( paths => {
// note: don't use client logger here - may not be avail
Log.debug( { paths : paths, sessionId : temptmp.sessionId }, 'Cleaned up temporary files' );
});
return callback(null);
});
},

View File

@ -18,7 +18,7 @@ const paths = require('path');
const async = require('async');
const fs = require('fs');
const later = require('later');
const temp = require('temp').track(); // track() cleans up temp dir/files for us
const temptmp = require('temptmp').createTrackedSession('ftn_bso');
const assert = require('assert');
const gaze = require('gaze');
const fse = require('fs-extra');
@ -1153,14 +1153,14 @@ function FTNMessageScanTossModule() {
};
this.createTempDirectories = function(cb) {
temp.mkdir('enigftnexport-', (err, tempDir) => {
temptmp.mkdir( { prefix : 'enigftnexport-' }, (err, tempDir) => {
if(err) {
return cb(err);
}
self.exportTempDir = tempDir;
temp.mkdir('enigftnimport-', (err, tempDir) => {
temptmp.mkdir( { prefix : 'enigftnimport-' }, (err, tempDir) => {
self.importTempDir = tempDir;
cb(err);
@ -1290,21 +1290,18 @@ FTNMessageScanTossModule.prototype.shutdown = function(cb) {
//
// Clean up temp dir/files we created
//
/*
:TODO: fix global temp cleanup issue!!!
temp.cleanup((err, stats) => {
const fullStats = Object.assign(stats, { exportTemp : this.exportTempDir, importTemp : this.importTempDir } );
temptmp.cleanup( paths => {
const fullStats = {
exportDir : this.exportTempDir,
importTemp : this.importTempDir,
paths : paths,
sessionId : temptmp.sessionId,
};
if(err) {
Log.warn(fullStats, 'Failed cleaning up temporary directories!');
} else {
Log.trace(fullStats, 'Temporary directories cleaned up');
}
Log.trace(fullStats, 'Temporary directories cleaned up');
FTNMessageScanTossModule.super_.prototype.shutdown.call(this, cb);
});
*/
FTNMessageScanTossModule.super_.prototype.shutdown.call(this, cb);
};

View File

@ -9,12 +9,13 @@ const Errors = require('./enig_error.js').Errors;
const DownloadQueue = require('./download_queue.js');
const StatLog = require('./stat_log.js');
const FileEntry = require('./file_entry.js');
const Log = require('./logger.js').log;
// deps
const async = require('async');
const _ = require('lodash');
const pty = require('ptyw.js');
const temp = require('temp').track(); // track() cleans up temp dir/files for us
const temptmp = require('temptmp').createTrackedSession('transfer_file');
const paths = require('path');
const fs = require('fs');
const fse = require('fs-extra');
@ -264,7 +265,7 @@ exports.getModule = class TransferFileModule extends MenuModule {
return callback(null, null);
}
temp.open( { prefix : TEMP_SUFFIX, suffix : '.txt' }, (err, tempFileInfo) => {
temptmp.open( { prefix : TEMP_SUFFIX, suffix : '.txt' }, (err, tempFileInfo) => {
if(err) {
return callback(err); // failed to create it
}
@ -511,15 +512,11 @@ exports.getModule = class TransferFileModule extends MenuModule {
});
}
},
function cleanupTempFiles(callback) {
/* :TODO: figure out the global temp cleanup() issue!!@!
temp.cleanup( err => {
if(err) {
self.client.log.warn( { error : err.message }, 'Failed to clean up temporary file/directory(s)' );
}
return callback(null); // ignore err
function cleanupTempFiles(callback) {
temptmp.cleanup( paths => {
Log.debug( { paths : paths, sessionId : temptmp.sessionId }, 'Temporary files cleaned up' );
});
*/
return callback(null);
},
function updateUserAndSystemStats(callback) {

View File

@ -10,11 +10,12 @@ const scanFile = require('../core/file_area.js').scanFile;
const ansiGoto = require('../core/ansi_term.js').goto;
const moveFileWithCollisionHandling = require('../core/file_util.js').moveFileWithCollisionHandling;
const pathWithTerminatingSeparator = require('../core/file_util.js').pathWithTerminatingSeparator;
const Log = require('../core/logger.js').log;
// deps
const async = require('async');
const _ = require('lodash');
const temp = require('temp').track(); // track() cleans up temp dir/files for us
const temptmp = require('temptmp').createTrackedSession('upload');
const paths = require('path');
exports.moduleInfo = {
@ -142,15 +143,16 @@ exports.getModule = class UploadModule extends MenuModule {
leave() {
// remove any temp files - only do this when
if(this.isFileTransferComplete()) {
// :TODO: fix global temp cleanup issue!!!
//temp.cleanup(); // remove any temp files
temptmp.cleanup( paths => {
Log.debug( { paths : paths, sessionId : temptmp.sessionId }, 'Temporary files cleaned up' );
});
}
super.leave();
}
performBlindUpload(cb) {
temp.mkdir('enigul-', (err, tempRecvDirectory) => {
temptmp.mkdir( { prefix : 'enigul-' }, (err, tempRecvDirectory) => {
if(err) {
return cb(err);
}

View File

@ -38,7 +38,7 @@
"ptyw.js": "NuSkooler/ptyw.js",
"sqlite3": "^3.1.1",
"ssh2": "^0.5.1",
"temp": "^0.8.3"
"temptmp" : "^1.0.0"
},
"devDependencies": {
"lodash-migrate": "^0.3.16"