Skip to content

Commit eb813ff

Browse files
committed
Add memorySize and timeout config
1 parent 75fccfd commit eb813ff

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

deploy/lib/compileFunctions.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
const path = require('path');
66

7+
const _ = require('lodash');
78
const BbPromise = require('bluebird');
89

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

34+
funcTemplate.properties.availableMemoryMb = _.get(funcObject, 'memorySize')
35+
|| _.get(this, 'serverless.service.provider.memorySize')
36+
|| 256;
37+
funcTemplate.properties.timeout = _.get(funcObject, 'timeout')
38+
|| _.get(this, 'serverless.service.provider.timeout')
39+
|| '60s';
40+
3341
const eventType = Object.keys(funcObject.events[0])[0];
3442

3543
if (eventType === 'http') {
@@ -103,6 +111,8 @@ const getFunctionTemplate = (funcObject, region, sourceArchiveUrl) => { //eslint
103111
name: funcObject.name,
104112
properties: {
105113
location: region,
114+
availableMemoryMb: 256,
115+
timeout: '60s',
106116
function: funcObject.handler,
107117
sourceArchiveUrl,
108118
},

deploy/lib/compileFunctions.test.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,138 @@ describe('CompileFunctions', () => {
9696
expect(() => googleDeploy.compileFunctions()).toThrow(Error);
9797
});
9898

99+
it('should set the memory size based on the functions configuration', () => {
100+
googleDeploy.serverless.service.functions = {
101+
func1: {
102+
handler: 'func1',
103+
memorySize: 1024,
104+
events: [
105+
{ http: 'foo' },
106+
],
107+
},
108+
};
109+
110+
const compiledResources = [{
111+
type: 'cloudfunctions.v1beta2.function',
112+
name: 'my-service-dev-func1',
113+
properties: {
114+
location: 'us-central1',
115+
function: 'func1',
116+
availableMemoryMb: 1024,
117+
timeout: '60s',
118+
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
119+
httpsTrigger: {
120+
url: 'foo',
121+
},
122+
},
123+
}];
124+
125+
return googleDeploy.compileFunctions().then(() => {
126+
expect(consoleLogStub.calledOnce).toEqual(true);
127+
expect(googleDeploy.serverless.service.provider.compiledConfigurationTemplate.resources)
128+
.toEqual(compiledResources);
129+
});
130+
});
131+
132+
it('should set the memory size based on the provider configuration', () => {
133+
googleDeploy.serverless.service.functions = {
134+
func1: {
135+
handler: 'func1',
136+
events: [
137+
{ http: 'foo' },
138+
],
139+
},
140+
};
141+
googleDeploy.serverless.service.provider.memorySize = 1024;
142+
143+
const compiledResources = [{
144+
type: 'cloudfunctions.v1beta2.function',
145+
name: 'my-service-dev-func1',
146+
properties: {
147+
location: 'us-central1',
148+
function: 'func1',
149+
availableMemoryMb: 1024,
150+
timeout: '60s',
151+
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
152+
httpsTrigger: {
153+
url: 'foo',
154+
},
155+
},
156+
}];
157+
158+
return googleDeploy.compileFunctions().then(() => {
159+
expect(consoleLogStub.calledOnce).toEqual(true);
160+
expect(googleDeploy.serverless.service.provider.compiledConfigurationTemplate.resources)
161+
.toEqual(compiledResources);
162+
});
163+
});
164+
165+
it('should set the timout based on the functions configuration', () => {
166+
googleDeploy.serverless.service.functions = {
167+
func1: {
168+
handler: 'func1',
169+
timeout: '120s',
170+
events: [
171+
{ http: 'foo' },
172+
],
173+
},
174+
};
175+
176+
const compiledResources = [{
177+
type: 'cloudfunctions.v1beta2.function',
178+
name: 'my-service-dev-func1',
179+
properties: {
180+
location: 'us-central1',
181+
function: 'func1',
182+
availableMemoryMb: 256,
183+
timeout: '120s',
184+
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
185+
httpsTrigger: {
186+
url: 'foo',
187+
},
188+
},
189+
}];
190+
191+
return googleDeploy.compileFunctions().then(() => {
192+
expect(consoleLogStub.calledOnce).toEqual(true);
193+
expect(googleDeploy.serverless.service.provider.compiledConfigurationTemplate.resources)
194+
.toEqual(compiledResources);
195+
});
196+
});
197+
198+
it('should set the timeout based on the provider configuration', () => {
199+
googleDeploy.serverless.service.functions = {
200+
func1: {
201+
handler: 'func1',
202+
events: [
203+
{ http: 'foo' },
204+
],
205+
},
206+
};
207+
googleDeploy.serverless.service.provider.timeout = '120s';
208+
209+
const compiledResources = [{
210+
type: 'cloudfunctions.v1beta2.function',
211+
name: 'my-service-dev-func1',
212+
properties: {
213+
location: 'us-central1',
214+
function: 'func1',
215+
availableMemoryMb: 256,
216+
timeout: '120s',
217+
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
218+
httpsTrigger: {
219+
url: 'foo',
220+
},
221+
},
222+
}];
223+
224+
return googleDeploy.compileFunctions().then(() => {
225+
expect(consoleLogStub.calledOnce).toEqual(true);
226+
expect(googleDeploy.serverless.service.provider.compiledConfigurationTemplate.resources)
227+
.toEqual(compiledResources);
228+
});
229+
});
230+
99231
it('should compile "http" events properly', () => {
100232
googleDeploy.serverless.service.functions = {
101233
func1: {
@@ -112,6 +244,8 @@ describe('CompileFunctions', () => {
112244
properties: {
113245
location: 'us-central1',
114246
function: 'func1',
247+
availableMemoryMb: 256,
248+
timeout: '60s',
115249
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
116250
httpsTrigger: {
117251
url: 'foo',
@@ -160,6 +294,8 @@ describe('CompileFunctions', () => {
160294
properties: {
161295
location: 'us-central1',
162296
function: 'func1',
297+
availableMemoryMb: 256,
298+
timeout: '60s',
163299
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
164300
eventTrigger: {
165301
eventType: 'foo',
@@ -174,6 +310,8 @@ describe('CompileFunctions', () => {
174310
properties: {
175311
location: 'us-central1',
176312
function: 'func2',
313+
availableMemoryMb: 256,
314+
timeout: '60s',
177315
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
178316
eventTrigger: {
179317
eventType: 'foo',

0 commit comments

Comments
 (0)