Skip to content

Commit

Permalink
Add logs if we see ENOSPC error (#1043)
Browse files Browse the repository at this point in the history
* Add logs if we see ENOSPC

* revert

* run these in parallel

* to error

* comment for the function
  • Loading branch information
dmanjunath committed Nov 6, 2020
1 parent 03acf85 commit 8f7e4fb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
14 changes: 13 additions & 1 deletion creator-node/src/fileManager.js
Expand Up @@ -60,7 +60,19 @@ async function saveFileToIPFSFromFS (req, srcPath) {

// store file copy by multihash for future retrieval
const dstPath = path.join(req.app.get('storagePath'), multihash)
fs.copyFileSync(srcPath, dstPath)

try {
fs.copyFileSync(srcPath, dstPath)
} catch (e) {
// if we see a ENOSPC error, log out the disk space and inode details from the system
if (e.message.includes('ENOSPC')) {
await Promise.all([
Utils.runShellCommand(`df`, ['-h'], req.logger),
Utils.runShellCommand(`df`, ['-ih'], req.logger)
])
}
throw e
}

return { multihash, dstPath }
}
Expand Down
28 changes: 28 additions & 0 deletions creator-node/src/utils.js
Expand Up @@ -3,6 +3,7 @@ const fs = require('fs-extra')
const path = require('path')
const { BufferListStream } = require('bl')
const axios = require('axios')
const spawn = require('child_process').spawn

const { logger: genericLogger } = require('./logging')
const models = require('./models')
Expand Down Expand Up @@ -434,6 +435,32 @@ async function writeStreamToFileSystem (stream, expectedStoragePath, createDir =
})
}

/**
* Generic function to run shell commands, eg `ls -alh`
* @param {String} command Command you want to execute from the shell eg `ls`
* @param {Array} args array of string quoted arguments to pass eg ['-alh']
* @param {Object} logger logger object with context
*/
async function runShellCommand (command, args, logger) {
return new Promise((resolve, reject) => {
const proc = spawn(command, args)
let stdout = ''
let stderr = ''
proc.stdout.on('data', (data) => (stdout += data.toString()))
proc.stderr.on('data', (data) => (stderr += data.toString()))

proc.on('close', (code) => {
if (code === 0) {
logger.info(`Successfully executed command ${command} ${args} with output: \n${stdout}`)
resolve()
} else {
logger.error(`Error while executing command ${command} ${args} with stdout: \n${stdout}, \nstderr: \n${stderr}`)
reject(new Error(`Error while executing command ${command} ${args}`))
}
})
})
}

module.exports = Utils
module.exports.validateStateForImageDirCIDAndReturnFileUUID = validateStateForImageDirCIDAndReturnFileUUID
module.exports.getIPFSPeerId = getIPFSPeerId
Expand All @@ -446,3 +473,4 @@ module.exports.ipfsStat = ipfsStat
module.exports.writeStreamToFileSystem = writeStreamToFileSystem
module.exports.getAllRegisteredCNodes = getAllRegisteredCNodes
module.exports.findCIDInNetwork = findCIDInNetwork
module.exports.runShellCommand = runShellCommand

0 comments on commit 8f7e4fb

Please sign in to comment.