Skip to content

Commit

Permalink
Add timeout for starting IPFS and better error handling (#211)
Browse files Browse the repository at this point in the history
* Add timeout for starting IPFS and better error handling

* Always migrate on IPFS daemon start to avoid user prompt
  • Loading branch information
izqui authored and 0xGabi committed Feb 25, 2019
1 parent 2575e75 commit 5f4776c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
21 changes: 13 additions & 8 deletions packages/aragon-cli/src/commands/ipfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,17 @@ exports.task = ({ apmOptions }) => {
exports.handler = function ({ reporter, apm: apmOptions }) {
const task = exports.task({ apmOptions })

task.run().then(ctx => {
if (ctx.started) {
reporter.info('IPFS daemon is now running. Stopping this process will stop IPFS')
} else {
reporter.warning('Didnt start IPFS, port busy')
process.exit()
}
})
task.run()
.then(ctx => {
if (ctx.started) {
reporter.info('IPFS daemon is now running. Stopping this process will stop IPFS')
} else {
reporter.warning('Didnt start IPFS, port busy')
process.exit()
}
})
.catch(error => {
reporter.error(error.message)
process.exit()
})
}
28 changes: 24 additions & 4 deletions packages/aragon-cli/src/helpers/ipfs-daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,39 @@ const ipfsBin = getNPMBinary('go-ipfs', 'bin/ipfs')

const ensureIPFSInitialized = async () => {
if (!fs.existsSync(path.join(os.homedir(), '.ipfs'))) {
// We could use 'ipfs daemon --init' when https://github.com/ipfs/go-ipfs/issues/3913 is solved
await execa(ipfsBin, ['init'])
}
}

const startIPFSDaemon = () => (
new Promise(async (resolve) => {
const startIPFSDaemon = () => {
const IPFS_START_TIMEOUT = 7500 // 7.5s for timeout, may need to be tweaked

let startOutput = ''

// We add a timeout as starting
const timeout = new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error(`Starting IPFS timed out:\n${startOutput}`))
}, IPFS_START_TIMEOUT)
})

const start = new Promise(async (resolve, reject) => {
await ensureIPFSInitialized()
const ipfsProc = execa(ipfsBin, ['daemon'])
const ipfsProc = execa(ipfsBin, ['daemon', '--migrate'])

ipfsProc.stdout.on('data', (data) => {
startOutput = `${startOutput}${data.toString()}\n`
if (data.toString().includes('Daemon is ready')) resolve()
})

ipfsProc.stderr.on('data', (data) => {
reject(new Error(`Starting IPFS failed: ${data.toString()}`))
})
})
)

return Promise.race([start, timeout])
}

let ipfsNode

Expand Down

0 comments on commit 5f4776c

Please sign in to comment.