clever-queue 0.1.0

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.
Files changed (69) hide show
  1. package/.gitlab-ci.yml +13 -0
  2. package/.prettierrc.json +4 -0
  3. package/README.md +15 -0
  4. package/dist/engine/index.d.ts +21 -0
  5. package/dist/engine/index.js +187 -0
  6. package/dist/engine/index.js.map +1 -0
  7. package/dist/engine/interfaces.d.ts +28 -0
  8. package/dist/engine/interfaces.js +9 -0
  9. package/dist/engine/interfaces.js.map +1 -0
  10. package/dist/errors.d.ts +13 -0
  11. package/dist/errors.js +42 -0
  12. package/dist/errors.js.map +1 -0
  13. package/dist/helpers/errors.d.ts +7 -0
  14. package/dist/helpers/errors.js +20 -0
  15. package/dist/helpers/errors.js.map +1 -0
  16. package/dist/helpers/index.d.ts +3 -0
  17. package/dist/helpers/index.js +30 -0
  18. package/dist/helpers/index.js.map +1 -0
  19. package/dist/helpers/logs.d.ts +75 -0
  20. package/dist/helpers/logs.js +32 -0
  21. package/dist/helpers/logs.js.map +1 -0
  22. package/dist/helpers/traces.d.ts +1 -0
  23. package/dist/helpers/traces.js +24 -0
  24. package/dist/helpers/traces.js.map +1 -0
  25. package/dist/index.d.ts +7 -0
  26. package/dist/index.js +37 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/queue/index.d.ts +19 -0
  29. package/dist/queue/index.js +110 -0
  30. package/dist/queue/index.js.map +1 -0
  31. package/dist/queue/interfaces.d.ts +27 -0
  32. package/dist/queue/interfaces.js +18 -0
  33. package/dist/queue/interfaces.js.map +1 -0
  34. package/dist/runner/index.d.ts +14 -0
  35. package/dist/runner/index.js +83 -0
  36. package/dist/runner/index.js.map +1 -0
  37. package/dist/runner/interfaces.d.ts +19 -0
  38. package/dist/runner/interfaces.js +13 -0
  39. package/dist/runner/interfaces.js.map +1 -0
  40. package/dist/task/index.d.ts +14 -0
  41. package/dist/task/index.js +81 -0
  42. package/dist/task/index.js.map +1 -0
  43. package/dist/task/interfaces.d.ts +17 -0
  44. package/dist/task/interfaces.js +19 -0
  45. package/dist/task/interfaces.js.map +1 -0
  46. package/eslint.config.mjs +68 -0
  47. package/exemples/index01.js +116 -0
  48. package/exemples/index01.ts +98 -0
  49. package/exemples/index02.mjs +13 -0
  50. package/exemples/tsconfig.json +24 -0
  51. package/package.json +55 -0
  52. package/src/engine/index.ts +166 -0
  53. package/src/engine/interfaces.ts +33 -0
  54. package/src/helpers/errors.ts +16 -0
  55. package/src/helpers/index.ts +3 -0
  56. package/src/helpers/logs.ts +91 -0
  57. package/src/helpers/traces.ts +24 -0
  58. package/src/index.ts +13 -0
  59. package/src/queue/index.ts +93 -0
  60. package/src/queue/interfaces.ts +38 -0
  61. package/src/runner/index.ts +64 -0
  62. package/src/runner/interfaces.ts +24 -0
  63. package/src/task/index.ts +65 -0
  64. package/src/task/interfaces.ts +34 -0
  65. package/src/tsconfig.json +27 -0
  66. package/test/miscellaneous/test.mjs +92 -0
  67. package/test/units/engine.mjs +35 -0
  68. package/test/units/queue.mjs +34 -0
  69. package/test/units/task.mjs +50 -0
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ // clever-queue
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.Queue = exports.Priorities = exports.ErrorsList = void 0;
28
+ const _ = __importStar(require("../helpers"));
29
+ const Task = __importStar(require("../task"));
30
+ const interfaces_1 = require("./interfaces");
31
+ const defaultOptions = {
32
+ autostart: true,
33
+ priority: interfaces_1.Priorities.Standard,
34
+ weight: 128,
35
+ logFunction: undefined,
36
+ };
37
+ class Queue {
38
+ status = "unknown";
39
+ options;
40
+ tick = 0;
41
+ tasks = [];
42
+ constructor(opt) {
43
+ this.status = "initialiazing";
44
+ this.options = this.#checkOptionsConsistancy({ ...defaultOptions, ...opt });
45
+ if (this.options.autostart)
46
+ this.start();
47
+ }
48
+ #checkOptionsConsistancy(options) {
49
+ if (!options)
50
+ throw new _.Errors.CQError(interfaces_1.ErrorsList.NoOptionsOnQueueInitialization);
51
+ if (!(typeof options.priority === "number") || options.priority < 0 || options.priority > 255)
52
+ throw new _.Errors.CQError(interfaces_1.ErrorsList.BadPriorityOptionsOnQueueInitialization, options);
53
+ if (!options.weight)
54
+ throw new _.Errors.CQError(interfaces_1.ErrorsList.NoWeightOptionsOnQueueInitialization);
55
+ return options;
56
+ }
57
+ start() {
58
+ this.status = "running";
59
+ }
60
+ stop() {
61
+ this.status = "stopping";
62
+ }
63
+ /*
64
+ async createTask(function_: Task._function): Promise<unknown> {
65
+ if (typeof function_ !== "function") {
66
+ console.error("raisong Task.ErrorsList.FunctionIsNotAFunction", Task.ErrorsList.FunctionIsNotAFunction);
67
+ throw new _.Errors.CQError(Task.ErrorsList.FunctionIsNotAFunction);
68
+ }
69
+ return new Promise((resolve, reject) => {
70
+ try {
71
+ const taskRunner = async (): Promise<void> => {
72
+ console.log("running Task");
73
+ const result = await function_();
74
+ console.log("task runned", result);
75
+ resolve(result);
76
+ };
77
+ const task = new Task.Task(taskRunner, {});
78
+ this.tasks.push(task);
79
+ } catch (error) {
80
+ reject(error);
81
+ }
82
+ });
83
+ }
84
+ */
85
+ async createTaskAndEnqueue(function_, options) {
86
+ const task = new Task.Task(function_, options);
87
+ return this.enqueue(task);
88
+ }
89
+ async enqueue(task) {
90
+ return new Promise((resolve, reject) => {
91
+ task.resolver = resolve;
92
+ task.rejecter = reject;
93
+ this.tasks.push(task);
94
+ });
95
+ }
96
+ statistics() {
97
+ const stat = {
98
+ status: this.status,
99
+ priority: this.options.priority,
100
+ weight: this.options.weight,
101
+ tasks: this.tasks.length,
102
+ };
103
+ return stat;
104
+ }
105
+ }
106
+ exports.Queue = Queue;
107
+ var interfaces_2 = require("./interfaces");
108
+ Object.defineProperty(exports, "ErrorsList", { enumerable: true, get: function () { return interfaces_2.ErrorsList; } });
109
+ Object.defineProperty(exports, "Priorities", { enumerable: true, get: function () { return interfaces_2.Priorities; } });
110
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/queue/index.ts"],"names":[],"mappings":";AAAA,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;AAEf,8CAAgC;AAChC,8CAAgC;AAChC,6CAA2E;AAM3E,MAAM,cAAc,GAAY;IAC9B,SAAS,EAAE,IAAI;IACf,QAAQ,EAAE,uBAAU,CAAC,QAAQ;IAC7B,MAAM,EAAE,GAAG;IACX,WAAW,EAAE,SAAS;CACvB,CAAC;AAEF,MAAM,KAAK;IACT,MAAM,GAAW,SAAS,CAAC;IAClB,OAAO,CAAU;IAC1B,IAAI,GAAW,CAAC,CAAC;IACjB,KAAK,GAAgB,EAAE,CAAC;IAExB,YAAY,GAAY;QACtB,IAAI,CAAC,MAAM,GAAG,eAAe,CAAC;QAC9B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,wBAAwB,CAAC,EAAE,GAAG,cAAc,EAAE,GAAG,GAAG,EAAE,CAAC,CAAC;QAC5E,IAAI,IAAI,CAAC,OAAO,CAAC,SAAS;YAAE,IAAI,CAAC,KAAK,EAAE,CAAC;IAC3C,CAAC;IAED,wBAAwB,CAAC,OAAgB;QACvC,IAAI,CAAC,OAAO;YAAE,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,uBAAU,CAAC,8BAA8B,CAAC,CAAC;QACpF,IAAI,CAAC,CAAC,OAAO,OAAO,CAAC,QAAQ,KAAK,QAAQ,CAAC,IAAI,OAAO,CAAC,QAAQ,GAAG,CAAC,IAAI,OAAO,CAAC,QAAQ,GAAG,GAAG;YAC3F,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,uBAAU,CAAC,uCAAuC,EAAE,OAAO,CAAC,CAAC;QAC1F,IAAI,CAAC,OAAO,CAAC,MAAM;YAAE,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,uBAAU,CAAC,oCAAoC,CAAC,CAAC;QACjG,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,KAAK;QACH,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;IAC1B,CAAC;IAED,IAAI;QACF,IAAI,CAAC,MAAM,GAAG,UAAU,CAAC;IAC3B,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;IAqBA;IACA,KAAK,CAAC,oBAAoB,CAAC,SAAiC,EAAE,OAAqB;QACjF,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QAC/C,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5B,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,IAAe;QAC3B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;YACxB,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC;YACvB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACxB,CAAC,CAAC,CAAC;IACL,CAAC;IAED,UAAU;QACR,MAAM,IAAI,GAAe;YACvB,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,QAAQ;YAC/B,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM;YAC3B,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM;SACzB,CAAC;QACF,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAGQ,sBAAK;AADd,2CAA2E;AAA7C,wGAAA,UAAU,OAAA;AAAE,wGAAA,UAAU,OAAA"}
@@ -0,0 +1,27 @@
1
+ import * as _ from "../helpers";
2
+ declare const Priorities: {
3
+ Absolute: number;
4
+ Standard: number;
5
+ BestEffort: number;
6
+ };
7
+ interface Options {
8
+ name?: string;
9
+ priority: number;
10
+ weight: number;
11
+ autostart?: boolean;
12
+ logFunction?: _.Logs.LogFunction;
13
+ }
14
+ interface Statistics {
15
+ status: string;
16
+ priority: number;
17
+ weight: number;
18
+ tasks: number;
19
+ }
20
+ type ErrorListKeys = "BadPriorityOptionsOnQueueInitialization" | "NoWeightOptionsOnQueueInitialization" | "NoOptionsOnQueueInitialization";
21
+ declare const ErrorsList: {
22
+ [index in ErrorListKeys]: {
23
+ name: string;
24
+ message: string;
25
+ };
26
+ };
27
+ export { Options, Statistics, ErrorsList, Priorities };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Priorities = exports.ErrorsList = void 0;
4
+ const Priorities = { Absolute: 0, Standard: 128, BestEffort: 255 };
5
+ exports.Priorities = Priorities;
6
+ const ErrorsList = {
7
+ BadPriorityOptionsOnQueueInitialization: {
8
+ name: "BadPriorityOptionsOnQueueInitialization",
9
+ message: "You must provide a 'priority' parameter for Queue initialization from 0 (absolute priority) to 255 (best effort priority), you may use CleverQueue.Priorities constants",
10
+ },
11
+ NoWeightOptionsOnQueueInitialization: {
12
+ name: "NoWeightOptionsOnQueueInitialization",
13
+ message: "You must provide a 'weight' parameter for Queue initialization",
14
+ },
15
+ NoOptionsOnQueueInitialization: { name: "NoOptionsOnQueueInitialization", message: "You must provide an options parameter to Queue" },
16
+ };
17
+ exports.ErrorsList = ErrorsList;
18
+ //# sourceMappingURL=interfaces.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../src/queue/interfaces.ts"],"names":[],"mappings":";;;AAEA,MAAM,UAAU,GAAG,EAAE,QAAQ,EAAE,CAAC,EAAE,QAAQ,EAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,CAAC;AAmCzB,gCAAU;AAhBpD,MAAM,UAAU,GAEZ;IACF,uCAAuC,EAAE;QACvC,IAAI,EAAE,yCAAyC;QAC/C,OAAO,EACL,yKAAyK;KAC5K;IACD,oCAAoC,EAAE;QACpC,IAAI,EAAE,sCAAsC;QAC5C,OAAO,EAAE,gEAAgE;KAC1E;IAED,8BAA8B,EAAE,EAAE,IAAI,EAAE,gCAAgC,EAAE,OAAO,EAAE,gDAAgD,EAAE;CACtI,CAAC;AAE4B,gCAAU"}
@@ -0,0 +1,14 @@
1
+ import { Options, Statistics } from "./interfaces";
2
+ import { Engine } from "../engine";
3
+ export * from "./interfaces";
4
+ type Status = "unknown" | "initialiazing" | "running" | "stopping" | "destroying";
5
+ declare class Runner {
6
+ #private;
7
+ status: Status;
8
+ constructor(opt: Options, engine: Engine);
9
+ start(): void;
10
+ stop(): void;
11
+ statistics(): Statistics;
12
+ }
13
+ export { Options, Statistics, ErrorsList } from "./interfaces";
14
+ export { Runner };
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ // clever-queue
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
26
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
27
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
28
+ };
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.Runner = exports.ErrorsList = void 0;
31
+ const _ = __importStar(require("../helpers"));
32
+ const interfaces_1 = require("./interfaces");
33
+ __exportStar(require("./interfaces"), exports);
34
+ const defaultOptions = {
35
+ priority: interfaces_1.Priorities.BestEffort,
36
+ autostart: true,
37
+ logFunction: undefined,
38
+ };
39
+ class Runner {
40
+ status = "unknown";
41
+ #options;
42
+ #engine;
43
+ constructor(opt, engine) {
44
+ this.status = "initialiazing";
45
+ this.#options = this.#checkOptionsConsistancy({ ...defaultOptions, ...opt });
46
+ this.#engine = engine; // Store parent engine for later callbacks
47
+ if (this.#options.autostart)
48
+ this.start();
49
+ }
50
+ #checkOptionsConsistancy(options) {
51
+ if (!options)
52
+ throw new _.Errors.CQError(interfaces_1.ErrorsList.NoOptionsOnRunnerInitialization);
53
+ return options;
54
+ }
55
+ async #run() {
56
+ while (this.status === "running") {
57
+ await new Promise((resolve) => setTimeout(resolve, 10)); // Do not freeze the event loop with infinite loop.
58
+ // get the next task to run according to this runner priority
59
+ const task = await this.#engine.getNextTask(this.#options.priority);
60
+ if (task) {
61
+ await task.runner();
62
+ }
63
+ }
64
+ }
65
+ start() {
66
+ this.status = "running";
67
+ this.#run();
68
+ }
69
+ stop() {
70
+ this.status = "stopping";
71
+ }
72
+ statistics() {
73
+ const stat = {
74
+ status: this.status,
75
+ priority: this.#options.priority,
76
+ };
77
+ return stat;
78
+ }
79
+ }
80
+ exports.Runner = Runner;
81
+ var interfaces_2 = require("./interfaces");
82
+ Object.defineProperty(exports, "ErrorsList", { enumerable: true, get: function () { return interfaces_2.ErrorsList; } });
83
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/runner/index.ts"],"names":[],"mappings":";AAAA,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEf,8CAAgC;AAChC,6CAA2E;AAG3E,+CAA6B;AAI7B,MAAM,cAAc,GAAY;IAC9B,QAAQ,EAAE,uBAAU,CAAC,UAAU;IAC/B,SAAS,EAAE,IAAI;IACf,WAAW,EAAE,SAAS;CACvB,CAAC;AAEF,MAAM,MAAM;IACV,MAAM,GAAW,SAAS,CAAC;IAClB,QAAQ,CAAU;IAClB,OAAO,CAAS;IAEzB,YAAY,GAAY,EAAE,MAAc;QACtC,IAAI,CAAC,MAAM,GAAG,eAAe,CAAC;QAC9B,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,wBAAwB,CAAC,EAAE,GAAG,cAAc,EAAE,GAAG,GAAG,EAAE,CAAC,CAAC;QAC7E,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC,0CAA0C;QACjE,IAAI,IAAI,CAAC,QAAQ,CAAC,SAAS;YAAE,IAAI,CAAC,KAAK,EAAE,CAAC;IAC5C,CAAC;IAED,wBAAwB,CAAC,OAAgB;QACvC,IAAI,CAAC,OAAO;YAAE,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,uBAAU,CAAC,+BAA+B,CAAC,CAAC;QACrF,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,KAAK,CAAC,IAAI;QACR,OAAO,IAAI,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;YACjC,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,mDAAmD;YAC5G,6DAA6D;YAC7D,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;YACpE,IAAI,IAAI,EAAE,CAAC;gBACT,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC;YACtB,CAAC;QACH,CAAC;IACH,CAAC;IAED,KAAK;QACH,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;QACxB,IAAI,CAAC,IAAI,EAAE,CAAC;IACd,CAAC;IAED,IAAI;QACF,IAAI,CAAC,MAAM,GAAG,UAAU,CAAC;IAC3B,CAAC;IAED,UAAU;QACR,MAAM,IAAI,GAAe;YACvB,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ;SACjC,CAAC;QACF,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAGQ,wBAAM;AADf,2CAA+D;AAAjC,wGAAA,UAAU,OAAA"}
@@ -0,0 +1,19 @@
1
+ import * as _ from "../helpers";
2
+ interface Options {
3
+ autostart?: boolean;
4
+ priority: number;
5
+ logFunction?: _.Logs.LogFunction;
6
+ }
7
+ interface Statistics {
8
+ status: string;
9
+ priority: number;
10
+ }
11
+ type ErrorListKeys = "NoOptionsOnRunnerInitialization";
12
+ declare const ErrorsList: {
13
+ [index in ErrorListKeys]: {
14
+ name: string;
15
+ message: string;
16
+ };
17
+ };
18
+ export { Priorities } from "../queue/interfaces";
19
+ export { Options, Statistics, ErrorsList };
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ErrorsList = exports.Priorities = void 0;
4
+ const ErrorsList = {
5
+ NoOptionsOnRunnerInitialization: {
6
+ name: "NoOptionsOnRunnerInitialization",
7
+ message: "NoOptionsOnRunnerInitialization",
8
+ },
9
+ };
10
+ exports.ErrorsList = ErrorsList;
11
+ var interfaces_1 = require("../queue/interfaces");
12
+ Object.defineProperty(exports, "Priorities", { enumerable: true, get: function () { return interfaces_1.Priorities; } });
13
+ //# sourceMappingURL=interfaces.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../src/runner/interfaces.ts"],"names":[],"mappings":";;;AAaA,MAAM,UAAU,GAEZ;IACF,+BAA+B,EAAE;QAC/B,IAAI,EAAE,iCAAiC;QACvC,OAAO,EAAE,iCAAiC;KAC3C;CACF,CAAC;AAG4B,gCAAU;AADxC,kDAAiD;AAAxC,wGAAA,UAAU,OAAA"}
@@ -0,0 +1,14 @@
1
+ import { Options, Statistics, FunctionToExecute } from "./interfaces";
2
+ type Status = "unknown" | "initialiazing" | "running" | "stopping" | "destroying";
3
+ declare class Task {
4
+ #private;
5
+ status: Status;
6
+ result: unknown | undefined;
7
+ readonly runner: () => Promise<unknown>;
8
+ resolver: (value: any | PromiseLike<any>) => void;
9
+ rejecter: (reason?: any) => void;
10
+ constructor(function_: FunctionToExecute, opt: Options);
11
+ statistics(): Statistics;
12
+ }
13
+ export { Options, Statistics, ErrorsList, FunctionToExecute } from "./interfaces";
14
+ export { Task };
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ // clever-queue
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.Task = exports.ErrorsList = void 0;
28
+ const _ = __importStar(require("../helpers"));
29
+ const interfaces_1 = require("./interfaces");
30
+ const defaultOptions = {
31
+ logFunction: undefined,
32
+ };
33
+ class Task {
34
+ status = "unknown";
35
+ #options;
36
+ #functionToExecute;
37
+ result = undefined;
38
+ runner;
39
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
40
+ resolver;
41
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
42
+ rejecter;
43
+ #defaultResolver() {
44
+ throw new _.Errors.CQError(interfaces_1.ErrorsList.TaskRunnerNotInitialized);
45
+ }
46
+ #defaultRejecter() {
47
+ throw new _.Errors.CQError(interfaces_1.ErrorsList.TaskRunnerNotInitialized);
48
+ }
49
+ constructor(function_, opt) {
50
+ this.status = "initialiazing";
51
+ this.#options = this.#checkOptionsConsistancy({ ...defaultOptions, ...opt });
52
+ if (typeof function_ !== "function")
53
+ throw new _.Errors.CQError(interfaces_1.ErrorsList.FunctionIsNotAFunction);
54
+ this.resolver = this.#defaultResolver;
55
+ this.rejecter = this.#defaultRejecter;
56
+ this.#functionToExecute = function_;
57
+ this.runner = async () => {
58
+ this.status = "running";
59
+ // console.log("running Task");
60
+ this.result = await this.#functionToExecute();
61
+ // console.log("task runned", this.result);
62
+ this.status = "stopping";
63
+ this.resolver(this.result);
64
+ };
65
+ }
66
+ #checkOptionsConsistancy(options) {
67
+ if (!options)
68
+ throw new _.Errors.CQError(interfaces_1.ErrorsList.NoOptionsOnTaskInitialization);
69
+ return options;
70
+ }
71
+ statistics() {
72
+ const stat = {
73
+ status: this.status,
74
+ };
75
+ return stat;
76
+ }
77
+ }
78
+ exports.Task = Task;
79
+ var interfaces_2 = require("./interfaces");
80
+ Object.defineProperty(exports, "ErrorsList", { enumerable: true, get: function () { return interfaces_2.ErrorsList; } });
81
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/task/index.ts"],"names":[],"mappings":";AAAA,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;AAEf,8CAAgC;AAChC,6CAAkF;AAIlF,MAAM,cAAc,GAAY;IAC9B,WAAW,EAAE,SAAS;CACvB,CAAC;AAEF,MAAM,IAAI;IACR,MAAM,GAAW,SAAS,CAAC;IAClB,QAAQ,CAAU;IAC3B,kBAAkB,CAAoB;IACtC,MAAM,GAAwB,SAAS,CAAC;IAC/B,MAAM,CAAyB;IAExC,8DAA8D;IAC9D,QAAQ,CAA0C;IAClD,8DAA8D;IAC9D,QAAQ,CAAyB;IAEjC,gBAAgB;QACd,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,uBAAU,CAAC,wBAAwB,CAAC,CAAC;IAClE,CAAC;IACD,gBAAgB;QACd,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,uBAAU,CAAC,wBAAwB,CAAC,CAAC;IAClE,CAAC;IAED,YAAY,SAA4B,EAAE,GAAY;QACpD,IAAI,CAAC,MAAM,GAAG,eAAe,CAAC;QAC9B,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,wBAAwB,CAAC,EAAE,GAAG,cAAc,EAAE,GAAG,GAAG,EAAE,CAAC,CAAC;QAE7E,IAAI,OAAO,SAAS,KAAK,UAAU;YAAE,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,uBAAU,CAAC,sBAAsB,CAAC,CAAC;QAEnG,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,gBAAgB,CAAC;QACtC,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,gBAAgB,CAAC;QACtC,IAAI,CAAC,kBAAkB,GAAG,SAAS,CAAC;QAEpC,IAAI,CAAC,MAAM,GAAG,KAAK,IAAmB,EAAE;YACtC,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;YACxB,+BAA+B;YAC/B,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,CAAC,kBAAkB,EAAE,CAAC;YAC9C,2CAA2C;YAC3C,IAAI,CAAC,MAAM,GAAG,UAAU,CAAC;YACzB,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC7B,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"}
@@ -0,0 +1,17 @@
1
+ import * as _ from "../helpers";
2
+ type FunctionToExecute = (...arguments_: any[]) => Promise<any> | void;
3
+ interface Options {
4
+ timeout?: number;
5
+ logFunction?: _.Logs.LogFunction;
6
+ }
7
+ interface Statistics {
8
+ status: string;
9
+ }
10
+ type ErrorListKeys = "NoOptionsOnTaskInitialization" | "FunctionIsNotAFunction" | "TaskRunnerNotInitialized";
11
+ declare const ErrorsList: {
12
+ [index in ErrorListKeys]: {
13
+ name: string;
14
+ message: string;
15
+ };
16
+ };
17
+ export { Options, Statistics, ErrorsList, FunctionToExecute };
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ErrorsList = void 0;
4
+ const ErrorsList = {
5
+ NoOptionsOnTaskInitialization: {
6
+ name: "NoOptionsOnTaskInitialization",
7
+ message: "NoOptionsOnTaskInitialization",
8
+ },
9
+ FunctionIsNotAFunction: {
10
+ name: "FunctionIsNotAFunction",
11
+ message: "FunctionIsNotAFunction",
12
+ },
13
+ TaskRunnerNotInitialized: {
14
+ name: "TaskRunnerNotInitialized",
15
+ message: "TaskRunnerNotInitialized",
16
+ },
17
+ };
18
+ exports.ErrorsList = ErrorsList;
19
+ //# sourceMappingURL=interfaces.js.map
@@ -0,0 +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"}
@@ -0,0 +1,68 @@
1
+ import eslint from "@eslint/js";
2
+ import tseslint from "typescript-eslint";
3
+ import globals from "globals";
4
+
5
+ import security from "eslint-plugin-security";
6
+ import unicorn from "eslint-plugin-unicorn";
7
+ import promise from "eslint-plugin-promise";
8
+ import prettierRecommended from "eslint-plugin-prettier/recommended";
9
+
10
+ import tsParser from "@typescript-eslint/parser";
11
+
12
+ export default [
13
+ eslint.configs.recommended,
14
+ ...tseslint.configs.recommended,
15
+ security.configs.recommended,
16
+ promise.configs["flat/recommended"],
17
+ unicorn.configs["flat/recommended"],
18
+ prettierRecommended,
19
+ {
20
+ languageOptions: {
21
+ parser: tsParser,
22
+ ecmaVersion: 2020,
23
+ sourceType: "module",
24
+ globals: {
25
+ ...globals.node,
26
+ },
27
+ },
28
+ rules: {
29
+ "no-console": "warn",
30
+
31
+ "max-len": [
32
+ "error",
33
+ {
34
+ code: 180,
35
+ ignoreUrls: true,
36
+ },
37
+ ],
38
+
39
+ "max-lines-per-function": [
40
+ "warn",
41
+ {
42
+ max: 75,
43
+ skipBlankLines: true,
44
+ skipComments: true,
45
+ },
46
+ ],
47
+
48
+ "@typescript-eslint/no-explicit-any": "warn",
49
+ "@typescript-eslint/explicit-function-return-type": "warn",
50
+ "unicorn/filename-case": "off",
51
+ "unicorn/prevent-abbreviations": "warn",
52
+ "security/detect-object-injection": "off",
53
+ "security/detect-non-literal-fs-filename": "off",
54
+ "promise/always-return": "warn",
55
+ "promise/no-return-wrap": "warn",
56
+ "promise/param-names": "warn",
57
+ "promise/catch-or-return": "warn",
58
+ "promise/no-native": "off",
59
+ "promise/no-nesting": "warn",
60
+ "promise/no-promise-in-callback": "warn",
61
+ "promise/no-callback-in-promise": "warn",
62
+ "promise/avoid-new": "off",
63
+ "promise/no-new-statics": "warn",
64
+ "promise/no-return-in-finally": "warn",
65
+ "promise/valid-params": "warn",
66
+ },
67
+ },
68
+ ];
@@ -0,0 +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();