1 | 'use strict'
|
2 |
|
3 | const fs = require('fs')
|
4 |
|
5 |
|
6 | module.exports = function (timeout, callback) {
|
7 | callback = callback.bind(null, null, process.pid, Math.random(), timeout)
|
8 | if (timeout)
|
9 | return setTimeout(callback, timeout)
|
10 | callback()
|
11 | }
|
12 |
|
13 |
|
14 | module.exports.args = function (callback) {
|
15 | console.log(process.argv)
|
16 | console.log(process.execArgv)
|
17 | callback()
|
18 | }
|
19 |
|
20 |
|
21 | module.exports.run0 = function (callback) {
|
22 | module.exports(0, callback)
|
23 | }
|
24 |
|
25 |
|
26 | module.exports.killable = function (id, callback) {
|
27 | if (Math.random() < 0.5)
|
28 | return process.exit(-1)
|
29 | callback(null, id, process.pid)
|
30 | }
|
31 |
|
32 |
|
33 | module.exports.err = function (type, message, data, callback) {
|
34 | if (typeof data == 'function') {
|
35 | callback = data
|
36 | data = null
|
37 | } else {
|
38 | let err = new Error(message)
|
39 | Object.keys(data).forEach(function(key) {
|
40 | err[key] = data[key]
|
41 | })
|
42 | callback(err)
|
43 | return
|
44 | }
|
45 |
|
46 | if (type == 'TypeError')
|
47 | return callback(new TypeError(message))
|
48 | callback(new Error(message))
|
49 | }
|
50 |
|
51 |
|
52 | module.exports.block = function () {
|
53 | while (true);
|
54 | }
|
55 |
|
56 |
|
57 |
|
58 | module.exports.stubborn = function (path, callback) {
|
59 | function isOutdated(path) {
|
60 | return ((new Date).getTime() - fs.statSync(path).mtime.getTime()) > 2000
|
61 | }
|
62 |
|
63 |
|
64 | if (!fs.existsSync(path) || isOutdated(path)) {
|
65 | fs.writeFileSync(path, '1')
|
66 | process.exit(-1)
|
67 | }
|
68 |
|
69 | let retry = parseInt(fs.readFileSync(path, 'utf8'))
|
70 | if (Number.isNaN(retry))
|
71 | return callback(new Error('file contents is not a number'))
|
72 |
|
73 | if (retry > 4) {
|
74 | callback(null, 12)
|
75 | } else {
|
76 | fs.writeFileSync(path, String(retry + 1))
|
77 | process.exit(-1)
|
78 | }
|
79 | }
|
80 |
|
81 |
|
82 | let started = Date.now()
|
83 | module.exports.uptime = function(callback) {
|
84 | callback(null, Date.now() - started)
|
85 | }
|