clever-queue 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/task/index.d.ts +1 -1
- package/dist/task/index.js +11 -4
- package/dist/task/index.js.map +1 -1
- package/dist/task/interfaces.d.ts +1 -1
- package/dist/task/interfaces.js +4 -0
- package/dist/task/interfaces.js.map +1 -1
- package/exemples/index01.js +116 -116
- package/package.json +54 -54
- package/src/task/index.ts +11 -5
- package/src/task/interfaces.ts +5 -1
- package/test/issues/00001.mjs +35 -0
package/dist/task/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Options, Statistics, FunctionToExecute } from "./interfaces";
|
|
2
|
-
type Status = "unknown" | "initialiazing" | "running" | "stopping" | "destroying";
|
|
2
|
+
type Status = "unknown" | "initialiazing" | "running" | "stopping" | "exception" | "destroying";
|
|
3
3
|
declare class Task {
|
|
4
4
|
#private;
|
|
5
5
|
status: Status;
|
package/dist/task/index.js
CHANGED
|
@@ -57,10 +57,17 @@ class Task {
|
|
|
57
57
|
this.runner = async () => {
|
|
58
58
|
this.status = "running";
|
|
59
59
|
// console.log("running Task");
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
try {
|
|
61
|
+
this.result = await this.#functionToExecute();
|
|
62
|
+
// console.log("task runned", this.result);
|
|
63
|
+
this.status = "stopping";
|
|
64
|
+
this.resolver(this.result);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
// console.log("task error", this.result);
|
|
68
|
+
this.status = "exception";
|
|
69
|
+
this.rejecter(new _.Errors.CQError(interfaces_1.ErrorsList.FunctionRaisedAnHundledException));
|
|
70
|
+
}
|
|
64
71
|
};
|
|
65
72
|
}
|
|
66
73
|
#checkOptionsConsistancy(options) {
|
package/dist/task/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources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xB,+BAA+B;YAC/B,IAAI,CAAC;gBACH,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,CAAC,kBAAkB,EAAE,CAAC;gBAC9C,2CAA2C;gBAC3C,IAAI,CAAC,MAAM,GAAG,UAAU,CAAC;gBACzB,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YAC7B,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,0CAA0C;gBAC1C,IAAI,CAAC,MAAM,GAAG,WAAW,CAAC;gBAC1B,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,uBAAU,CAAC,gCAAgC,CAAC,CAAC,CAAC;YACnF,CAAC;QACH,CAAC,CAAC;IACJ,CAAC;IAED,wBAAwB,CAAC,OAAgB;QACvC,IAAI,CAAC,OAAO;YAAE,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,uBAAU,CAAC,6BAA6B,CAAC,CAAC;QACnF,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,UAAU;QACR,MAAM,IAAI,GAAe;YACvB,MAAM,EAAE,IAAI,CAAC,MAAM;SACpB,CAAC;QACF,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAGQ,oBAAI;AADb,2CAAkF;AAApD,wGAAA,UAAU,OAAA"}
|
|
@@ -7,7 +7,7 @@ interface Options {
|
|
|
7
7
|
interface Statistics {
|
|
8
8
|
status: string;
|
|
9
9
|
}
|
|
10
|
-
type ErrorListKeys = "NoOptionsOnTaskInitialization" | "FunctionIsNotAFunction" | "TaskRunnerNotInitialized";
|
|
10
|
+
type ErrorListKeys = "NoOptionsOnTaskInitialization" | "FunctionIsNotAFunction" | "FunctionRaisedAnHundledException" | "TaskRunnerNotInitialized";
|
|
11
11
|
declare const ErrorsList: {
|
|
12
12
|
[index in ErrorListKeys]: {
|
|
13
13
|
name: string;
|
package/dist/task/interfaces.js
CHANGED
|
@@ -10,6 +10,10 @@ const ErrorsList = {
|
|
|
10
10
|
name: "FunctionIsNotAFunction",
|
|
11
11
|
message: "FunctionIsNotAFunction",
|
|
12
12
|
},
|
|
13
|
+
FunctionRaisedAnHundledException: {
|
|
14
|
+
name: "FunctionRaisedAnHundledException",
|
|
15
|
+
message: "FunctionRaisedAnHundledException",
|
|
16
|
+
},
|
|
13
17
|
TaskRunnerNotInitialized: {
|
|
14
18
|
name: "TaskRunnerNotInitialized",
|
|
15
19
|
message: "TaskRunnerNotInitialized",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../src/task/interfaces.ts"],"names":[],"mappings":";;;AAgBA,MAAM,UAAU,GAEZ;IACF,6BAA6B,EAAE;QAC7B,IAAI,EAAE,+BAA+B;QACrC,OAAO,EAAE,+BAA+B;KACzC;IACD,sBAAsB,EAAE;QACtB,IAAI,EAAE,wBAAwB;QAC9B,OAAO,EAAE,wBAAwB;KAClC;IACD,wBAAwB,EAAE;QACxB,IAAI,EAAE,0BAA0B;QAChC,OAAO,EAAE,0BAA0B;KACpC;CACF,CAAC;AAE4B,gCAAU"}
|
|
1
|
+
{"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../src/task/interfaces.ts"],"names":[],"mappings":";;;AAgBA,MAAM,UAAU,GAEZ;IACF,6BAA6B,EAAE;QAC7B,IAAI,EAAE,+BAA+B;QACrC,OAAO,EAAE,+BAA+B;KACzC;IACD,sBAAsB,EAAE;QACtB,IAAI,EAAE,wBAAwB;QAC9B,OAAO,EAAE,wBAAwB;KAClC;IACD,gCAAgC,EAAE;QAChC,IAAI,EAAE,kCAAkC;QACxC,OAAO,EAAE,kCAAkC;KAC5C;IACD,wBAAwB,EAAE;QACxB,IAAI,EAAE,0BAA0B;QAChC,OAAO,EAAE,0BAA0B;KACpC;CACF,CAAC;AAE4B,gCAAU"}
|
package/exemples/index01.js
CHANGED
|
@@ -1,116 +1,116 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
const CleverQueue = __importStar(require("../dist/index.js"));
|
|
27
|
-
/* openTelemetry */
|
|
28
|
-
/*
|
|
29
|
-
import * as OTL from "@opentelemetry/api";
|
|
30
|
-
import * as OTLTrace from "@opentelemetry/sdk-trace-node";
|
|
31
|
-
import * as OTLTraceBase from "@opentelemetry/sdk-trace-base";
|
|
32
|
-
|
|
33
|
-
const { trace, context } = require("@opentelemetry/api");
|
|
34
|
-
|
|
35
|
-
const activeSpan = OTL.trace.getSpan(OTL.context.active());
|
|
36
|
-
|
|
37
|
-
const traceId = activeSpan.spanContext().traceId;
|
|
38
|
-
const spanId = activeSpan.spanContext().spanId;
|
|
39
|
-
const traceFlag = activeSpan.spanContext().traceFlags;
|
|
40
|
-
|
|
41
|
-
console.log(traceId, spanId, traceFlag);
|
|
42
|
-
|
|
43
|
-
/*
|
|
44
|
-
import { NodeSDK as OTLNodeSDK } from "@opentelemetry/sdk-node";
|
|
45
|
-
|
|
46
|
-
const otl = new OTLNodeSDK({
|
|
47
|
-
traceExporter: new OTLConsoleSpanExporter(),
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
otl.start();
|
|
51
|
-
*/
|
|
52
|
-
/*
|
|
53
|
-
OTL.diag.setLogger(new OTL.DiagConsoleLogger(), OTL.DiagLogLevel.DEBUG);
|
|
54
|
-
const tracerProvider = new OTLTrace.NodeTracerProvider();
|
|
55
|
-
const exporter = new OTLTraceBase.ConsoleSpanExporter();
|
|
56
|
-
const processor = new OTLTraceBase.SimpleSpanProcessor(exporter);
|
|
57
|
-
tracerProvider.addSpanProcessor(processor);
|
|
58
|
-
tracerProvider.register();
|
|
59
|
-
|
|
60
|
-
const tracer = OTL.trace.getTracer("esm-tracer");
|
|
61
|
-
tracer.startActiveSpan("manual", (span) => {
|
|
62
|
-
span.addEvent("myEvent");
|
|
63
|
-
span.addEvent("myEvent2");
|
|
64
|
-
span.end();
|
|
65
|
-
});
|
|
66
|
-
*/
|
|
67
|
-
// eslint-disable-next-line unicorn/no-process-exit
|
|
68
|
-
// process.exit(0);
|
|
69
|
-
const myFunction = function (message) {
|
|
70
|
-
// eslint-disable-next-line no-console
|
|
71
|
-
console.log("Executing", message);
|
|
72
|
-
};
|
|
73
|
-
/*
|
|
74
|
-
const myPromise: CleverQueue.Task.FunctionToExecute = function (message: string): Promise<string> {
|
|
75
|
-
console.log(message);
|
|
76
|
-
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
77
|
-
console.log(message);
|
|
78
|
-
return message;
|
|
79
|
-
};
|
|
80
|
-
*/
|
|
81
|
-
const myAsync = async function (message) {
|
|
82
|
-
logFunction({ body: "Executing " + message });
|
|
83
|
-
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
84
|
-
return message;
|
|
85
|
-
};
|
|
86
|
-
const logFunction = function (logRecord) {
|
|
87
|
-
console.log(JSON.stringify(logRecord));
|
|
88
|
-
};
|
|
89
|
-
async function run() {
|
|
90
|
-
logFunction({ body: "01 - Creating Engine" });
|
|
91
|
-
const engine = CleverQueue.createEngine({ logFunction: logFunction, bestEffortRunners: 2 });
|
|
92
|
-
logFunction({ body: "02 - Creating Queue" });
|
|
93
|
-
const queue = engine.createQueue({ priority: CleverQueue.Queue.Priorities.Standard, weight: 50 });
|
|
94
|
-
logFunction({ body: "03 - Creating Tasks" });
|
|
95
|
-
const task1 = engine.createTask(() => myAsync("myAsync01"), {});
|
|
96
|
-
const task2 = engine.createTask(() => myAsync("myAsync02"), {});
|
|
97
|
-
const task3 = engine.createTask(() => myAsync("myAsync03"), {});
|
|
98
|
-
logFunction({ body: "04 - Enqueue Task" });
|
|
99
|
-
const result = await queue.enqueue(task1);
|
|
100
|
-
queue.enqueue(task2);
|
|
101
|
-
queue.enqueue(task3);
|
|
102
|
-
logFunction({ body: JSON.stringify(engine.statistics()) });
|
|
103
|
-
logFunction({ body: result });
|
|
104
|
-
logFunction({ body: "05 - Stopping Engine" });
|
|
105
|
-
logFunction({ body: JSON.stringify(engine.statistics()) });
|
|
106
|
-
engine.stop();
|
|
107
|
-
logFunction({ body: JSON.stringify(engine.statistics()) });
|
|
108
|
-
while (engine.statistics().engine.tasks > 0) {
|
|
109
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
110
|
-
}
|
|
111
|
-
logFunction({ body: JSON.stringify(engine.statistics()) });
|
|
112
|
-
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
113
|
-
logFunction({ body: JSON.stringify(engine.statistics()) });
|
|
114
|
-
logFunction({ body: { task1: task1.result, task2: task2.result, task3: task3.result } });
|
|
115
|
-
}
|
|
116
|
-
run();
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
const CleverQueue = __importStar(require("../dist/index.js"));
|
|
27
|
+
/* openTelemetry */
|
|
28
|
+
/*
|
|
29
|
+
import * as OTL from "@opentelemetry/api";
|
|
30
|
+
import * as OTLTrace from "@opentelemetry/sdk-trace-node";
|
|
31
|
+
import * as OTLTraceBase from "@opentelemetry/sdk-trace-base";
|
|
32
|
+
|
|
33
|
+
const { trace, context } = require("@opentelemetry/api");
|
|
34
|
+
|
|
35
|
+
const activeSpan = OTL.trace.getSpan(OTL.context.active());
|
|
36
|
+
|
|
37
|
+
const traceId = activeSpan.spanContext().traceId;
|
|
38
|
+
const spanId = activeSpan.spanContext().spanId;
|
|
39
|
+
const traceFlag = activeSpan.spanContext().traceFlags;
|
|
40
|
+
|
|
41
|
+
console.log(traceId, spanId, traceFlag);
|
|
42
|
+
|
|
43
|
+
/*
|
|
44
|
+
import { NodeSDK as OTLNodeSDK } from "@opentelemetry/sdk-node";
|
|
45
|
+
|
|
46
|
+
const otl = new OTLNodeSDK({
|
|
47
|
+
traceExporter: new OTLConsoleSpanExporter(),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
otl.start();
|
|
51
|
+
*/
|
|
52
|
+
/*
|
|
53
|
+
OTL.diag.setLogger(new OTL.DiagConsoleLogger(), OTL.DiagLogLevel.DEBUG);
|
|
54
|
+
const tracerProvider = new OTLTrace.NodeTracerProvider();
|
|
55
|
+
const exporter = new OTLTraceBase.ConsoleSpanExporter();
|
|
56
|
+
const processor = new OTLTraceBase.SimpleSpanProcessor(exporter);
|
|
57
|
+
tracerProvider.addSpanProcessor(processor);
|
|
58
|
+
tracerProvider.register();
|
|
59
|
+
|
|
60
|
+
const tracer = OTL.trace.getTracer("esm-tracer");
|
|
61
|
+
tracer.startActiveSpan("manual", (span) => {
|
|
62
|
+
span.addEvent("myEvent");
|
|
63
|
+
span.addEvent("myEvent2");
|
|
64
|
+
span.end();
|
|
65
|
+
});
|
|
66
|
+
*/
|
|
67
|
+
// eslint-disable-next-line unicorn/no-process-exit
|
|
68
|
+
// process.exit(0);
|
|
69
|
+
const myFunction = function (message) {
|
|
70
|
+
// eslint-disable-next-line no-console
|
|
71
|
+
console.log("Executing", message);
|
|
72
|
+
};
|
|
73
|
+
/*
|
|
74
|
+
const myPromise: CleverQueue.Task.FunctionToExecute = function (message: string): Promise<string> {
|
|
75
|
+
console.log(message);
|
|
76
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
77
|
+
console.log(message);
|
|
78
|
+
return message;
|
|
79
|
+
};
|
|
80
|
+
*/
|
|
81
|
+
const myAsync = async function (message) {
|
|
82
|
+
logFunction({ body: "Executing " + message });
|
|
83
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
84
|
+
return message;
|
|
85
|
+
};
|
|
86
|
+
const logFunction = function (logRecord) {
|
|
87
|
+
console.log(JSON.stringify(logRecord));
|
|
88
|
+
};
|
|
89
|
+
async function run() {
|
|
90
|
+
logFunction({ body: "01 - Creating Engine" });
|
|
91
|
+
const engine = CleverQueue.createEngine({ logFunction: logFunction, bestEffortRunners: 2 });
|
|
92
|
+
logFunction({ body: "02 - Creating Queue" });
|
|
93
|
+
const queue = engine.createQueue({ priority: CleverQueue.Queue.Priorities.Standard, weight: 50 });
|
|
94
|
+
logFunction({ body: "03 - Creating Tasks" });
|
|
95
|
+
const task1 = engine.createTask(() => myAsync("myAsync01"), {});
|
|
96
|
+
const task2 = engine.createTask(() => myAsync("myAsync02"), {});
|
|
97
|
+
const task3 = engine.createTask(() => myAsync("myAsync03"), {});
|
|
98
|
+
logFunction({ body: "04 - Enqueue Task" });
|
|
99
|
+
const result = await queue.enqueue(task1);
|
|
100
|
+
queue.enqueue(task2);
|
|
101
|
+
queue.enqueue(task3);
|
|
102
|
+
logFunction({ body: JSON.stringify(engine.statistics()) });
|
|
103
|
+
logFunction({ body: result });
|
|
104
|
+
logFunction({ body: "05 - Stopping Engine" });
|
|
105
|
+
logFunction({ body: JSON.stringify(engine.statistics()) });
|
|
106
|
+
engine.stop();
|
|
107
|
+
logFunction({ body: JSON.stringify(engine.statistics()) });
|
|
108
|
+
while (engine.statistics().engine.tasks > 0) {
|
|
109
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
110
|
+
}
|
|
111
|
+
logFunction({ body: JSON.stringify(engine.statistics()) });
|
|
112
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
113
|
+
logFunction({ body: JSON.stringify(engine.statistics()) });
|
|
114
|
+
logFunction({ body: { task1: task1.result, task2: task2.result, task3: task3.result } });
|
|
115
|
+
}
|
|
116
|
+
run();
|
package/package.json
CHANGED
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "clever-queue",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Queuing system for promises that handle concurring, throttling, weighting and prioritizing in a clever fashion.",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"build": "npx tsc -p src/ --declaration",
|
|
8
|
-
"build:watch": "npx tsc -p src/ -w",
|
|
9
|
-
"prestart": "npx npm run build",
|
|
10
|
-
"start": "node dist/index.js",
|
|
11
|
-
"debug": "node --nolazy --inspect-brk=9229 dist/index.js",
|
|
12
|
-
"test": "node --test ./test/**/*.mjs",
|
|
13
|
-
"test:unit:watch": "node --test --watch ./test/units/*.mjs"
|
|
14
|
-
},
|
|
15
|
-
"repository": {
|
|
16
|
-
"type": "git",
|
|
17
|
-
"url": "git+https://gitlab.com/job-so/clever-queue.git"
|
|
18
|
-
},
|
|
19
|
-
"keywords": [
|
|
20
|
-
"queue",
|
|
21
|
-
"clever",
|
|
22
|
-
"concurrency",
|
|
23
|
-
"throttling",
|
|
24
|
-
"priority",
|
|
25
|
-
"weight",
|
|
26
|
-
"promise"
|
|
27
|
-
],
|
|
28
|
-
"author": "Olivier GROSJEANNE",
|
|
29
|
-
"license": "ISC",
|
|
30
|
-
"bugs": {
|
|
31
|
-
"url": "https://gitlab.com/job-so/clever-queue/issues"
|
|
32
|
-
},
|
|
33
|
-
"homepage": "https://gitlab.com/job-so/clever-queue#readme",
|
|
34
|
-
"devDependencies": {
|
|
35
|
-
"@eslint/eslintrc": "^3.1",
|
|
36
|
-
"@eslint/js": "^9.8",
|
|
37
|
-
"@opentelemetry/api": "^1.9.0",
|
|
38
|
-
"@opentelemetry/sdk-metrics": "^1.26",
|
|
39
|
-
"@opentelemetry/sdk-node": "^0.53",
|
|
40
|
-
"@opentelemetry/sdk-trace-node": "^1.26",
|
|
41
|
-
"@types/eslint__js": "^8.42",
|
|
42
|
-
"@types/node": "^22.1",
|
|
43
|
-
"eslint": "^9.8",
|
|
44
|
-
"eslint-config-prettier": "^9.1",
|
|
45
|
-
"eslint-plugin-node": "^11.1",
|
|
46
|
-
"eslint-plugin-prettier": "^5.1",
|
|
47
|
-
"eslint-plugin-promise": "^6.4",
|
|
48
|
-
"eslint-plugin-security": "^3.0",
|
|
49
|
-
"eslint-plugin-unicorn": "^54.0",
|
|
50
|
-
"globals": "^15.9",
|
|
51
|
-
"node": "^22.4",
|
|
52
|
-
"typescript": "^5.5",
|
|
53
|
-
"typescript-eslint": "^8.0"
|
|
54
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "clever-queue",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Queuing system for promises that handle concurring, throttling, weighting and prioritizing in a clever fashion.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "npx tsc -p src/ --declaration",
|
|
8
|
+
"build:watch": "npx tsc -p src/ -w",
|
|
9
|
+
"prestart": "npx npm run build",
|
|
10
|
+
"start": "node dist/index.js",
|
|
11
|
+
"debug": "node --nolazy --inspect-brk=9229 dist/index.js",
|
|
12
|
+
"test": "node --test ./test/**/*.mjs",
|
|
13
|
+
"test:unit:watch": "node --test --watch ./test/units/*.mjs"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://gitlab.com/job-so/clever-queue.git"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"queue",
|
|
21
|
+
"clever",
|
|
22
|
+
"concurrency",
|
|
23
|
+
"throttling",
|
|
24
|
+
"priority",
|
|
25
|
+
"weight",
|
|
26
|
+
"promise"
|
|
27
|
+
],
|
|
28
|
+
"author": "Olivier GROSJEANNE",
|
|
29
|
+
"license": "ISC",
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://gitlab.com/job-so/clever-queue/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://gitlab.com/job-so/clever-queue#readme",
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@eslint/eslintrc": "^3.1",
|
|
36
|
+
"@eslint/js": "^9.8",
|
|
37
|
+
"@opentelemetry/api": "^1.9.0",
|
|
38
|
+
"@opentelemetry/sdk-metrics": "^1.26",
|
|
39
|
+
"@opentelemetry/sdk-node": "^0.53",
|
|
40
|
+
"@opentelemetry/sdk-trace-node": "^1.26",
|
|
41
|
+
"@types/eslint__js": "^8.42",
|
|
42
|
+
"@types/node": "^22.1",
|
|
43
|
+
"eslint": "^9.8",
|
|
44
|
+
"eslint-config-prettier": "^9.1",
|
|
45
|
+
"eslint-plugin-node": "^11.1",
|
|
46
|
+
"eslint-plugin-prettier": "^5.1",
|
|
47
|
+
"eslint-plugin-promise": "^6.4",
|
|
48
|
+
"eslint-plugin-security": "^3.0",
|
|
49
|
+
"eslint-plugin-unicorn": "^54.0",
|
|
50
|
+
"globals": "^15.9",
|
|
51
|
+
"node": "^22.4",
|
|
52
|
+
"typescript": "^5.5",
|
|
53
|
+
"typescript-eslint": "^8.0"
|
|
54
|
+
}
|
|
55
55
|
}
|
package/src/task/index.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import * as _ from "../helpers";
|
|
4
4
|
import { Options, Statistics, ErrorsList, FunctionToExecute } from "./interfaces";
|
|
5
5
|
|
|
6
|
-
type Status = "unknown" | "initialiazing" | "running" | "stopping" | "destroying";
|
|
6
|
+
type Status = "unknown" | "initialiazing" | "running" | "stopping" | "exception" | "destroying";
|
|
7
7
|
|
|
8
8
|
const defaultOptions: Options = {
|
|
9
9
|
logFunction: undefined,
|
|
@@ -41,10 +41,16 @@ class Task {
|
|
|
41
41
|
this.runner = async (): Promise<void> => {
|
|
42
42
|
this.status = "running";
|
|
43
43
|
// console.log("running Task");
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
try {
|
|
45
|
+
this.result = await this.#functionToExecute();
|
|
46
|
+
// console.log("task runned", this.result);
|
|
47
|
+
this.status = "stopping";
|
|
48
|
+
this.resolver(this.result);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
// console.log("task error", this.result);
|
|
51
|
+
this.status = "exception";
|
|
52
|
+
this.rejecter(new _.Errors.CQError(ErrorsList.FunctionRaisedAnHundledException));
|
|
53
|
+
}
|
|
48
54
|
};
|
|
49
55
|
}
|
|
50
56
|
|
package/src/task/interfaces.ts
CHANGED
|
@@ -12,7 +12,7 @@ interface Statistics {
|
|
|
12
12
|
status: string;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
type ErrorListKeys = "NoOptionsOnTaskInitialization" | "FunctionIsNotAFunction" | "TaskRunnerNotInitialized";
|
|
15
|
+
type ErrorListKeys = "NoOptionsOnTaskInitialization" | "FunctionIsNotAFunction" | "FunctionRaisedAnHundledException" | "TaskRunnerNotInitialized";
|
|
16
16
|
|
|
17
17
|
const ErrorsList: {
|
|
18
18
|
[index in ErrorListKeys]: { name: string; message: string };
|
|
@@ -25,6 +25,10 @@ const ErrorsList: {
|
|
|
25
25
|
name: "FunctionIsNotAFunction",
|
|
26
26
|
message: "FunctionIsNotAFunction",
|
|
27
27
|
},
|
|
28
|
+
FunctionRaisedAnHundledException: {
|
|
29
|
+
name: "FunctionRaisedAnHundledException",
|
|
30
|
+
message: "FunctionRaisedAnHundledException",
|
|
31
|
+
},
|
|
28
32
|
TaskRunnerNotInitialized: {
|
|
29
33
|
name: "TaskRunnerNotInitialized",
|
|
30
34
|
message: "TaskRunnerNotInitialized",
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
|
|
4
|
+
import * as cleverQueue from "../../dist/index.js";
|
|
5
|
+
|
|
6
|
+
const engineOptions = {
|
|
7
|
+
bestEffortRunners: 1,
|
|
8
|
+
logFunction: undefined,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const task = async (string_) => {
|
|
12
|
+
// console.log(string_, "Start", new Date());
|
|
13
|
+
await new Promise((resolve, reject) => setTimeout(reject(string_), 2000));
|
|
14
|
+
// console.log(string_, "End", new Date());
|
|
15
|
+
return string_;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
describe("Issue #1", () => {
|
|
19
|
+
it("Exception if function passed raised an exception", () => {
|
|
20
|
+
const engine = cleverQueue.createEngine(engineOptions);
|
|
21
|
+
const queue1 = engine.createQueue({
|
|
22
|
+
name: "A",
|
|
23
|
+
priority: cleverQueue.Queue.Priorities.Absolute,
|
|
24
|
+
weight: 50,
|
|
25
|
+
});
|
|
26
|
+
assert.rejects(
|
|
27
|
+
async () => {
|
|
28
|
+
const myTask = engine.createTask(() => task("taskMustFail"), {});
|
|
29
|
+
await queue1.enqueue(myTask);
|
|
30
|
+
},
|
|
31
|
+
{ name: "FunctionRaisedAnHundledException" },
|
|
32
|
+
);
|
|
33
|
+
engine.stop();
|
|
34
|
+
});
|
|
35
|
+
});
|