dcp-worker 3.2.19 → 3.2.21

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.
@@ -12,6 +12,8 @@
12
12
  function main() {
13
13
  const { clearLine, cursorTo } = require('readline');
14
14
  const path = require('path');
15
+ const fs = require('fs');
16
+ const os = require('os');
15
17
  const dcpConfig = require('dcp/dcp-config');
16
18
  const dcpClientDir = path.dirname(require.resolve('dcp-client'));
17
19
  var evaluator;
@@ -29,6 +31,7 @@ function main() {
29
31
  .option('port', {
30
32
  alias: ['p'],
31
33
  desc: 'Port to listen on',
34
+ deprecated: true,
32
35
  default: Number(dcpConfig.evaluator.listen.port),
33
36
  })
34
37
  .option('stdio', {
@@ -47,6 +50,7 @@ function main() {
47
50
  })
48
51
  .option('evaluator-lib-dir', {
49
52
  alias: ['l'],
53
+ deprecated: true,
50
54
  desc: 'Location of evaluator libraries',
51
55
  default: dcpConfig.evaluator.libDir || undefined
52
56
  })
@@ -83,10 +87,16 @@ function main() {
83
87
  alias: ['v'],
84
88
  desc: 'Generate verbose output',
85
89
  })
90
+ .option('pidFileLoc', {
91
+ alias: ['f'],
92
+ desc: 'Location for dcp-evaluator PID file',
93
+ })
86
94
  .options('debugLogLevel', {
87
95
  alias: ['d'],
96
+ deprecated: true,
88
97
  desc: 'Log level for evaluator output (max 8)',
89
98
  })
99
+ .epilogue("To pass arguments to the evaluator, make sure to separate your arguments with a '--'. For example: 'dcp-evaluator-start -p 9000 -- --options=--max-old-space-size=2048'")
90
100
  .wrap(process.stdout.columns || 80)
91
101
  .strict()
92
102
  .argv;
@@ -109,14 +119,19 @@ function main() {
109
119
  let args = []; /* Command-line arguments to pass to evaluator */
110
120
  let files = []; /* Startup files passed to evaluator as supplementary arguments */
111
121
 
112
- if (!options.stdio)
122
+ if (!options.stdio && options.port) {
123
+ console.warn(`Note: --port (-p) is deprecated. Please pass this argument to the evalutor 'dcp-evaluator-start ... -- -p ${options.port}'`);
113
124
  args = args.concat(['-p', options.port]);
125
+ }
114
126
  if (options.evaluatorLibDir) {
127
+ console.warn(`Note: --evaluator-lib-dir (-l) is deprecated. Please pass this argument to the evalutor 'dcp-evaluator-start ... -- -l ${options.evaluatorLibDir}'`);
115
128
  options.evaluatorLibDir = path.resolve(options.prefix, 'etc', options.evaluatorLibDir);
116
129
  args = args.concat(['-l', options.evaluatorLibDir]);
117
130
  }
118
- if (options.debugLogLevel)
131
+ if (options.debugLogLevel){
132
+ console.warn(`Note: --debugLogLevel (-d) is deprecated. Please pass this argument to the evalutor 'dcp-evaluator-start ... -- --debug ${options.debugLogLevel}'`);
119
133
  args = args.concat(['-d'], options.debugLogLevel)
134
+ }
120
135
  if (options._)
121
136
  args = args.concat(options._);
122
137
 
@@ -190,6 +205,9 @@ function main() {
190
205
  console.log('');
191
206
  }
192
207
 
208
+ if (options.pidFileLoc)
209
+ memoizePid(options.pidFileLoc)
210
+
193
211
  if (options.verbose) {
194
212
  console.log('. cwd:', path.join(options.prefix));
195
213
  console.log('. run:', [evaluator].concat(args).concat(files).map(a => `"${a}"`).join(' '));
@@ -229,5 +247,106 @@ if (!localConfig.scheduler)
229
247
  localConfig.bundle.location = false;
230
248
  localConfig.scheduler.configLocation = false;
231
249
  Object.assign(dcpConfig, require('dcp/utils').leafMerge(dcpConfig, localConfig));
250
+
251
+ // Create the PID file for the worker
252
+ function memoizePid(dir)
253
+ {
254
+ const path = require('path');
255
+ const fs = require('fs');
256
+ const program = path.basename(require.main.filename, '.js');
257
+ let location;
258
+ let filename;
259
+ let fileType = '.pid';
260
+
261
+ // Set default location for pid file
262
+ let DEFAULT_PID_LOC;
263
+ if (fs.existsSync('/var/dcp/run'))
264
+ DEFAULT_PID_LOC = '/var/dcp/run/';
265
+ else if (fs.existsSync('/var/run'))
266
+ DEFAULT_PID_LOC = '/var/run/';
267
+ else
268
+ DEFAULT_PID_LOC = require('os').tmpdir();
269
+
270
+ if (dir.length && dir.length > 0)
271
+ {
272
+ if (fs.existsSync(dir))
273
+ {
274
+ if (fs.statSync(dir).isDirectory())
275
+ {
276
+ location = dir;
277
+ filename = program;
278
+ }
279
+ else
280
+ {
281
+ console.warn('Previous PID file was not cleaned up');
282
+ location = path.dirname(dir);
283
+ filename = path.basename(dir);
284
+ fileType = '';
285
+ }
286
+ }
287
+ else if (dir.endsWith(path.sep))
288
+ {
289
+ location = dir;
290
+ filename = program;
291
+ }
292
+ else
293
+ {
294
+ location = path.dirname(dir)
295
+ filename = path.basename(dir);
296
+ fileType = '';
297
+ }
298
+ }
299
+ else
300
+ {
301
+ location = DEFAULT_PID_LOC;
302
+ filename = program;
303
+ }
304
+
305
+ const pidfile = path.join(
306
+ location,
307
+ filename + fileType
308
+ )
309
+ try
310
+ {
311
+ if (fs.existsSync(pidfile))
312
+ {
313
+ const oldPid = fs.readFileSync(pidfile, 'utf8')
314
+ console.warn(`Warning: Previous invocation${oldPid.length ? ' pid#' + parseInt(String(oldPid)) : ''} did not remove ${pidfile}`);
315
+ }
316
+ else
317
+ memoizePid.fd = fs.openSync(pidfile, 'wx');
318
+
319
+ fs.writeSync(memoizePid.fd, Buffer.from(process.pid + '\n'), 0);
320
+ }
321
+ catch (error)
322
+ {
323
+ console.warn(`Warning: Could not create pidfile at ${pidfile} (${error.code || error.message})`);
324
+
325
+ if (typeof memoizePid.fd === 'number')
326
+ {
327
+ fs.closeSync(memoizePid.fd);
328
+ delete memoizePid.fd;
329
+ }
330
+ return;
331
+ }
332
+
333
+ function exitHandler()
334
+ {
335
+ try
336
+ {
337
+ fs.unlinkSync(pidfile);
338
+ fs.closeSync(memoizePid.fd);
339
+ delete memoizePid.fd;
340
+ }
341
+ catch (error)
342
+ {
343
+ console.warn(`Warning: Could not remove pidfile at ${pidfile} (${error.code})`);
344
+ }
345
+ }
346
+
347
+ // Cleanup PID file
348
+ require('dcp/signal-handler').init();
349
+ process.on('dcpExit', exitHandler);
350
+ }
232
351
  main();
233
352
 
package/bin/dcp-worker CHANGED
@@ -15,6 +15,15 @@ const fs = require('fs');
15
15
  // Set to true to try to understand unhandled rejection.
16
16
  const ANALYZE_UNHANDELD_REJECTION = false;
17
17
 
18
+ // Set default location for pid file
19
+ let DEFAULT_PID_LOC;
20
+ if (fs.existsSync('/var/dcp/run'))
21
+ DEFAULT_PID_LOC = '/var/dcp/run/';
22
+ else if (fs.existsSync('/var/run'))
23
+ DEFAULT_PID_LOC = '/var/run/';
24
+ else
25
+ DEFAULT_PID_LOC = os.tmpdir();
26
+
18
27
  const TOTAL_CPU_VCORES = os.cpus().length;
19
28
  const DEFAULT_CORES = TOTAL_CPU_VCORES - 1;
20
29
  var worker, dcpConfig;
@@ -174,7 +183,12 @@ async function main () {
174
183
  describe: 'If set, dump the configuration and exit',
175
184
  type: 'boolean',
176
185
  hidden: true,
177
- }
186
+ },
187
+ pidFileLoc: {
188
+ alias: 'f',
189
+ describe: 'If set, location to generate the worker pid file',
190
+ normalize: true
191
+ },
178
192
  })
179
193
  .strict()
180
194
  .wrap(process.stdout.columns || 80)
@@ -220,6 +234,8 @@ async function startWorking(cliArgs) {
220
234
  else
221
235
  paymentAddress = (await wallet.get()).address;
222
236
 
237
+ if (cliArgs.pidFileLoc)
238
+ memoizePid(cliArgs.pidFileLoc)
223
239
 
224
240
  // Different ways to get the identity:
225
241
  let identityKeystore = false;
@@ -528,6 +544,86 @@ async function startWorking(cliArgs) {
528
544
  return exitcode;
529
545
  }
530
546
 
547
+ // Create the PID file for the worker
548
+ function memoizePid(dir)
549
+ {
550
+ const path = require('path');
551
+ const program = path.basename(require.main.filename, '.js');
552
+ let location;
553
+ let filename;
554
+
555
+ if (dir.length && dir.length > 0)
556
+ {
557
+ location = path.dirname(dir);
558
+ if (fs.existsSync(dir))
559
+ {
560
+ if (fs.statSync(dir).isDirectory())
561
+ filename = program;
562
+ else
563
+ {
564
+ console.warn('Previous PID file was not cleaned up');
565
+ filename = path.basename(dir);
566
+ }
567
+ }
568
+ else if (dir.endsWith(path.sep))
569
+ filename = program;
570
+ else
571
+ filename = path.basename(dir);
572
+ }
573
+ else
574
+ {
575
+ location = DEFAULT_PID_LOC;
576
+ filename = program;
577
+ }
578
+
579
+ const pidfile = path.join(
580
+ location,
581
+ filename + '.pid'
582
+ )
583
+ try
584
+ {
585
+ if (fs.existsSync(pidfile))
586
+ {
587
+ const oldPid = fs.readFileSync(pidfile, 'utf8')
588
+ console.warn(`Warning: Previous invocation${oldPid.length ? ' pid#' + parseInt(String(oldPid)) : ''} did not remove ${pidfile}`);
589
+ }
590
+ else
591
+ memoizePid.fd = fs.openSync(pidfile, 'wx');
592
+
593
+ fs.writeSync(memoizePid.fd, Buffer.from(process.pid + '\n'), 0);
594
+ }
595
+ catch (error)
596
+ {
597
+ console.warn(`Warning: Could not create pidfile at ${pidfile} (${error.code || error.message})`);
598
+
599
+ if (typeof memoizePid.fd === 'number')
600
+ {
601
+ fs.closeSync(memoizePid.fd);
602
+ delete memoizePid.fd;
603
+ }
604
+ return;
605
+ }
606
+
607
+
608
+
609
+ function exitHandler()
610
+ {
611
+ try
612
+ {
613
+ fs.unlinkSync(pidfile);
614
+ fs.closeSync(memoizePid.fd);
615
+ delete memoizePid.fd;
616
+ }
617
+ catch (error)
618
+ {
619
+ console.warn(`Warning: Could not remove pidfile at ${pidfile} (${error.code})`);
620
+ }
621
+ }
622
+
623
+ // Cleanup PID file
624
+ process.on('dcpExit', exitHandler);
625
+ }
626
+
531
627
  /**
532
628
  * Unhandled rejection handler: __must not ever throw no matter what__.
533
629
  * If we hit an unhandled rejection, we are by definition no longer confident of our program state, meaning that
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dcp-worker",
3
- "version": "3.2.19",
3
+ "version": "3.2.21",
4
4
  "description": "JavaScript portion of DCP Workers for Node.js",
5
5
  "main": "bin/dcp-worker",
6
6
  "keywords": [
@@ -36,7 +36,7 @@
36
36
  "blessed-contrib": "^4.11.0",
37
37
  "bravojs": "^1.0.16",
38
38
  "chalk": "^4.1.0",
39
- "dcp-client": "^4.2.24",
39
+ "dcp-client": "^4.2.26",
40
40
  "semver": "^7.3.8"
41
41
  },
42
42
  "optionalDependencies": {