fix: synchronous blocking calls
This commit is contained in:
parent
6a5b2a6f8e
commit
2151c4b4aa
1 changed files with 27 additions and 18 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
const https = require('https')
|
const https = require('https')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const fs = require('fs')
|
const pify = require('pify')
|
||||||
|
const fs = pify(require('fs'))
|
||||||
const uuid = require('uuid')
|
const uuid = require('uuid')
|
||||||
const _ = require('lodash/fp')
|
const _ = require('lodash/fp')
|
||||||
const { PerformanceObserver, performance } = require('perf_hooks')
|
const { PerformanceObserver, performance } = require('perf_hooks')
|
||||||
|
|
@ -8,23 +9,23 @@ const { PerformanceObserver, performance } = require('perf_hooks')
|
||||||
const utils = require('./utils')
|
const utils = require('./utils')
|
||||||
const variables = require('./utils/variables')
|
const variables = require('./utils/variables')
|
||||||
|
|
||||||
function getCert (machineIndex) {
|
var certificate = {}
|
||||||
try {
|
var connectionInfo = {}
|
||||||
return {
|
|
||||||
key: fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'client.key')),
|
async function getCert (machineIndex) {
|
||||||
cert: fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'client.pem'))
|
const key = fs.readFile(path.resolve(__dirname, 'machines', `${machineIndex}`, 'client.key'))
|
||||||
}
|
const cert = fs.readFile(path.resolve(__dirname, 'machines', `${machineIndex}`, 'client.pem'))
|
||||||
} catch (e) {
|
|
||||||
|
return Promise.all([key, cert]).then(values => {
|
||||||
|
return { key: values[0], cert: values[1] }
|
||||||
|
}).catch(err => {
|
||||||
|
console.err('The following error when reading the certificate: ', err)
|
||||||
return null
|
return null
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function connectionInfo (machineIndex) {
|
async function getConnectionInfo (machineIndex) {
|
||||||
try {
|
return fs.readFile(path.resolve(__dirname, 'machines', `${machineIndex}`, 'connection_info.json'))
|
||||||
return JSON.parse(fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'connection_info.json')))
|
|
||||||
} catch (e) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let counter = 0
|
let counter = 0
|
||||||
|
|
@ -38,9 +39,9 @@ function request (machineIndex, pid) {
|
||||||
port: 3000,
|
port: 3000,
|
||||||
path: '/poll?state=chooseCoin&model=unknown&version=7.5.0-beta.0&idle=true&pid=' + pid + '&sn=' + counter,
|
path: '/poll?state=chooseCoin&model=unknown&version=7.5.0-beta.0&idle=true&pid=' + pid + '&sn=' + counter,
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
key: getCert(machineIndex).key,
|
key: certificate.key,
|
||||||
cert: getCert(machineIndex).cert,
|
cert: certificate.cert,
|
||||||
ca: connectionInfo(machineIndex).ca,
|
ca: connectionInfo.ca,
|
||||||
headers: {
|
headers: {
|
||||||
date: new Date().toISOString(),
|
date: new Date().toISOString(),
|
||||||
'request-id': uuid.v4()
|
'request-id': uuid.v4()
|
||||||
|
|
@ -67,6 +68,14 @@ obs.observe({ entryTypes: ['measure'] })
|
||||||
process.on('message', async (msg) => {
|
process.on('message', async (msg) => {
|
||||||
console.log('Message from parent:', msg)
|
console.log('Message from parent:', msg)
|
||||||
|
|
||||||
|
const promises = [getCert(msg.machineIndex), getConnectionInfo(msg.machineIndex)]
|
||||||
|
Promise.all(promises).then(values => {
|
||||||
|
certificate = values[0]
|
||||||
|
connectionInfo = JSON.parse(values[1])
|
||||||
|
}).catch(err => {
|
||||||
|
console.err('The following error occurred during certificate parsing: ', err)
|
||||||
|
})
|
||||||
|
|
||||||
if (msg.hasVariance) await new Promise(resolve => setTimeout(resolve, utils.randomIntFromInterval(1, variables.POLLING_INTERVAL)))
|
if (msg.hasVariance) await new Promise(resolve => setTimeout(resolve, utils.randomIntFromInterval(1, variables.POLLING_INTERVAL)))
|
||||||
const pid = uuid.v4()
|
const pid = uuid.v4()
|
||||||
request(msg.machineIndex, pid)
|
request(msg.machineIndex, pid)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue