Skip to content

Commit 9038603

Browse files
richardschneiderJonKrone
authored andcommitted
feat: ipfs shutdown (ipfs#1200)
* feat: route to shutdown daemon * feat: cli and http-api shutdown * chore: argggh fix the command count * chore: merge conflicts * chore: rebasing * chore: fix rebase errors * docs: add ipfs.shutdown * fix: always report state error when stopping * chore: documentation is 'stop' not 'shutdown' * fix: generic stop * fix: package.json
1 parent 52be7e4 commit 9038603

File tree

10 files changed

+54
-7
lines changed

10 files changed

+54
-7
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ A complete API definition is in the works. Meanwhile, you can learn how to you u
332332
- `ipfs.ping()`
333333
- `ipfs.init([options], callback)`
334334
- `ipfs.start([callback])`
335-
- `ipfs.stop([callback])`
335+
- [`ipfs.stop([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/MISCELLANEOUS.md#stop)
336336
- `ipfs.isOnline()`
337337

338338
- [config](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/CONFIG.md)

src/cli/commands/shutdown.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
module.exports = {
4+
command: 'shutdown',
5+
6+
describe: 'Shut down the ipfs daemon',
7+
8+
builder: {},
9+
10+
handler (argv) {
11+
argv.ipfs.shutdown((err) => {
12+
if (err) {
13+
throw err
14+
}
15+
})
16+
}
17+
}

src/core/components/stop.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ module.exports = (self) => {
1313
return callback(new Error('Already stopped'))
1414
}
1515

16+
if (self.state.state() !== 'running') {
17+
return callback(new Error('Not able to stop from state: ' + self.state.state()))
18+
}
19+
1620
const done = (err) => {
1721
if (err) {
1822
self.emit('error', err)
@@ -23,10 +27,6 @@ module.exports = (self) => {
2327
callback()
2428
}
2529

26-
if (self.state.state() !== 'running') {
27-
return done(new Error('Not able to stop from state: ' + self.state.state()))
28-
}
29-
3030
self.state.stop()
3131
self._blockService.unsetExchange()
3232
self._bitswap.stop()

src/core/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class IPFS extends EventEmitter {
7777
this.preStart = components.preStart(this)
7878
this.start = components.start(this)
7979
this.stop = components.stop(this)
80+
this.shutdown = this.stop
8081
this.isOnline = components.isOnline(this)
8182
// - interface-ipfs-core defined API
8283
this.version = components.version(this)

src/http/api/resources/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
exports.version = require('./version')
4+
exports.shutdown = require('./shutdown')
45
exports.id = require('./id')
56
exports.bootstrap = require('./bootstrap')
67
exports.repo = require('./repo')

src/http/api/resources/shutdown.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict'
2+
3+
exports = module.exports
4+
5+
/*
6+
* Stop the daemon.
7+
*
8+
* Returns an empty response to the caller then
9+
* on the next 'tick' emits SIGTERM.
10+
*/
11+
exports.do = (request, reply) => {
12+
setImmediate(() => process.emit('SIGTERM'))
13+
return reply()
14+
}

src/http/api/routes/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module.exports = (server) => {
44
require('./version')(server)
5+
require('./shutdown')(server)
56
require('./id')(server)
67
require('./bootstrap')(server)
78
require('./block')(server)

src/http/api/routes/shutdown.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
const resources = require('./../resources')
4+
5+
module.exports = (server) => {
6+
const api = server.select('API')
7+
8+
api.route({
9+
method: '*',
10+
path: '/api/v0/shutdown',
11+
handler: resources.shutdown.do
12+
})
13+
}

test/cli/commands.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const expect = require('chai').expect
55
const runOnAndOff = require('../utils/on-and-off')
66

7-
const commandCount = 67
7+
const commandCount = 68
88
describe('commands', () => runOnAndOff((thing) => {
99
let ipfs
1010

test/core/interface/generic.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const common = {
2626
})
2727
},
2828
teardown: function (callback) {
29-
// Stopped by the tests themselves
29+
// No need to stop, because the test suite does a 'stop' test.
3030
// parallel(nodes.map((node) => (cb) => node.stop(cb)), callback)
3131
callback()
3232
}

0 commit comments

Comments
 (0)