Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promises4on volumio shutdown&reboot #1373

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1199,13 +1199,27 @@ CoreCommandRouter.prototype.pushAirplay = function (data) {
// Platform specific & Hardware related options, they can be found in platformSpecific.js
// This allows to change system commands across different devices\environments
CoreCommandRouter.prototype.shutdown = function () {
this.pluginManager.onVolumioShutdown();
this.platformspecific.shutdown();
var self = this;

self.pluginManager.onVolumioShutdown().then( function() {
self.platformspecific.shutdown();
}).fail(function(e){
self.logger.info("Error in onVolumioShutdown Plugin Promise handling: "+ e);
self.platformspecific.shutdown();
});

};

CoreCommandRouter.prototype.reboot = function () {
this.pluginManager.onVolumioReboot();
this.platformspecific.reboot();
var self = this;

self.pluginManager.onVolumioReboot().then( function() {
self.platformspecific.reboot();
}).fail(function(e){
self.logger.info("Error in onVolumioReboot Plugin Promise handling: "+ e);
self.platformspecific.reboot();
});

};

CoreCommandRouter.prototype.networkRestart = function () {
Expand Down Expand Up @@ -1887,4 +1901,5 @@ CoreCommandRouter.prototype.deleteMyVolumioDevice = function (device) {
var defer = libQ.defer();

return self.executeOnPlugin('system_controller', 'my_volumio', 'deleteMyVolumioDevice', device);
}
}

85 changes: 79 additions & 6 deletions app/pluginmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,26 +434,99 @@ PluginManager.prototype.getPluginsMatrix = function () {

PluginManager.prototype.onVolumioShutdown = function () {
var self = this;
var defer_onShutdownList=[];

self.logger.info("___________ PLUGINS: Run Shutdown Tasks ___________");

/*
each plugin's onVolumioShutdown() is launched following plugins.json order.
Note: there is no resolution strategy: each plugin completes
at it's own pace, and in whatever order.
Should completion order matter, a new promise strategy should be
implemented below (chain by start order, or else...)
*/

self.plugins.forEach(function (value, key) {
if (self.isEnabled(value.category, value.name)) {
var plugin = value.instance;
if (plugin.onVolumioShutdown !== undefined)
plugin.onVolumioShutdown();
var plugin_defer = self.onVolumioShutdownPlugin(value.category,value.name);
defer_onShutdownList.push(plugin_defer);
}
});

return libQ.all(defer_onShutdownList);
};

PluginManager.prototype.onVolumioShutdownPlugin = function (category, name) {
var self = this;
var defer=libQ.defer();

var plugin = self.getPlugin(category, name);

if(plugin!==undefined)
{
if(plugin.onVolumioShutdown!==undefined)
{
self.logger.info("PLUGIN onShutdown : "+name);
var myPromise = plugin.onVolumioShutdown();

if (Object.prototype.toString.call(myPromise) != Object.prototype.toString.call(libQ.resolve())) {
// Handle non-compliant onVolumioShutdown(): push an error message
// self.coreCommand.pushToastMessage('error',name + " Plugin","This plugin has failing routine on Shutdown. Please install updated version, or contact plugin developper");
self.logger.error("Plugin " + name + " does not return adequate promise from onVolumioShutdown: please update!");
myPromise = libQ.resolve(); // passing a fake promise to avoid crashes in new promise management
}

return myPromise;
}
}

return defer.resolve();
};

PluginManager.prototype.onVolumioReboot = function () {
var self = this;
var defer_onRebootList=[];
self.logger.info("___________ PLUGINS: Run onVolumioReboot Tasks ___________");
/*
each plugin's onVolumioReboot() is launched following plugins.json order.
Note: there is no resolution strategy: each plugin completes
at it's own pace, and in whatever order.
Should completion order matter, a new promise strategy should be
implemented below (chain by start order, or else...)
*/

self.plugins.forEach(function (value, key) {
if (self.isEnabled(value.category, value.name)) {
var plugin = value.instance;
if (plugin.onVolumioReboot !== undefined)
plugin.onVolumioReboot();
var plugin_defer = self.onVolumioRebootPlugin(value.category,value.name);
defer_onRebootList.push(plugin_defer);
}
});

return libQ.all(defer_onRebootList);
};

PluginManager.prototype.onVolumioRebootPlugin = function (category, name) {
var self = this;
var defer=libQ.defer();
var plugin = self.getPlugin(category, name);

if(plugin!==undefined)
{
if(plugin.onVolumioReboot!==undefined)
{
self.logger.info("PLUGIN onReboot : "+name);
var myPromise = plugin.onVolumioReboot();
if (Object.prototype.toString.call(myPromise) != Object.prototype.toString.call(libQ.resolve())) {
// Handle non-compliant onVolumioReboot(): push an error message
// self.coreCommand.pushToastMessage('error',name + " Plugin","This plugin has failing routine on Reboot. Please install updated version, or contact plugin developper");
self.logger.error("Plugin " + name + " does not return adequate promise from onVolumioReboot: please update!");
myPromise = libQ.resolve(); // passing a fake promise to avoid crashes in new promise management
}

return myPromise;
}
}
return defer.resolve();
};

PluginManager.prototype.getPlugin = function (category, name) {
Expand Down