memory-sri 1.2.2 → 1.2.8
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 +66 -21
- package/index.d.ts +1 -1
- package/lib/main.js +6 -6
- package/lib/worker.js +63 -28
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -41,6 +41,7 @@ Memory.set('5', "not set expire");
|
|
|
41
41
|
|
|
42
42
|
#### set
|
|
43
43
|
```bash
|
|
44
|
+
// for set it take job to stored data
|
|
44
45
|
Memory.set('1', "a1", 10);
|
|
45
46
|
Memory.set('2', { b: 2 }, 20);
|
|
46
47
|
Memory.set('3', [ "a1", "a2" ], 30);
|
|
@@ -50,20 +51,23 @@ Memory.set('5', "not set expire");
|
|
|
50
51
|
|
|
51
52
|
#### get
|
|
52
53
|
```bash
|
|
53
|
-
|
|
54
|
-
let
|
|
55
|
-
let
|
|
56
|
-
let
|
|
54
|
+
// for get data realtime
|
|
55
|
+
let get1 = Memory.get(1); // "a1"
|
|
56
|
+
let get2 = Memory.get(2); // { b: 2 }
|
|
57
|
+
let get3 = Memory.get(3); // [ "a1", "a2" ]
|
|
58
|
+
let get4 = Memory.get(4); // [ { a: "a1", b: "b1" } ]
|
|
57
59
|
```
|
|
58
60
|
|
|
59
61
|
#### get timeout
|
|
60
62
|
```bash
|
|
61
|
-
|
|
63
|
+
// for get timeout data realtime
|
|
64
|
+
let ttl1 = Memory.ttl(1); // 10000
|
|
62
65
|
```
|
|
63
66
|
|
|
64
67
|
#### multiple
|
|
65
68
|
```bash
|
|
66
|
-
|
|
69
|
+
// for get data all realtime
|
|
70
|
+
let getall = Memory.multiple();
|
|
67
71
|
```
|
|
68
72
|
|
|
69
73
|
### Output:
|
|
@@ -78,38 +82,79 @@ let test1 = Memory.multiple();
|
|
|
78
82
|
|
|
79
83
|
#### delete
|
|
80
84
|
```bash
|
|
85
|
+
// for delete it take job to delete data from stored
|
|
81
86
|
Memory.del(1);
|
|
82
87
|
```
|
|
83
88
|
|
|
84
89
|
#### delete all
|
|
85
90
|
```bash
|
|
91
|
+
// for delete all it take job to delete all data from stored
|
|
86
92
|
Memory.delall();
|
|
87
93
|
```
|
|
88
94
|
|
|
89
95
|
### Task
|
|
90
|
-
####
|
|
96
|
+
#### Main page for process
|
|
91
97
|
```bash
|
|
92
|
-
|
|
98
|
+
// queue.service.js
|
|
99
|
+
const Memory = require('memory-sri');
|
|
100
|
+
|
|
101
|
+
const plus = function (a, b) {
|
|
93
102
|
return a + b;
|
|
94
|
-
}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
Memory.process('test', async function () {
|
|
106
|
+
console.log(this.data);
|
|
107
|
+
let get3 = Memory.get('3'); // [ 2, 3 ]
|
|
108
|
+
let getttl5 = Memory.ttl('5');
|
|
109
|
+
Memory.del('1');
|
|
110
|
+
let a = get3[0];
|
|
111
|
+
let b = get3[1];
|
|
112
|
+
let c = plus(a, b);
|
|
113
|
+
console.log(`result: ${c}`);
|
|
114
|
+
let getall = Memory.multiple();
|
|
115
|
+
console.log(getall);
|
|
116
|
+
let deleted = Memory.delall();
|
|
117
|
+
return c;
|
|
118
|
+
});
|
|
119
|
+
|
|
95
120
|
```
|
|
96
121
|
|
|
97
122
|
#### Task usage
|
|
98
123
|
```bash
|
|
99
|
-
//
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
```
|
|
124
|
+
// index.js
|
|
125
|
+
import express from 'express';
|
|
126
|
+
require('./queue.service');
|
|
127
|
+
const Memory = require('memory-sri');
|
|
104
128
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
129
|
+
const app = express();
|
|
130
|
+
app.use(express.json());
|
|
131
|
+
|
|
132
|
+
// Retrieve Data from Redis
|
|
133
|
+
app.get('/', async (req, res) => {
|
|
134
|
+
try {
|
|
135
|
+
Memory.set('1', 'a1', 10);
|
|
136
|
+
Memory.set('2', { b: 2 }, 20);
|
|
137
|
+
Memory.set('3', [2, 3], 30);
|
|
138
|
+
Memory.set('4', [{ a: 'a1', b: 'b1' }], 40);
|
|
139
|
+
Memory.set('5', 'not set expire');
|
|
140
|
+
|
|
141
|
+
// "test" is name of task same process
|
|
142
|
+
// { user: 1 } is data send to task
|
|
143
|
+
// retry 3 time, maximum 5 time
|
|
144
|
+
let result = Memory.addtask('test', { user: 1 }, 3);
|
|
145
|
+
res.json({ id: result.id, value: result.data });
|
|
146
|
+
} catch (error) {
|
|
147
|
+
console.error('Retrieve Error:', error);
|
|
148
|
+
res.status(500).json({ message: 'Internal Server Error' });
|
|
149
|
+
}
|
|
112
150
|
});
|
|
151
|
+
|
|
152
|
+
// Start Server
|
|
153
|
+
const PORT = 3000;
|
|
154
|
+
app.listen(PORT, () => {
|
|
155
|
+
console.log(`Server running on http://localhost:${PORT}`);
|
|
156
|
+
});
|
|
157
|
+
|
|
113
158
|
```
|
|
114
159
|
|
|
115
160
|
### Key Features:
|
package/index.d.ts
CHANGED
|
@@ -4,5 +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 process(name?: any, taskFn?: any): any;
|
|
7
8
|
export function addtask(name?: any, data?: any, retries?: any): any;
|
|
8
|
-
export function runtask(name?: any, taskFn?: any): any;
|
package/lib/main.js
CHANGED
|
@@ -12,7 +12,7 @@ let memory = module.exports;
|
|
|
12
12
|
/**
|
|
13
13
|
* version
|
|
14
14
|
*/
|
|
15
|
-
memory.version = "1.
|
|
15
|
+
memory.version = "1.2.8";
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* Memory
|
|
@@ -41,21 +41,21 @@ memory.multiple = function getDataAll() {
|
|
|
41
41
|
memory.set = function setData(identifier, data = [], time = 259200) {
|
|
42
42
|
const MAX_TTL = 604800;
|
|
43
43
|
const limittime = Math.min(time, MAX_TTL);
|
|
44
|
-
return
|
|
44
|
+
return Task.addTaskBE(setTask(identifier, data, limittime), data);
|
|
45
45
|
};
|
|
46
46
|
|
|
47
47
|
memory.del = function delData(
|
|
48
48
|
identifier = "a501cf48-9e8a-497e-a158-f87081503425"
|
|
49
49
|
) {
|
|
50
|
-
return
|
|
50
|
+
return Task.addTaskBE(delTask(identifier), identifier);
|
|
51
51
|
};
|
|
52
52
|
|
|
53
53
|
memory.delall = function delAllData() {
|
|
54
|
-
return
|
|
54
|
+
return Task.addTaskBE(delAllTask(), "Delete All");
|
|
55
55
|
};
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
|
-
*
|
|
58
|
+
* Task
|
|
59
59
|
*
|
|
60
60
|
* @param {Fuction} taskFn
|
|
61
61
|
* @param {Object} [args]
|
|
@@ -63,7 +63,7 @@ memory.delall = function delAllData() {
|
|
|
63
63
|
*/
|
|
64
64
|
|
|
65
65
|
memory.addtask = Task.addTask;
|
|
66
|
-
memory.
|
|
66
|
+
memory.process = Task.process;
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
69
|
* private util functions
|
package/lib/worker.js
CHANGED
|
@@ -3,9 +3,16 @@
|
|
|
3
3
|
*/
|
|
4
4
|
let Task = module.exports;
|
|
5
5
|
let tasks = [];
|
|
6
|
-
let isProcessing = false;
|
|
7
6
|
let taskId = 0;
|
|
8
7
|
let registeredFns = {};
|
|
8
|
+
let tasksBE = [];
|
|
9
|
+
let taskIdBE = 0;
|
|
10
|
+
/**
|
|
11
|
+
* Processor for a task name
|
|
12
|
+
*/
|
|
13
|
+
Task.process = function (name, fn) {
|
|
14
|
+
registeredFns[name] = fn;
|
|
15
|
+
};
|
|
9
16
|
|
|
10
17
|
/**
|
|
11
18
|
* Add a job into the queue (before the processor is defined)
|
|
@@ -13,7 +20,6 @@ let registeredFns = {};
|
|
|
13
20
|
Task.addTask = function (name = "temp", data = {}, retries = 1) {
|
|
14
21
|
taskId += 1;
|
|
15
22
|
retries = !retries ? 1 : retries > 5 ? 5 : retries;
|
|
16
|
-
|
|
17
23
|
const task = {
|
|
18
24
|
id: taskId,
|
|
19
25
|
name,
|
|
@@ -21,50 +27,79 @@ Task.addTask = function (name = "temp", data = {}, retries = 1) {
|
|
|
21
27
|
attempts: 0,
|
|
22
28
|
maxAttempts: retries,
|
|
23
29
|
};
|
|
24
|
-
|
|
25
30
|
tasks.push(task);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return { id: task.id };
|
|
31
|
+
runTask(name);
|
|
32
|
+
return { id: task.id, data: data };
|
|
29
33
|
};
|
|
30
34
|
|
|
31
|
-
|
|
32
|
-
* Register a processor for a task name
|
|
33
|
-
*/
|
|
34
|
-
Task.runTask = function (name, taskFn) {
|
|
35
|
+
Task.addTaskBE = function addTaskService(taskFn, data) {
|
|
35
36
|
if (typeof taskFn !== "function") {
|
|
36
|
-
|
|
37
|
+
logData(`Task must be a function`);
|
|
38
|
+
return null;
|
|
37
39
|
}
|
|
38
|
-
|
|
40
|
+
taskIdBE += 1;
|
|
41
|
+
tasksBE.push({ id: taskIdBE, run: taskFn });
|
|
39
42
|
processTasks();
|
|
43
|
+
return {
|
|
44
|
+
id: taskIdBE,
|
|
45
|
+
data: data,
|
|
46
|
+
};
|
|
40
47
|
};
|
|
41
48
|
|
|
42
49
|
/**
|
|
43
50
|
* private util functions
|
|
44
51
|
*/
|
|
45
|
-
async function
|
|
46
|
-
|
|
47
|
-
|
|
52
|
+
async function runTask(name) {
|
|
53
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
54
|
+
const index = tasks.findIndex((t) => t.name === name);
|
|
55
|
+
/**
|
|
56
|
+
* no tasks found
|
|
57
|
+
*/
|
|
58
|
+
if (index === -1) return null;
|
|
59
|
+
/**
|
|
60
|
+
* remove from queue
|
|
61
|
+
*/
|
|
62
|
+
const task = tasks.splice(index, 1)[0];
|
|
63
|
+
const fn = registeredFns[task.name];
|
|
64
|
+
if (!fn) throw new Error(`No processor registered for task "${task.name}"`);
|
|
48
65
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const
|
|
66
|
+
try {
|
|
67
|
+
task.attempts += 1;
|
|
68
|
+
const result = await fn.call({
|
|
69
|
+
data: task.data,
|
|
70
|
+
id: task.id,
|
|
71
|
+
name: task.name,
|
|
72
|
+
});
|
|
52
73
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
74
|
+
return result;
|
|
75
|
+
} catch (err) {
|
|
76
|
+
if (task.attempts < task.maxAttempts) {
|
|
77
|
+
/**
|
|
78
|
+
* requeue
|
|
79
|
+
*/
|
|
80
|
+
tasks.push(task);
|
|
57
81
|
}
|
|
82
|
+
throw err;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function processTasks() {
|
|
87
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
88
|
+
if (tasksBE.length === 0) return;
|
|
58
89
|
|
|
90
|
+
let attempts = 0;
|
|
91
|
+
const currentTasks = tasksBE.splice(0, tasksBE.length);
|
|
92
|
+
for (const taskObj of currentTasks) {
|
|
59
93
|
try {
|
|
60
|
-
|
|
61
|
-
await
|
|
94
|
+
attempts += 1;
|
|
95
|
+
await taskObj.run();
|
|
62
96
|
} catch (err) {
|
|
63
|
-
if (
|
|
64
|
-
|
|
97
|
+
if (attempts < 3) {
|
|
98
|
+
/**
|
|
99
|
+
* requeue
|
|
100
|
+
*/
|
|
101
|
+
await taskObj.run();
|
|
65
102
|
}
|
|
66
103
|
}
|
|
67
104
|
}
|
|
68
|
-
|
|
69
|
-
isProcessing = false;
|
|
70
105
|
}
|