system: add disk usage task

This commit is contained in:
Girish Ramakrishnan
2025-07-16 23:09:06 +02:00
parent 11a6cf8236
commit 5539f74bea
10 changed files with 150 additions and 94 deletions
+50
View File
@@ -0,0 +1,50 @@
'use strict';
const debug = require('debug')('box:asynctask'),
EventEmitter = require('events'),
safe = require('safetydance');
// this runs in-process
class AsyncTask extends EventEmitter {
#name;
#abortController;
constructor(name) {
super();
this.#name = name;
this.#abortController = new AbortController();
}
async _run(/*signal*/) {
// subclasses implement this
}
async start() {
debug(`start: ${this.#name} started`);
const [error] = await safe(this._run(this.#abortController.signal)); // background
debug(`start: ${this.#name} done`, error);
this.done(error);
}
stop() {
debug(`stop: ${this.#name} stopped`);
this.#abortController.abort();
}
done(error) {
debug(`done: ${this.#name} finished`);
this.emit('done', { errorMessage: error?.message || '' });
}
emitProgress(percent, message) {
this.emit('data', 'progress', { percent, message });
}
emitData(obj) {
this.emit('data', 'data', obj);
}
}
exports = module.exports = {
AsyncTask
};