ldn-inbox-server 1.5.4 → 1.6.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.
- package/bin/ldn-inbox-server.js +2 -1
- package/lib/handler.js +81 -34
- package/package.json +2 -1
package/bin/ldn-inbox-server.js
CHANGED
|
@@ -89,12 +89,13 @@ program
|
|
|
89
89
|
.option('--outbox <outbox>','outbox',OUTBOX_PATH)
|
|
90
90
|
.option('--public <public>','public',PUBLIC_PATH)
|
|
91
91
|
.option('--error <errbox>','errbox',ERROR_PATH)
|
|
92
|
-
.option('--loop
|
|
92
|
+
.option('--loop', 'run in a loop')
|
|
93
93
|
.option('--batch_size <num>','batch size to process',INBOX_BATCH_SIZE)
|
|
94
94
|
.option('--glob <glob>','files to process in inbox',INBOX_GLOB)
|
|
95
95
|
.option('--config <path>','config file for handlers')
|
|
96
96
|
.option('-hi,--inbox_handler <handler>','inbox handler')
|
|
97
97
|
.option('-hn,--notification_handler <handler>','notification handler')
|
|
98
|
+
.option('-s,--single','handle this one specific notification')
|
|
98
99
|
.argument('<box>','box to process')
|
|
99
100
|
.action( async(box,options) => {
|
|
100
101
|
switch (box) {
|
package/lib/handler.js
CHANGED
|
@@ -3,15 +3,46 @@ const fsPath = require('path');
|
|
|
3
3
|
const lockfile = require('proper-lockfile');
|
|
4
4
|
const { dynamic_handler, moveTo } = require('../lib/util');
|
|
5
5
|
const piscina = require('piscina');
|
|
6
|
+
const chokidar = require('chokidar');
|
|
6
7
|
const logger = require('../lib/util.js').getLogger();
|
|
7
8
|
|
|
8
9
|
async function handle_inbox(path,options) {
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
if (options.single) {
|
|
11
|
+
logger.debug(`*single file execution*`);
|
|
12
|
+
handle_single(path,options);
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
logger.debug(`*directory execution*`);
|
|
16
|
+
const handler = dynamic_handler(options['inbox_handler'],defaultInboxHandler);
|
|
17
|
+
return await handler({path,options});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function handle_single(path,options) {
|
|
22
|
+
const handler = dynamic_handler(
|
|
23
|
+
options['notification_handler'],
|
|
24
|
+
fsPath.resolve(__dirname,'..','lib','demoNotificationHandler.js')
|
|
25
|
+
);
|
|
26
|
+
const result = await handler({path,options});
|
|
27
|
+
const success = result['success'];
|
|
28
|
+
const notification = result['path'];
|
|
29
|
+
|
|
30
|
+
if (success) {
|
|
31
|
+
logger.info(`processing ${notification} is a success`);
|
|
32
|
+
if (fs.existsSync(notification)) {
|
|
33
|
+
logger.debug(`removing ${notification}`);
|
|
34
|
+
fs.unlinkSync(result['path']);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
logger.warn(`processing ${notification} is a failure`);
|
|
39
|
+
logger.debug(`moving ${notification} to ${options['error']}`);
|
|
40
|
+
moveTo(notification,options['error']);
|
|
41
|
+
}
|
|
11
42
|
}
|
|
12
43
|
|
|
13
44
|
async function defaultInboxHandler({path,options}) {
|
|
14
|
-
logger.
|
|
45
|
+
logger.debug(`[${path}]`);
|
|
15
46
|
|
|
16
47
|
const queue_size = options['queue_size'] ?? 'auto';
|
|
17
48
|
|
|
@@ -31,45 +62,61 @@ async function defaultInboxHandler({path,options}) {
|
|
|
31
62
|
maxQueue: queue_size
|
|
32
63
|
});
|
|
33
64
|
|
|
34
|
-
|
|
65
|
+
await poolRun(pool,path,options);
|
|
66
|
+
|
|
67
|
+
if (! options.loop) return;
|
|
35
68
|
|
|
36
|
-
|
|
37
|
-
try {
|
|
38
|
-
const [prms,locks] = await inboxProcessor(pool,path,options);
|
|
39
|
-
const results = await Promise.all(prms);
|
|
69
|
+
// Loop
|
|
40
70
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
71
|
+
const watcher = chokidar.watch(path, {
|
|
72
|
+
ignored: /(^|[\/\\])\../, // ignore dotfiles
|
|
73
|
+
persistent: true
|
|
74
|
+
});
|
|
45
75
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
76
|
+
watcher.on('ready', () => {
|
|
77
|
+
logger.info(`start watching ${path}...`);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
watcher.on('error', error => {
|
|
81
|
+
logger.error(error);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
watcher.on('add', async notification => {
|
|
85
|
+
logger.info(`new notifiction ${notification} detected`);
|
|
86
|
+
await poolRun(pool,path,options);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
58
89
|
|
|
59
|
-
|
|
60
|
-
|
|
90
|
+
async function poolRun(pool,path,options) {
|
|
91
|
+
try {
|
|
92
|
+
const [prms,locks] = await inboxProcessor(pool,path,options);
|
|
93
|
+
const results = await Promise.all(prms);
|
|
94
|
+
|
|
95
|
+
for (let i = 0 ; i < results.length ; i++) {
|
|
96
|
+
const result = results[i];
|
|
97
|
+
const success = result['success'];
|
|
98
|
+
const notification = result['path'];
|
|
99
|
+
|
|
100
|
+
if (success) {
|
|
101
|
+
logger.info(`processing ${notification} is a success`);
|
|
102
|
+
if (fs.existsSync(notification)) {
|
|
103
|
+
logger.debug(`removing ${notification}`);
|
|
104
|
+
fs.unlinkSync(result['path']);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
logger.warn(`processing ${notification} is a failure`);
|
|
109
|
+
logger.debug(`moving ${notification} to ${options['error']}`);
|
|
110
|
+
moveTo(notification,options['error']);
|
|
61
111
|
}
|
|
62
|
-
}
|
|
63
|
-
catch (e) {
|
|
64
|
-
logger.error(e);
|
|
65
|
-
}
|
|
66
112
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
await new Promise(resolve => setTimeout(resolve, loopWait*1000));
|
|
113
|
+
// release lock
|
|
114
|
+
if (locks[i]) locks[i]();
|
|
70
115
|
}
|
|
71
116
|
}
|
|
72
|
-
|
|
117
|
+
catch (e) {
|
|
118
|
+
logger.error(e);
|
|
119
|
+
}
|
|
73
120
|
}
|
|
74
121
|
|
|
75
122
|
async function inboxProcessor(pool,path,options) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ldn-inbox-server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "A demonstration Event Notifications Inbox server",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"author": "Patrick Hochstenbach <Patrick.Hochstenbach@UGent.be>",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
],
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"dependencies": {
|
|
25
|
+
"chokidar": "^3.6.0",
|
|
25
26
|
"commander": "^12.0.0",
|
|
26
27
|
"dotenv": "^16.4.5",
|
|
27
28
|
"exponential-backoff": "^3.1.1",
|