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

Commit

Permalink
feat: ipfs shutdown (#1200)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
richardschneider authored and daviddias committed Feb 15, 2018
1 parent 9a445d1 commit 95365fa
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ A complete API definition is in the works. Meanwhile, you can learn how to you u
- `ipfs.ping()`
- `ipfs.init([options], callback)`
- `ipfs.start([callback])`
- `ipfs.stop([callback])`
- [`ipfs.stop([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/MISCELLANEOUS.md#stop)
- `ipfs.isOnline()`

- [config](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/CONFIG.md)
Expand Down
17 changes: 17 additions & 0 deletions src/cli/commands/shutdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

module.exports = {
command: 'shutdown',

describe: 'Shut down the ipfs daemon',

builder: {},

handler (argv) {
argv.ipfs.shutdown((err) => {
if (err) {
throw err
}
})
}
}
8 changes: 4 additions & 4 deletions src/core/components/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ module.exports = (self) => {
return callback(new Error('Already stopped'))
}

if (self.state.state() !== 'running') {
return callback(new Error('Not able to stop from state: ' + self.state.state()))
}

const done = (err) => {
if (err) {
self.emit('error', err)
Expand All @@ -23,10 +27,6 @@ module.exports = (self) => {
callback()
}

if (self.state.state() !== 'running') {
return done(new Error('Not able to stop from state: ' + self.state.state()))
}

self.state.stop()
self._blockService.unsetExchange()
self._bitswap.stop()
Expand Down
1 change: 1 addition & 0 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class IPFS extends EventEmitter {
this.preStart = components.preStart(this)
this.start = components.start(this)
this.stop = components.stop(this)
this.shutdown = this.stop
this.isOnline = components.isOnline(this)
// - interface-ipfs-core defined API
this.version = components.version(this)
Expand Down
1 change: 1 addition & 0 deletions src/http/api/resources/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

exports.version = require('./version')
exports.shutdown = require('./shutdown')
exports.id = require('./id')
exports.bootstrap = require('./bootstrap')
exports.repo = require('./repo')
Expand Down
14 changes: 14 additions & 0 deletions src/http/api/resources/shutdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

exports = module.exports

/*
* Stop the daemon.
*
* Returns an empty response to the caller then
* on the next 'tick' emits SIGTERM.
*/
exports.do = (request, reply) => {
setImmediate(() => process.emit('SIGTERM'))
return reply()
}
1 change: 1 addition & 0 deletions src/http/api/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module.exports = (server) => {
require('./version')(server)
require('./shutdown')(server)
require('./id')(server)
require('./bootstrap')(server)
require('./block')(server)
Expand Down
13 changes: 13 additions & 0 deletions src/http/api/routes/shutdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

const resources = require('./../resources')

module.exports = (server) => {
const api = server.select('API')

api.route({
method: '*',
path: '/api/v0/shutdown',
handler: resources.shutdown.do
})
}
2 changes: 1 addition & 1 deletion test/cli/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const expect = require('chai').expect
const runOnAndOff = require('../utils/on-and-off')

const commandCount = 67
const commandCount = 68
describe('commands', () => runOnAndOff((thing) => {
let ipfs

Expand Down
2 changes: 1 addition & 1 deletion test/core/interface/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const common = {
})
},
teardown: function (callback) {
// Stopped by the tests themselves
// No need to stop, because the test suite does a 'stop' test.
// parallel(nodes.map((node) => (cb) => node.stop(cb)), callback)
callback()
}
Expand Down

0 comments on commit 95365fa

Please sign in to comment.