fix(core): update

This commit is contained in:
Philipp Kunz 2022-11-14 14:54:26 +01:00
parent ff57dccee1
commit 84f186924e
4 changed files with 69 additions and 5 deletions

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@pushrocks/taskbuffer', name: '@pushrocks/taskbuffer',
version: '3.0.3', version: '3.0.4',
description: 'flexible task management. TypeScript ready!' description: 'flexible task management. TypeScript ready!'
} }

View File

@ -1,5 +1,42 @@
import { Task } from './taskbuffer.classes.task.js';
import * as plugins from './taskbuffer.plugins.js'; import * as plugins from './taskbuffer.plugins.js';
/**
* constains all data for the final coordinator to actually make an informed decision
*/
export interface IDistributedDecisionInfoBasis {
taskName: string;
taskVersion: string;
taskExecutionTime: number;
taskExecutionTimeout: number;
taskExecutionParallel: number;
status: 'requesting' | 'gotRejected' | 'failed' | 'succeeded';
}
export interface ITaskConsultationResult {
considered: boolean;
rank: string;
reason: string;
shouldTrigger: boolean;
}
export interface IDistributedCoordinatorConstructorOptions {
announceDistributedDecisionInfoBasis: (infoBasisArg: IDistributedDecisionInfoBasis) => Promise<ITaskConsultationResult>
updateDistributedDecisionInfoBasis: (infoBasisArg: IDistributedDecisionInfoBasis) => Promise<void>
}
export class DistributedCoordinator { export class DistributedCoordinator {
public options: IDistributedCoordinatorConstructorOptions;
constructor(optionsArg: IDistributedCoordinatorConstructorOptions) {
this.options = optionsArg;
}
public async announceDistributedDecisionInfoBasis(infoBasisArg: IDistributedDecisionInfoBasis): Promise<ITaskConsultationResult> {
return this.options.announceDistributedDecisionInfoBasis(infoBasisArg);
}
public async updateDistributedDevisionInfoBasis(infoBasisArg: IDistributedDecisionInfoBasis): Promise<void> {
return this.options.updateDistributedDecisionInfoBasis(infoBasisArg)
}
} }

View File

@ -128,12 +128,19 @@ export class Task {
// INSTANCE // INSTANCE
// mandatory properties // mandatory properties
public name: string; public name: string;
/**
* the version of the task
* should follow semver
* might be important for DistributedCoordinator
*/
public version: string;
public taskFunction: ITaskFunction; public taskFunction: ITaskFunction;
public buffered: boolean; public buffered: boolean;
public cronJob: plugins.smarttime.CronJob; public cronJob: plugins.smarttime.CronJob;
public bufferMax: number; public bufferMax: number;
public execDelay: number; public execDelay: number;
public timeout: number;
// tasks to run before and after // tasks to run before and after
public preTask: Task | TPreOrAfterTaskFunction; public preTask: Task | TPreOrAfterTaskFunction;

View File

@ -1,5 +1,6 @@
import * as plugins from './taskbuffer.plugins.js'; import * as plugins from './taskbuffer.plugins.js';
import { Task } from './taskbuffer.classes.task.js'; import { Task } from './taskbuffer.classes.task.js';
import { DistributedCoordinator } from './taskbuffer.classes.distributedcoordinator.js';
export interface ICronJob { export interface ICronJob {
cronString: string; cronString: string;
@ -7,12 +8,20 @@ export interface ICronJob {
job: any; job: any;
} }
export interface ITaskManagerConstructorOptions {
distributedCoordinator?: DistributedCoordinator
}
export class TaskManager { export class TaskManager {
public taskMap = new plugins.lik.ObjectMap<Task>(); public taskMap = new plugins.lik.ObjectMap<Task>();
private cronJobManager = new plugins.smarttime.CronManager(); private cronJobManager = new plugins.smarttime.CronManager();
constructor() { public options: ITaskManagerConstructorOptions = {
// nothing here distributedCoordinator: null
};
constructor(optionosArg: ITaskManagerConstructorOptions) {
this.options = Object.assign(this.options, optionosArg);
} }
/** /**
@ -68,7 +77,7 @@ export class TaskManager {
*/ */
public scheduleTaskByName(taskNameArg: string, cronStringArg: string) { public scheduleTaskByName(taskNameArg: string, cronStringArg: string) {
const taskToSchedule = this.getTaskByName(taskNameArg); const taskToSchedule = this.getTaskByName(taskNameArg);
const cronJob = this.cronJobManager.addCronjob(cronStringArg, async () => { const cronJob = this.cronJobManager.addCronjob(cronStringArg, async (triggerTimeArg: number) => {
console.log(`taskbuffer schedule triggered task >>${taskToSchedule.name}<<`); console.log(`taskbuffer schedule triggered task >>${taskToSchedule.name}<<`);
console.log( console.log(
`task >>${taskToSchedule.name}<< is ${ `task >>${taskToSchedule.name}<< is ${
@ -77,6 +86,17 @@ export class TaskManager {
: `unbuffered` : `unbuffered`
}` }`
); );
if (this.options.distributedCoordinator) {
console.log(`Found a distrubuted coordinator, performing distributed consultation.`);
const announcementResult = this.options.distributedCoordinator.announceDistributedDecisionInfoBasis({
status: 'requesting',
taskExecutionParallel: 1,
taskExecutionTime: triggerTimeArg,
taskExecutionTimeout: taskToSchedule.timeout,
taskName: taskToSchedule.name,
taskVersion: taskToSchedule.version,
})
}
await taskToSchedule.trigger(); await taskToSchedule.trigger();
}); });
taskToSchedule.cronJob = cronJob; taskToSchedule.cronJob = cronJob;