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.
@@ -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 <seconds>', 'run in a loop',0)
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
- const handler = dynamic_handler(options['inbox_handler'],defaultInboxHandler);
10
- return await handler({path,options});
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.info(`[${path}]`);
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
- const loopWait = options['loop'] || 0;
65
+ await poolRun(pool,path,options);
66
+
67
+ if (! options.loop) return;
35
68
 
36
- do {
37
- try {
38
- const [prms,locks] = await inboxProcessor(pool,path,options);
39
- const results = await Promise.all(prms);
69
+ // Loop
40
70
 
41
- for (let i = 0 ; i < results.length ; i++) {
42
- const result = results[i];
43
- const success = result['success'];
44
- const notification = result['path'];
71
+ const watcher = chokidar.watch(path, {
72
+ ignored: /(^|[\/\\])\../, // ignore dotfiles
73
+ persistent: true
74
+ });
45
75
 
46
- if (success) {
47
- logger.info(`processing ${notification} is a success`);
48
- if (fs.existsSync(notification)) {
49
- logger.debug(`removing ${notification}`);
50
- fs.unlinkSync(result['path']);
51
- }
52
- }
53
- else {
54
- logger.warn(`processing ${notification} is a failure`);
55
- logger.debug(`moving ${notification} to ${options['error']}`);
56
- moveTo(notification,options['error']);
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
- // release lock
60
- if (locks[i]) locks[i]();
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
- if (loopWait) {
68
- logger.debug(`sleep ${loopWait} secs...`);
69
- await new Promise(resolve => setTimeout(resolve, loopWait*1000));
113
+ // release lock
114
+ if (locks[i]) locks[i]();
70
115
  }
71
116
  }
72
- while (loopWait > 0);
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.5.4",
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",