Skip to content

Commit df59acf

Browse files
committed
feature: support async plugin.stop implementation
Fixes SignalK#1802 Add support for plugins returning a Promise from their stop implementation to allow async stop operations. In plugin restarts, mainly when changing configuration, we wait for the optional stop Promise to resolve before calling plugin.start().
1 parent 31b4a2e commit df59acf

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

docs/src/develop/plugins/server_plugin.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,12 @@ A plugin must return an object containing the following functions:
213213
- `start(settings, restartPlugin)`: This function is called when the plugin is enabled or when the server starts (and the plugin is enabled). The `settings` parameter contains the configuration data entered via the **Plugin Config** screen. `restartPlugin` is a function that can be called by the plugin to restart itself.
214214

215215
- `stop()`: This function is called when the plugin is disabled or after configuration changes. Use this function to "clean up" the resources consumed by the plugin i.e. unsubscribe from streams, stop timers / loops and close devices.
216+
If there are asynchronous operations in your plugin's stop implementation you should return a Promise that resolves
217+
when stopping is complete.
216218

217219
- `schema()`: A function that returns an object defining the schema of the plugin's configuration data. It is used by the server to generate the user interface in the **Plugin Config** screen.
218220

219-
_Note: When a plugin's configuration is changed the server will first call `stop()` to stop the plugin and then `start()` with the new configuration data._
221+
_Note: When a plugin's configuration is changed the server will first call `stop()` to stop the plugin and then `start()` with the new configuration data. Return a Promise from `stop` if needed so that `start` is not called before stopping is complete._
220222

221223
A plugin can also contain the following optional functions:
222224

src/interfaces/plugins.ts

+20-13
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ module.exports = (theApp: any) => {
418418
}
419419
}
420420

421-
function stopPlugin(plugin: PluginInfo) {
421+
function stopPlugin(plugin: PluginInfo): Promise<any> {
422422
debug('Stopping plugin ' + plugin.name)
423423
onStopHandlers[plugin.id].forEach((f: () => void) => {
424424
try {
@@ -428,9 +428,12 @@ module.exports = (theApp: any) => {
428428
}
429429
})
430430
onStopHandlers[plugin.id] = []
431-
plugin.stop()
432-
theApp.setPluginStatus(plugin.id, 'Stopped')
433-
debug('Stopped plugin ' + plugin.name)
431+
const result = Promise.resolve(plugin.stop())
432+
result.then(() => {
433+
theApp.setPluginStatus(plugin.id, 'Stopped')
434+
debug('Stopped plugin ' + plugin.name)
435+
})
436+
return result
434437
}
435438

436439
function setPluginStartedMessage(plugin: PluginInfo) {
@@ -657,8 +660,11 @@ module.exports = (theApp: any) => {
657660
if (err) {
658661
console.error(err)
659662
} else {
660-
stopPlugin(plugin)
661-
doPluginStart(app, plugin, location, newConfiguration, restart)
663+
stopPlugin(plugin).then(() => {
664+
return Promise.resolve(
665+
doPluginStart(app, plugin, location, newConfiguration, restart)
666+
)
667+
})
662668
}
663669
})
664670
}
@@ -713,13 +719,14 @@ module.exports = (theApp: any) => {
713719
return
714720
}
715721
res.json('Saved configuration for plugin ' + plugin.id)
716-
stopPlugin(plugin)
717-
const options = getPluginOptions(plugin.id)
718-
plugin.enableLogging = options.enableLogging
719-
plugin.enableDebug = options.enableDebug
720-
if (options.enabled) {
721-
doPluginStart(app, plugin, location, options.configuration, restart)
722-
}
722+
stopPlugin(plugin).then(() => {
723+
const options = getPluginOptions(plugin.id)
724+
plugin.enableLogging = options.enableLogging
725+
plugin.enableDebug = options.enableDebug
726+
if (options.enabled) {
727+
doPluginStart(app, plugin, location, options.configuration, restart)
728+
}
729+
})
723730
})
724731
})
725732

0 commit comments

Comments
 (0)