BREAKING CHANGE(taskbuffer): Change default Task error handling: trigger() now rejects when taskFunction throws; add catchErrors option (default false) to preserve previous swallow behavior; track errors (lastError, errorCount) and expose them in metadata; improve error propagation and logging across runners, chains, parallels and debounced tasks; add tests and documentation for new behavior.
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
import * as plugins from './taskbuffer.plugins.js';
|
||||
|
||||
import { Task } from './taskbuffer.classes.task.js';
|
||||
import { logger } from './taskbuffer.logging.js';
|
||||
|
||||
export class TaskRunner {
|
||||
public maxParrallelJobs: number = 1;
|
||||
public maxParallelJobs: number = 1;
|
||||
public status: 'stopped' | 'running' = 'stopped';
|
||||
public runningTasks: plugins.lik.ObjectMap<Task> =
|
||||
new plugins.lik.ObjectMap<Task>();
|
||||
public qeuedTasks: Task[] = [];
|
||||
public queuedTasks: Task[] = [];
|
||||
|
||||
constructor() {
|
||||
this.runningTasks.eventSubject.subscribe(async (eventArg) => {
|
||||
@@ -16,19 +17,19 @@ export class TaskRunner {
|
||||
}
|
||||
|
||||
/**
|
||||
* adds a task to the qeue
|
||||
* adds a task to the queue
|
||||
*/
|
||||
public addTask(taskArg: Task) {
|
||||
this.qeuedTasks.push(taskArg);
|
||||
this.queuedTasks.push(taskArg);
|
||||
this.checkExecution();
|
||||
}
|
||||
|
||||
/**
|
||||
* set amount of parallel tasks
|
||||
* be careful, you might loose dependability of tasks
|
||||
* be careful, you might lose dependability of tasks
|
||||
*/
|
||||
public setMaxParallelJobs(maxParrallelJobsArg: number) {
|
||||
this.maxParrallelJobs = maxParrallelJobsArg;
|
||||
public setMaxParallelJobs(maxParallelJobsArg: number) {
|
||||
this.maxParallelJobs = maxParallelJobsArg;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,17 +40,21 @@ export class TaskRunner {
|
||||
}
|
||||
|
||||
/**
|
||||
* checks wether execution is on point
|
||||
* checks whether execution is on point
|
||||
*/
|
||||
public async checkExecution() {
|
||||
if (
|
||||
this.runningTasks.getArray().length < this.maxParrallelJobs &&
|
||||
this.runningTasks.getArray().length < this.maxParallelJobs &&
|
||||
this.status === 'running' &&
|
||||
this.qeuedTasks.length > 0
|
||||
this.queuedTasks.length > 0
|
||||
) {
|
||||
const nextJob = this.qeuedTasks.shift();
|
||||
const nextJob = this.queuedTasks.shift();
|
||||
this.runningTasks.add(nextJob);
|
||||
await nextJob.trigger();
|
||||
try {
|
||||
await nextJob.trigger();
|
||||
} catch (err) {
|
||||
logger.log('error', `TaskRunner: task "${nextJob.name || 'unnamed'}" failed: ${err instanceof Error ? err.message : String(err)}`);
|
||||
}
|
||||
this.runningTasks.remove(nextJob);
|
||||
this.checkExecution();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user