@akala/core
Version:
53 lines • 2.03 kB
JavaScript
import { EventEmitter } from './events/event-emitter.js';
import { each as eachAsync } from './eachAsync.js';
import sequencify from './sequencify.js';
/**
* Orchestrates task execution with dependency management
* @class Orchestrator
* @extends EventEmitter
* @fires Orchestrator#start
* @fires Orchestrator#task_start
* @fires Orchestrator#task_stop
* @fires Orchestrator#error
* @fires Orchestrator#stop
*/
export class Orchestrator extends EventEmitter {
tasks = {};
/**
* Adds a task to the orchestrator
* @param {string} name - Unique identifier for the task
* @param {string[]} dependencies - Array of task names this task depends on
* @param {function} [action] - Optional async function to execute for the task
*/
add(name, dependencies, action) {
this.tasks[name] = { name, dep: dependencies, action };
}
/**
* Executes tasks in dependency order
* @param {...string} names - Task names to execute (and their dependencies)
* @returns {Promise<void>} Resolves when all tasks complete, rejects on critical error
*/
async start(...names) {
const seq = sequencify(this.tasks, names);
this.emit('start', names);
try {
await eachAsync(seq.sequence, async (task) => {
this.emit('task_start', { message: `starting ${task}...`, task: this.tasks[task], taskName: task });
if (this.tasks[task].action)
try {
await this.tasks[task].action();
}
catch (e) {
this.emit('error', { error: e, task: this.tasks[task] });
throw e;
}
this.emit('task_stop', { message: `${task} finished.`, task: this.tasks[task], taskName: task }, this.tasks[task]);
});
this.emit('stop');
}
catch (e) {
this.emit('stop', e);
}
}
}
//# sourceMappingURL=orchestrator.js.map