Skip to content

Commit

Permalink
Merge pull request #44 from serverless/add-memory-size-and-timeout-co…
Browse files Browse the repository at this point in the history
…nfig

Add memorySize and timeout config
  • Loading branch information
pmuens committed Mar 6, 2017
2 parents 75fccfd + eb813ff commit 235793e
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
10 changes: 10 additions & 0 deletions deploy/lib/compileFunctions.js
Expand Up @@ -4,6 +4,7 @@

const path = require('path');

const _ = require('lodash');
const BbPromise = require('bluebird');

module.exports = {
Expand All @@ -30,6 +31,13 @@ module.exports = {
this.serverless.service.service
}-${this.options.stage}/${this.serverless.service.package.artifactFilePath}`);

funcTemplate.properties.availableMemoryMb = _.get(funcObject, 'memorySize')
|| _.get(this, 'serverless.service.provider.memorySize')
|| 256;
funcTemplate.properties.timeout = _.get(funcObject, 'timeout')
|| _.get(this, 'serverless.service.provider.timeout')
|| '60s';

const eventType = Object.keys(funcObject.events[0])[0];

if (eventType === 'http') {
Expand Down Expand Up @@ -103,6 +111,8 @@ const getFunctionTemplate = (funcObject, region, sourceArchiveUrl) => { //eslint
name: funcObject.name,
properties: {
location: region,
availableMemoryMb: 256,
timeout: '60s',
function: funcObject.handler,
sourceArchiveUrl,
},
Expand Down
138 changes: 138 additions & 0 deletions deploy/lib/compileFunctions.test.js
Expand Up @@ -96,6 +96,138 @@ describe('CompileFunctions', () => {
expect(() => googleDeploy.compileFunctions()).toThrow(Error);
});

it('should set the memory size based on the functions configuration', () => {
googleDeploy.serverless.service.functions = {
func1: {
handler: 'func1',
memorySize: 1024,
events: [
{ http: 'foo' },
],
},
};

const compiledResources = [{
type: 'cloudfunctions.v1beta2.function',
name: 'my-service-dev-func1',
properties: {
location: 'us-central1',
function: 'func1',
availableMemoryMb: 1024,
timeout: '60s',
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
httpsTrigger: {
url: 'foo',
},
},
}];

return googleDeploy.compileFunctions().then(() => {
expect(consoleLogStub.calledOnce).toEqual(true);
expect(googleDeploy.serverless.service.provider.compiledConfigurationTemplate.resources)
.toEqual(compiledResources);
});
});

it('should set the memory size based on the provider configuration', () => {
googleDeploy.serverless.service.functions = {
func1: {
handler: 'func1',
events: [
{ http: 'foo' },
],
},
};
googleDeploy.serverless.service.provider.memorySize = 1024;

const compiledResources = [{
type: 'cloudfunctions.v1beta2.function',
name: 'my-service-dev-func1',
properties: {
location: 'us-central1',
function: 'func1',
availableMemoryMb: 1024,
timeout: '60s',
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
httpsTrigger: {
url: 'foo',
},
},
}];

return googleDeploy.compileFunctions().then(() => {
expect(consoleLogStub.calledOnce).toEqual(true);
expect(googleDeploy.serverless.service.provider.compiledConfigurationTemplate.resources)
.toEqual(compiledResources);
});
});

it('should set the timout based on the functions configuration', () => {
googleDeploy.serverless.service.functions = {
func1: {
handler: 'func1',
timeout: '120s',
events: [
{ http: 'foo' },
],
},
};

const compiledResources = [{
type: 'cloudfunctions.v1beta2.function',
name: 'my-service-dev-func1',
properties: {
location: 'us-central1',
function: 'func1',
availableMemoryMb: 256,
timeout: '120s',
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
httpsTrigger: {
url: 'foo',
},
},
}];

return googleDeploy.compileFunctions().then(() => {
expect(consoleLogStub.calledOnce).toEqual(true);
expect(googleDeploy.serverless.service.provider.compiledConfigurationTemplate.resources)
.toEqual(compiledResources);
});
});

it('should set the timeout based on the provider configuration', () => {
googleDeploy.serverless.service.functions = {
func1: {
handler: 'func1',
events: [
{ http: 'foo' },
],
},
};
googleDeploy.serverless.service.provider.timeout = '120s';

const compiledResources = [{
type: 'cloudfunctions.v1beta2.function',
name: 'my-service-dev-func1',
properties: {
location: 'us-central1',
function: 'func1',
availableMemoryMb: 256,
timeout: '120s',
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
httpsTrigger: {
url: 'foo',
},
},
}];

return googleDeploy.compileFunctions().then(() => {
expect(consoleLogStub.calledOnce).toEqual(true);
expect(googleDeploy.serverless.service.provider.compiledConfigurationTemplate.resources)
.toEqual(compiledResources);
});
});

it('should compile "http" events properly', () => {
googleDeploy.serverless.service.functions = {
func1: {
Expand All @@ -112,6 +244,8 @@ describe('CompileFunctions', () => {
properties: {
location: 'us-central1',
function: 'func1',
availableMemoryMb: 256,
timeout: '60s',
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
httpsTrigger: {
url: 'foo',
Expand Down Expand Up @@ -160,6 +294,8 @@ describe('CompileFunctions', () => {
properties: {
location: 'us-central1',
function: 'func1',
availableMemoryMb: 256,
timeout: '60s',
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
eventTrigger: {
eventType: 'foo',
Expand All @@ -174,6 +310,8 @@ describe('CompileFunctions', () => {
properties: {
location: 'us-central1',
function: 'func2',
availableMemoryMb: 256,
timeout: '60s',
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
eventTrigger: {
eventType: 'foo',
Expand Down

0 comments on commit 235793e

Please sign in to comment.