Compare commits

...

4 Commits

Author SHA1 Message Date
613d46bb38 2.1.16 2021-11-11 20:37:27 +01:00
439da1f342 fix(core): update 2021-11-11 20:37:27 +01:00
e9e4a60fe6 2.1.15 2021-11-11 19:59:56 +01:00
1630f6971f fix(core): update 2021-11-11 19:59:56 +01:00
6 changed files with 1326 additions and 1638 deletions

2879
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/taskbuffer", "name": "@pushrocks/taskbuffer",
"version": "2.1.14", "version": "2.1.16",
"private": false, "private": false,
"description": "flexible task management. TypeScript ready!", "description": "flexible task management. TypeScript ready!",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
@ -32,15 +32,15 @@
"@pushrocks/smartlog": "^2.0.44", "@pushrocks/smartlog": "^2.0.44",
"@pushrocks/smartpromise": "^3.1.6", "@pushrocks/smartpromise": "^3.1.6",
"@pushrocks/smartrx": "^2.0.19", "@pushrocks/smartrx": "^2.0.19",
"@pushrocks/smarttime": "^3.0.38", "@pushrocks/smarttime": "^3.0.43",
"@types/cron": "^1.7.3" "@types/cron": "^1.7.3"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.27", "@gitzone/tsbuild": "^2.1.28",
"@gitzone/tsbundle": "^1.0.87", "@gitzone/tsbundle": "^1.0.88",
"@gitzone/tstest": "^1.0.57", "@gitzone/tstest": "^1.0.60",
"@pushrocks/tapbundle": "^3.2.14", "@pushrocks/tapbundle": "^3.2.14",
"@types/node": "^16.10.1", "@types/node": "^16.11.7",
"tslint": "^6.1.3", "tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0" "tslint-config-prettier": "^1.18.0"
}, },

View File

@ -18,8 +18,10 @@ tap.test('should run the task as expected', async () => {
myTaskManager.addTask( myTaskManager.addTask(
new taskbuffer.Task({ new taskbuffer.Task({
name: 'myTask', name: 'myTask',
bufferMax: 1,
buffered: true,
taskFunction: async () => { taskFunction: async () => {
console.log('Task executed!'); console.log('Task "myTask" executed!');
referenceBoolean = true; referenceBoolean = true;
taskRunCounter++; taskRunCounter++;
if (taskRunCounter === 10) { if (taskRunCounter === 10) {
@ -35,7 +37,7 @@ tap.test('should run the task as expected', async () => {
}); });
tap.test('should schedule task', async () => { tap.test('should schedule task', async () => {
myTaskManager.scheduleTaskByName('myTask', '* * * * * *'); myTaskManager.scheduleTaskByName('myTask', '*/10 * * * * *');
await taskDone.promise; await taskDone.promise;
myTaskManager.descheduleTaskByName('myTask'); myTaskManager.descheduleTaskByName('myTask');
}); });

View File

@ -0,0 +1,52 @@
import { tap, expect } from '@pushrocks/tapbundle';
import * as taskbuffer from '../ts';
let counter1 = 0;
let counter2 = 0;
let counter3 = 0;
tap.test('should run buffered', async (tools) => {
const task = new taskbuffer.Task({
name: 'a buffered task',
taskFunction: async () => {
counter1++;
await tools.delayFor(2000);
console.log(`task 1 ran ${counter1} times`);
},
buffered: true,
bufferMax: 1,
afterTask: () => {
return task2;
}
});
const task2 = new taskbuffer.Task({
name: 'a buffered task',
taskFunction: async () => {
counter2++;
await tools.delayFor(2000);
console.log(`task2 ran ${counter2} times`);
},
buffered: true,
bufferMax: 1,
afterTask: () => {
return task3;
}
});
const task3 = new taskbuffer.Task({
name: 'a buffered task',
taskFunction: async () => {
counter3++;
await tools.delayFor(2000);
console.log(`task3 ran ${counter3} times`);
},
buffered: true,
bufferMax: 1
});
while(counter1 < 10) {
await tools.delayFor(5000);
task.trigger();
}
})
tap.start();

View File

@ -58,6 +58,7 @@ export class Task {
taskArg: Task | TPreOrAfterTaskFunction, taskArg: Task | TPreOrAfterTaskFunction,
optionsArg: { x?: any; touchedTasksArray?: Task[] } optionsArg: { x?: any; touchedTasksArray?: Task[] }
) => { ) => {
// extracts the task in case it is specified as a return value of a function
const taskToRun = Task.extractTask(taskArg); const taskToRun = Task.extractTask(taskArg);
const done = plugins.smartpromise.defer(); const done = plugins.smartpromise.defer();
@ -125,7 +126,7 @@ export class Task {
}; };
// INSTANCE // INSTANCE
// man datory properties // mandatory properties
public name: string; public name: string;
public taskFunction: ITaskFunction; public taskFunction: ITaskFunction;
public buffered: boolean; public buffered: boolean;
@ -208,7 +209,7 @@ export class Task {
/** /**
* trigger task buffered. * trigger task buffered.
* will simply call task.trigger(), which respects buffering by default * note: .trigger() also calls this function
*/ */
public triggerBuffered(x?: any): Promise<any> { public triggerBuffered(x?: any): Promise<any> {
return this.bufferRunner.trigger(x); return this.bufferRunner.trigger(x);

View File

@ -70,7 +70,15 @@ 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 () => {
await taskToSchedule.triggerBuffered(); console.log(`taskbuffer schedule triggered task >>${taskToSchedule.name}<<`);
console.log(
`task >>${taskToSchedule.name}<< is ${
taskToSchedule.buffered
? `buffered with max ${taskToSchedule.bufferMax} buffered calls`
: `unbuffered`
}`
);
await taskToSchedule.trigger();
}); });
taskToSchedule.cronJob = cronJob; taskToSchedule.cronJob = cronJob;
} }