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 path = require('path')
|
||||
const fs = require('fs')
|
||||
const pify = require('pify')
|
||||
const fs = pify(require('fs'))
|
||||
const uuid = require('uuid')
|
||||
const _ = require('lodash/fp')
|
||||
const { PerformanceObserver, performance } = require('perf_hooks')
|
||||
|
|
@ -8,23 +9,23 @@ const { PerformanceObserver, performance } = require('perf_hooks')
|
|||
const utils = require('./utils')
|
||||
const variables = require('./utils/variables')
|
||||
|
||||
function getCert (machineIndex) {
|
||||
try {
|
||||
return {
|
||||
key: fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'client.key')),
|
||||
cert: fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'client.pem'))
|
||||
}
|
||||
} catch (e) {
|
||||
var certificate = {}
|
||||
var connectionInfo = {}
|
||||
|
||||
async function getCert (machineIndex) {
|
||||
const key = fs.readFile(path.resolve(__dirname, 'machines', `${machineIndex}`, 'client.key'))
|
||||
const cert = fs.readFile(path.resolve(__dirname, 'machines', `${machineIndex}`, 'client.pem'))
|
||||
|
||||
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
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function connectionInfo (machineIndex) {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'connection_info.json')))
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
async function getConnectionInfo (machineIndex) {
|
||||
return fs.readFile(path.resolve(__dirname, 'machines', `${machineIndex}`, 'connection_info.json'))
|
||||
}
|
||||
|
||||
let counter = 0
|
||||
|
|
@ -38,9 +39,9 @@ function request (machineIndex, pid) {
|
|||
port: 3000,
|
||||
path: '/poll?state=chooseCoin&model=unknown&version=7.5.0-beta.0&idle=true&pid=' + pid + '&sn=' + counter,
|
||||
method: 'GET',
|
||||
key: getCert(machineIndex).key,
|
||||
cert: getCert(machineIndex).cert,
|
||||
ca: connectionInfo(machineIndex).ca,
|
||||
key: certificate.key,
|
||||
cert: certificate.cert,
|
||||
ca: connectionInfo.ca,
|
||||
headers: {
|
||||
date: new Date().toISOString(),
|
||||
'request-id': uuid.v4()
|
||||
|
|
@ -67,6 +68,14 @@ obs.observe({ entryTypes: ['measure'] })
|
|||
process.on('message', async (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)))
|
||||
const pid = uuid.v4()
|
||||
request(msg.machineIndex, pid)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue