memory-sri 1.1.12 → 1.2.1

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/README.md CHANGED
@@ -89,33 +89,27 @@ Memory.delall();
89
89
  ### Task
90
90
  #### Task function
91
91
  ```bash
92
- const sayhi = () => {
93
- console.log('hello world !');
94
- }
95
- const test = (a, b) => {
96
- let c = test1(a, b);
97
- let d = test2(a, b);
98
- return c * d;
99
- }
100
- const test1 = (a, b) => {
92
+ const plus = function(a, b){
101
93
  return a + b;
102
94
  }
103
- const test2 = (a, b) => {
104
- return b - a;
105
- }
106
- let tests = {
107
- value: "I Just want to test return data only !"
108
- };
109
- const testJson = (value) => {
110
- return value;
111
- }
112
95
  ```
113
96
 
114
97
  #### Task usage
115
98
  ```bash
116
- Memory.task(sayhi);
117
- Memory.task(test, [2, 5]);
118
- Memory.task(testJson, tests);
99
+ // "test" is name of task
100
+ // { user: 1 } is data send to task
101
+ // retry 3 time, maximum 5 time
102
+ Task.addtask("test", { user: 1 }, 3);
103
+ ```
104
+
105
+ ```bash
106
+ Task.runtask("test", async function () {
107
+ console.log("Running with data:", this.data);
108
+ let a = 1;
109
+ let b = 2;
110
+ let c = plus(a, b);
111
+ console.log(`result: ${c}`);
112
+ });
119
113
  ```
120
114
 
121
115
  ### Key Features:
package/index.d.ts CHANGED
@@ -4,4 +4,5 @@ export function multiple(): any;
4
4
  export function set(identifier: string, data?: any, time?: number): any;
5
5
  export function del(identifier: string): any;
6
6
  export function delall(): any;
7
- export function task(taskFn?: any, args?: any): any;
7
+ export function addtask(name?: any, data?: any, retries?: any): any;
8
+ export function runtask(name?: any, taskFn?: any): any;
package/index.js CHANGED
@@ -1 +1,17 @@
1
- module.exports = require('./lib/main');
1
+ const Task = require("./lib/main");
2
+
3
+ const plus = function (a, b) {
4
+ return a + b;
5
+ };
6
+
7
+ Task.addtask("test", { user: 1 }, 3);
8
+
9
+ Task.runtask("test", async function () {
10
+ console.log("Running with data:", this.data);
11
+ let a = 1;
12
+ let b = 2;
13
+ let c = plus(a, b);
14
+ console.log(`result: ${c}`);
15
+ });
16
+
17
+ module.exports = Task;
package/lib/main.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * module dependencies
3
3
  */
4
4
  const Memory = require("./memory");
5
- const Worker = require("./worker");
5
+ const Task = require("./worker");
6
6
 
7
7
  /**
8
8
  * expose object
@@ -22,11 +22,15 @@ memory.version = "1.1.11";
22
22
  * @param {Object} [data]
23
23
  * @api public
24
24
  */
25
- memory.get = function getSearch(identifier = "a501cf48-9e8a-497e-a158-f87081503425") {
25
+ memory.get = function getSearch(
26
+ identifier = "a501cf48-9e8a-497e-a158-f87081503425"
27
+ ) {
26
28
  return Memory.get(identifier);
27
29
  };
28
30
 
29
- memory.ttl = function getTimeout(identifier = "a501cf48-9e8a-497e-a158-f87081503425") {
31
+ memory.ttl = function getTimeout(
32
+ identifier = "a501cf48-9e8a-497e-a158-f87081503425"
33
+ ) {
30
34
  return Memory.ttl(identifier);
31
35
  };
32
36
 
@@ -40,7 +44,9 @@ memory.set = function setData(identifier, data = [], time = 259200) {
40
44
  return Worker.addTask(setTask(identifier, data, limittime), data);
41
45
  };
42
46
 
43
- memory.del = function delData(identifier = "a501cf48-9e8a-497e-a158-f87081503425") {
47
+ memory.del = function delData(
48
+ identifier = "a501cf48-9e8a-497e-a158-f87081503425"
49
+ ) {
44
50
  return Worker.addTask(delTask(identifier), identifier);
45
51
  };
46
52
 
@@ -55,17 +61,9 @@ memory.delall = function delAllData() {
55
61
  * @param {Object} [args]
56
62
  * @api public
57
63
  */
58
- memory.task = function addTaskWorker(taskFn, args = []) {
59
- if (Array.isArray(args)) {
60
- return Worker.addTask(() => taskFn(...args), args);
61
- } else if (args !== undefined && args !== null) {
62
- return Worker.addTask(() => taskFn(args), args);
63
- } else {
64
- return "No arguments provided for the task";
65
- }
66
- };
67
64
 
68
- memory.isLog = Worker.logTask;
65
+ memory.addtask = Task.addTask;
66
+ memory.runtask = Task.runTask;
69
67
 
70
68
  /**
71
69
  * private util functions
package/lib/worker.js CHANGED
@@ -1,52 +1,70 @@
1
1
  /**
2
2
  * expose object
3
3
  */
4
- let Worker = module.exports;
5
-
6
- const tasks = [];
4
+ let Task = module.exports;
5
+ let tasks = [];
6
+ let isProcessing = false;
7
7
  let taskId = 0;
8
- let isLogging = false;
8
+ let registeredFns = {};
9
+
10
+ /**
11
+ * Add a job into the queue (before the processor is defined)
12
+ */
13
+ Task.addTask = function (name = "temp", data = {}, retries = 1) {
14
+ taskId += 1;
15
+ retries = !retries ? 1 : retries > 5 ? 5 : retries;
16
+
17
+ const task = {
18
+ id: taskId,
19
+ name,
20
+ data,
21
+ attempts: 0,
22
+ maxAttempts: retries,
23
+ };
24
+
25
+ tasks.push(task);
26
+ processTasks();
9
27
 
10
- Worker.logTask = function logTaskService(flag) {
11
- isLogging = !!flag;
12
- return isLogging;
28
+ return { id: task.id };
13
29
  };
14
30
 
15
- /* set task */
16
- Worker.addTask = function addTaskService(taskFn, data) {
31
+ /**
32
+ * Register a processor for a task name
33
+ */
34
+ Task.runTask = function (name, taskFn) {
17
35
  if (typeof taskFn !== "function") {
18
- logData(`Task must be a function`);
19
- return null;
36
+ throw new Error("Task must be a function");
20
37
  }
21
- taskId += 1;
22
- tasks.push({ id: taskId, run: taskFn });
23
- logData(`Task queued with ID: ${taskId}`);
38
+ registeredFns[name] = taskFn;
24
39
  processTasks();
25
- return {
26
- id: taskId,
27
- data: data,
28
- };
29
40
  };
30
41
 
31
42
  /**
32
43
  * private util functions
33
44
  */
34
45
  async function processTasks() {
35
- await new Promise((resolve) => setTimeout(resolve, 1000));
36
- if (tasks.length === 0) return;
46
+ if (isProcessing) return;
47
+ isProcessing = true;
48
+
49
+ while (tasks.length > 0) {
50
+ const task = tasks.shift();
51
+ const fn = registeredFns[task.name];
52
+
53
+ if (!fn) {
54
+ // processor not defined yet → put it back and stop
55
+ tasks.unshift(task);
56
+ break;
57
+ }
37
58
 
38
- const currentTasks = tasks.splice(0, tasks.length);
39
- for (const taskObj of currentTasks) {
40
59
  try {
41
- await taskObj.run();
42
- logData(`Task_id: ${taskObj.id} completed`);
60
+ task.attempts += 1;
61
+ await fn.call({ data: task.data, id: task.id, name: task.name });
43
62
  } catch (err) {
44
- logData(`Task_id: ${taskObj.id} failed`);
45
- logData(err);
63
+ if (task.attempts < task.maxAttempts) {
64
+ tasks.push(task);
65
+ }
46
66
  }
47
67
  }
48
- }
49
68
 
50
- function logData(data) {
51
- if (isLogging) console.log(data);
69
+ isProcessing = false;
52
70
  }
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "memory-sri",
3
- "version": "1.1.12",
3
+ "version": "1.2.1",
4
4
  "description": "A simple and customizable memory data per 1 loadbalance",
5
5
  "scripts": {
6
6
  "test": "npx jest ---coverage=false --detectOpenHandles --forceExit",
7
- "exit": "echo \"No tests specified\" && exit 0"
7
+ "exit": "echo \"No tests specified\" && exit 0",
8
+ "dev": "node index.js"
8
9
  },
9
10
  "author": "Santipap Eiamsamlee",
10
11
  "license": "MIT",