Skip to content

Commit ce1c332

Browse files
committed
Improvement to logging and cleanup
1 parent 6bca69a commit ce1c332

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

index.d.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,13 @@ export function setLogger(logger: Logger): void;
285285
* @param [options.cleanup] If truthy, the subdirectory will be deleted
286286
* when the function settles. Use with caution as it may delete files
287287
* still in use.
288+
* @param [options.logTags] If provided, these will be passed as the first
289+
* parameter to logger calls during cleanup.
288290
*/
289291
export function withSubdir<T>(
290292
dir: string,
291293
fn: (cleanupFn: () => Promise<void>) => Promise<T>,
292-
options?: { cleanup?: boolean }
294+
options?: { cleanup?: boolean, logTags?: Record<string, unknown> }
293295
): Promise<T>;
294296

295297
/**
@@ -298,4 +300,4 @@ export function withSubdir<T>(
298300
* This should be used advisedly, as it may delete files still in use.
299301
* Intended usage is at the top level between processing requests.
300302
*/
301-
export function gc(): void;
303+
export function gc(): void;

lib/tmp.js

+27-27
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
const asyncHooks = require("async_hooks");
1313
const fs = require('fs');
14+
const fsp = require('fs/promises');
1415
const os = require('os');
1516
const path = require('path');
1617
const crypto = require('crypto');
@@ -385,6 +386,8 @@ function _prepareRemoveCallback(removeFunction, fileOrDirName, sync, cleanupCall
385386
};
386387
}
387388

389+
const sleep = (ms) => new Promise((resolve)=>setTimeout(resolve, ms));
390+
388391
/**
389392
* The garbage collector.
390393
*
@@ -394,7 +397,7 @@ function _garbageCollector() {
394397
/* istanbul ignore else */
395398
if (!_gracefulCleanup) return;
396399

397-
_logger.info("lib-tmp.gc.start");
400+
_logger.info("lib-tmp.gc.global.start");
398401

399402
const attemptedObjects = _removeObjects.length;
400403
let removedObjects = 0;
@@ -414,7 +417,7 @@ function _garbageCollector() {
414417

415418
_logger.info(
416419
{ expected: attemptedObjects, actual: removedObjects, count: removedObjects, duration: dt },
417-
"lib-tmp.gc.end"
420+
"lib-tmp.gc.global.end"
418421
);
419422
}
420423

@@ -506,7 +509,8 @@ function _parseArguments(options, callback) {
506509
*/
507510
function _generateTmpName(opts) {
508511

509-
const tmpDir = opts.tmpdir;
512+
const ctx = prefixStackStorage.getStore();
513+
const tmpDir = ctx && ctx.value ? ctx.value : opts.tmpdir;
510514

511515
/* istanbul ignore else */
512516
if (!_isUndefined(opts.name))
@@ -685,13 +689,6 @@ function setGracefulCleanup() {
685689
* @returns {string} the currently configured tmp dir
686690
*/
687691
function _getTmpDir(options) {
688-
if (_defaultDir) {
689-
return path.resolve(
690-
options && options.tmpdir,
691-
_defaultDir || os.tmpdir()
692-
)
693-
}
694-
if (options && options.tmpdir) {}
695692
return path.resolve(options && options.tmpdir || os.tmpdir());
696693
}
697694

@@ -700,22 +697,26 @@ function setLogger(logger) {
700697
}
701698

702699
async function withSubdir(prefix, fn, options) {
700+
const ctx = prefixStackStorage.getStore();
701+
const tmpDir = path.resolve(
702+
ctx ? ctx.value : _getTmpDir(),
703+
prefix
704+
);
705+
await fsp.mkdir(tmpDir, { recursive: true });
706+
707+
const logTags = {
708+
...(options && options.logTags ? options.logTags : {}),
709+
path: tmpDir
710+
};
711+
703712
return new Promise((resolve, reject) => {
704-
const currentValue = prefixStackStorage.getStore();
705-
const tmpDir = path.resolve(
706-
currentValue ? currentValue.value : os.tmpdir(),
707-
prefix
708-
);
709-
fs.mkdirSync(tmpDir, { recursive: true });
710-
const cleanup = (cb) => {
711-
if (cb) {
712-
fs.rmdir(tmpDir, { recursive: true }, cb);
713-
} else {
714-
return new Promise((resolve, reject) => fs.rmdir(
715-
tmpDir,
716-
{ recursive: true },
717-
(err) => err ? reject(err) : resolve)
718-
);
713+
const cleanup = async () => {
714+
_logger.info(logTags,"lib-tmp.gc.start");
715+
try {
716+
await fsp.rmdir(tmpDir, { recursive: true, maxRetries: 5 });
717+
_logger.info({ ...logTags, duration: Date.now() - t0 }, "lib-tmp.gc.end");
718+
} catch (error) {
719+
_logger.info({ ...logTags, duration: Date.now() - t0, error }, "lib-tmp.gc.error");
719720
}
720721
}
721722
prefixStackStorage.run({ value: tmpDir }, () => {
@@ -842,6 +843,5 @@ module.exports.tmpNameSync = tmpNameSync;
842843
module.exports.setGracefulCleanup = setGracefulCleanup;
843844

844845
module.exports.setLogger = setLogger;
845-
module.exports.setDefaultDir = setDefaultDir;
846846
module.exports.gc = _garbageCollector;
847-
module.exports.withSubdir = withSubdir;
847+
module.exports.withSubdir = withSubdir;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"url": "http://github.com/raszi/node-tmp/issues"
2323
},
2424
"engines": {
25-
"node": ">=14.14"
25+
"node": ">=18"
2626
},
2727
"dependencies": {},
2828
"devDependencies": {

0 commit comments

Comments
 (0)