I create a Sails.js server that can use third party code on hook initialization.
I want to use domain to catch error safely but when an error occurred, error is catch correctly but sails server doesn't start with this error :
error: Error: The hook `myhook` is taking too long to load.
Make sure it is triggering its `initialize()` callback, or else set `sails.config.plugins._hookTimeout to a higher value (currently 20000)
at tooLong [as _onTimeout] (/.../node_modules/sails/lib/app/private/loadHooks.js:92:21)
at Timer.listOnTimeout (timers.js:110:15) { [Error: The hook `plugins` is taking too long to load.
Make sure it is triggering its `initialize()` callback, or else set `sails.config.plugins._hookTimeout to a higher value (currently 20000)] code: 'E_HOOK_TIMEOUT' }
Here is my hook code :
module.exports = function (sails)
{
return {
initialize : function (next)
{
var scope = this;
sails.after(["hook:services:loaded", "hook:orm:loaded"], function ()
{
scope.loadPlugins(next);
});
},
loadPlugins : function (next)
{
var fs = require("fs");
var path = require("path");
fs.readdir("plugins", function (err, list)
{
var domain = require('domain').create();
domain.on('error', function (err)
{
// handle the error safely
sails.log.error(err);
sails.log.error(pluginname);
});
for (var i = 0; i < list.length; i++)
{
var pluginname = list[i];
var plugin = require(__dirname + "/../../plugins/" + pluginname + "/" + pluginname + ".js");
sails.services.pluginservice[pluginname] = plugin;
domain.run(function ()
{
if (plugin.init)
{
plugin.init(function (err)
{
if (err)
{
sails.log.error(err);
}
});
}
});
}
if (next)
{
next();
}
});
}
}
}
I put a breakpoint on next method and it's called correctly. But sails doesn't see it and make a timeout.
I use node.js v0.12.2 and sails v0.11.0.