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

Execution timeout #100

Merged
merged 2 commits into from
Sep 1, 2015
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
1 change: 1 addition & 0 deletions docs/registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ registry.register({
|`dependencies`|`array`|the npm modules available for components logic|
|`env`|`object`|sets the registry environment|
|`env.name`|`string`|sets the environment name|
|`executionTimeout`|`number` (seconds)|timeout for component's server execution time|
|`pollingInterval`|`number` (seconds)|When the components' list cache will be refreshed. This is required for distributing the components on each registry instance. Given the polling mechanism is quite efficient, this number should be very low. Suggested is around 5-10 seconds.|
|`port`|`number`|default `3000`, sets the port where to start the registry|
|`prefix`|`string`|sets the href prefix, for example: `/v2/`|
Expand Down
22 changes: 21 additions & 1 deletion registry/routes/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ module.exports = function(conf, repository){
parameters: req.query
};

var conf = res.conf;
var conf = res.conf,
componentCallbackDone = false;

repository.getComponent(requestedComponent.name, requestedComponent.version, function(err, component){

Expand Down Expand Up @@ -65,6 +66,10 @@ module.exports = function(conf, repository){
}

var returnComponent = function(err, data){

if(componentCallbackDone){ return; }
componentCallbackDone = true;

if(!!err){
res.errorDetails = format(strings.errors.registry.COMPONENT_EXECUTION_ERROR, err.message || '');
return res.json(500, { error: res.errorDetails, details: { message: err.message, stack: err.stack, originalError: err} });
Expand Down Expand Up @@ -164,12 +169,23 @@ module.exports = function(conf, repository){
staticPath: repository.getStaticFilePath(component.name, component.version, '').replace('https:', '')
};

var setCallbackTimeout = function(){
if(!!conf.executionTimeout){
setTimeout(function(){
returnComponent({
message: format('timeout ({0}ms)', conf.executionTimeout * 1000)
});
}, conf.executionTimeout * 1000);
}
};

if(!!cached && !res.conf.local){
domain.on('error', returnComponent);

try {
domain.run(function(){
cached(contextObj, returnComponent);
setCallbackTimeout();
});
} catch(e){
return returnComponent(e);
Expand All @@ -178,6 +194,7 @@ module.exports = function(conf, repository){
repository.getDataProvider(component.name, component.version, function(err, dataProcessorJs){

if(err){
componentCallbackDone = true;
res.errorDetails = strings.errors.registry.RESOLVING_ERROR;
return res.json(502, { error: res.errorDetails });
}
Expand All @@ -197,6 +214,7 @@ module.exports = function(conf, repository){
if(err.code === 'DEPENDENCY_MISSING_FROM_REGISTRY'){
res.errorDetails = format(strings.errors.registry.DEPENDENCY_NOT_FOUND, err.missing.join(', '));
res.errorCode = err.code;
componentCallbackDone = true;

return res.json(501, {
code: res.errorCode,
Expand All @@ -212,6 +230,7 @@ module.exports = function(conf, repository){

res.errorDetails = format(strings.errors.registry.PLUGIN_NOT_FOUND, unRegisteredPlugins.join(' ,'));
res.errorCode = 'PLUGIN_MISSING_FROM_COMPONENT';
componentCallbackDone = true;

return res.json(501, {
code: res.errorCode,
Expand All @@ -231,6 +250,7 @@ module.exports = function(conf, repository){
domain.on('error', handleError);
domain.run(function(){
processData(contextObj, returnComponent);
setCallbackTimeout();
});
} catch(err){
handleError(err);
Expand Down
58 changes: 58 additions & 0 deletions test/unit/registry-routes-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,64 @@ describe('registry : routes : component', function(){
};
};

describe('when getting a component with server.js execution timeout', function(){

var code, response;
before(function(done){
initialise({
component: {
name: 'timeout-component',
version: '1.0.0',
oc: {
container: false,
files: {
template: {
type: 'jade',
hashKey: '8c1fbd954f2b0d8cd5cf11c885fed4805225749f',
src: 'template.js'
},
dataProvider: {
type: 'node.js',
hashKey: '123456',
src: 'server.js'
}
}
}
},
data: '"use strict";module.exports.data=function(t,u){setTimeout(function(){u(null,{done:true});}, 5000);};',
view: 'var oc=oc||{};oc.components=oc.components||{},oc.components["8c1fbd954f2b0d8cd5cf11c885fed4805225749f"]' +
'=function(){var o=[];return o.push("<div>hello</div>"),o.join("")};'
});

componentRoute = new ComponentRoute({}, mockedRepository);

var resJson = function(calledCode, calledResponse){
code = calledCode;
response = calledResponse;
done();
};

componentRoute({
headers: {},
params: { componentName: 'timeout-component' }
}, {
conf: {
baseUrl: 'http://component.com/',
executionTimeout: 0.1
},
json: resJson
});
});

it('should return 500 status code', function(){
expect(code).to.be.equal(500);
});

it('should respond with error message', function(){
expect(response.error).to.equal('Component execution error: timeout (100ms)');
});
});

describe('when getting a component with server.js execution errors', function(){

before(function(){
Expand Down