Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: make js-ipfs daemon stop with same SIG as go-ipfs (#1067)
Browse files Browse the repository at this point in the history
* feat: pre-start throws if Adresses.Swarm is missing

* feat: add SIGTERM handler for go-ipfs compatibility

* fix: interupt message

* test: adding for Addresses.Swarm empty

* test: adding missing daemon tests

* fix: exit with 0 on SIGINT & SIGTERM
  • Loading branch information
dryajov authored and daviddias committed Nov 14, 2017
1 parent bb715f9 commit 7dd4e01
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 10 deletions.
10 changes: 7 additions & 3 deletions src/cli/commands/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ module.exports = {
print('Daemon is ready')
})

process.on('SIGINT', () => {
print('Received interrupt signal, shutting down..')
const cleanup = () => {
print(`Received interrupt signal, shutting down..`)
httpAPI.stop((err) => {
if (err) {
throw err
}
process.exit(0)
})
})
}

// listen for graceful termination
process.on('SIGTERM', cleanup)
process.on('SIGINT', cleanup)
}
}
16 changes: 9 additions & 7 deletions src/core/components/pre-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ module.exports = function preStart (self) {
(config, id, cb) => {
self._peerInfo = new PeerInfo(id)

config.Addresses.Swarm.forEach((addr) => {
let ma = multiaddr(addr)
if (config.Addresses && config.Addresses.Swarm) {
config.Addresses.Swarm.forEach((addr) => {
let ma = multiaddr(addr)

if (ma.getPeerId()) {
ma = ma.encapsulate('/ipfs/' + self._peerInfo.id.toB58String())
}
if (ma.getPeerId()) {
ma = ma.encapsulate('/ipfs/' + self._peerInfo.id.toB58String())
}

self._peerInfo.multiaddrs.add(ma)
})
self._peerInfo.multiaddrs.add(ma)
})
}

cb()
}
Expand Down
86 changes: 86 additions & 0 deletions test/cli/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,104 @@
const expect = require('chai').expect
const clean = require('../utils/clean')
const ipfsCmd = require('../utils/ipfs-exec')
const pull = require('pull-stream')
const toPull = require('stream-to-pull-stream')
const os = require('os')
const fs = require('fs')
const path = require('path')

const isWindows = os.platform() === 'win32'

const checkLock = (repo, cb) => {
// skip on windows
// https://github.com/ipfs/js-ipfsd-ctl/pull/155#issuecomment-326983530
if (!isWindows) {
if (fs.existsSync(path.join(repo, 'repo.lock'))) {
cb(new Error('repo.lock not removed'))
}
if (fs.existsSync(path.join(repo, 'api'))) {
cb(new Error('api file not removed'))
}
}
cb()
}

describe('daemon', () => {
let repoPath
let ipfs

const killSig = (sig) => {
let proc = null
return ipfs('init').then(() => {
proc = ipfs('daemon')
return new Promise((resolve, reject) => {
pull(
toPull(proc.stdout),
pull.collect((err, res) => {
expect(err).to.not.exist()
const data = res.toString()
if (data.includes(`Daemon is ready`)) {
if (proc.kill(sig)) {
resolve()
} else {
reject(new Error(`Unable to ${sig} process`))
}
}
})
)

pull(
toPull(proc.stderr),
pull.collect((err, res) => {
expect(err).to.not.exist()
const data = res.toString()
if (data.length > 0) {
reject(new Error(data))
}
})
)
})
})
}

beforeEach(() => {
repoPath = '/tmp/ipfs-test-not-found-' + Math.random().toString().substring(2, 8)
ipfs = ipfsCmd(repoPath)
})

afterEach(() => clean(repoPath))

it(`don't crash if Addresses.Swarm is empty`, function (done) {
this.timeout(20000)
ipfs('init').then(() => {
return ipfs('config', 'Addresses', JSON.stringify({
API: '/ip4/0.0.0.0/tcp/0',
Gateway: '/ip4/0.0.0.0/tcp/0'
}), '--json')
}).then(() => {
return ipfs('daemon')
}).then((res) => {
expect(res).to.have.string('Daemon is ready')
done()
}).catch((err) => {
done(err)
})
})

it(`should handle SIGINT gracefully`, function (done) {
this.timeout(20000)
killSig('SIGINT').then(() => {
checkLock(repoPath, done)
}).catch(done)
})

it(`should handle SIGTERM gracefully`, function (done) {
this.timeout(20000)
killSig('SIGTERM').then(() => {
checkLock(repoPath, done)
}).catch(done)
})

it('gives error if user hasn\'t run init before', (done) => {
const expectedError = 'no initialized ipfs repo found in ' + repoPath

Expand Down

0 comments on commit 7dd4e01

Please sign in to comment.