0
我正在嘗試編寫一個客戶端,它使用mqtt使用nodejs來安裝和更新軟件包。我測試了數據傳輸和存檔的所有功能,但是當我來到Windows環境並且嘗試運行某些腳本時,我遇到了一些奇怪的錯誤。具有解壓縮內容的節點js spawn/exec的奇怪行爲
我將添加一些代碼,以便任何人都可以看到我如何將下載內容寫入磁盤,但首先我會解釋行爲。從MQTT
- 下載內容(作品)
- 創建緩衝區並從中流(作品)
- 解壓流盤(作品)
- 運行批處理:對各itteration
步驟在下載的內容中工作(奇怪的事情發生)
- 運行另一批作業(更奇怪)
奇怪的事情:首先嚐試spawn/exec respoonses與:error spawn cmd.exe ENOENT
,但在第二次嘗試(由主題訂閱觸發)它在這個階段。但是,當第一批任務通過了第二份工作,在未來itteration返回錯誤:EBUSY: resource busy or locked, open C:\[email protected]\nssm.exe'
其中nssm.exe從批作業
代碼調用下載和提取文件:
const debug = require('debug')('service-manager:functions:download')
const serviceBus = require('../helpers/mqtt-helper.js')
const stream = require('stream')
const unzip = require('unzip-stream')
module.exports = (service, to, from) => {
debug(`Downloading ${service}@${to}`)
return serviceBus.getFromBus(`service_manager/${service}/${to}`)
.then(data => {
debug(`Downloaded ${service}@${to}`)
return new Promise((resolve, reject) => {
debug(`Unzipping ${service}@${to}`)
const buffer = Buffer.from(data)
const bufferStream = new stream.PassThrough()
bufferStream.end(buffer)
bufferStream.pipe(unzip.Extract({
path: `${__dirname}/../../services/active/${service}@${to}`
})).on('finish', resolve)
}).then(() => {
debug(`Unzipped ${service}@${to}`)
})
})
}
代碼用於運行批處理作業:
const debug = require('debug')('service-manager:functions:runner')
const { exec } = require('child_process')
const { resolve } = require('path')
module.exports = (type, reverse) => (service, to, from) => {
return new Promise((resolve, reject) => {
debug(`Running ${type} for ${service}@${reverse ? from : to}`)
const batchFile = resolve(
`../../services/active/${service}@${reverse ? from : to}/${type}.sh`
)
exec(batchFile, (error, stdout, stderr) => {
if (error) {
return reject(error)
}
debug(`Gotten STDOUT on ${type} for ${service}@${reverse ? from : to}: ${stdout}`)
debug(`Gotten STDERR on ${type} for ${service}@${reverse ? from : to}: ${stderr}`)
return resolve()
})
})
}