system: add disk usage task
This commit is contained in:
@@ -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
|
||||
};
|
||||
Reference in New Issue
Block a user