diff --git a/deploy/lib/compileFunctions.js b/deploy/lib/compileFunctions.js index 15a4d03..031db0d 100644 --- a/deploy/lib/compileFunctions.js +++ b/deploy/lib/compileFunctions.js @@ -4,6 +4,7 @@ const path = require('path'); +const _ = require('lodash'); const BbPromise = require('bluebird'); module.exports = { @@ -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') { @@ -103,6 +111,8 @@ const getFunctionTemplate = (funcObject, region, sourceArchiveUrl) => { //eslint name: funcObject.name, properties: { location: region, + availableMemoryMb: 256, + timeout: '60s', function: funcObject.handler, sourceArchiveUrl, }, diff --git a/deploy/lib/compileFunctions.test.js b/deploy/lib/compileFunctions.test.js index 6986afb..880164a 100644 --- a/deploy/lib/compileFunctions.test.js +++ b/deploy/lib/compileFunctions.test.js @@ -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: { @@ -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', @@ -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', @@ -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',