Skip to content

Commit bef307b

Browse files
authored
Merge pull request #57 from devsapp/support-lifecycle
Support lifecycle
2 parents 7694dd2 + 6000e08 commit bef307b

File tree

8 files changed

+131
-52
lines changed

8 files changed

+131
-52
lines changed

__tests__/ut/resources_acr_test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,12 @@ describe('replaceFunction', () => {
241241
instanceLifecycleConfig: {
242242
initializer: {
243243
handler: '',
244+
command: [],
244245
timeout: 3,
245246
},
246247
preStop: {
247248
handler: '',
249+
command: [],
248250
timeout: 3,
249251
},
250252
},

package-lock.json

Lines changed: 64 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
"license": "ISC",
2424
"dependencies": {
2525
"@alicloud/fc2": "^2.6.6",
26-
"@alicloud/fc20230330": "4.3.2",
26+
"@alicloud/fc20230330": "4.3.3",
2727
"@alicloud/pop-core": "^1.8.0",
2828
"@serverless-cd/srm-aliyun-pop-core": "^0.0.7-beta.21",
2929
"@serverless-cd/srm-aliyun-ram20150501": "^0.0.2-beta.9",
3030
"@serverless-cd/srm-aliyun-sls20201230": "0.0.5-beta.3",
3131
"@serverless-devs/diff": "^0.0.3-beta.6",
32-
"@serverless-devs/downloads": "^0.0.6",
33-
"@serverless-devs/load-component": "^0.0.8",
34-
"@serverless-devs/utils": "^0.0.16",
32+
"@serverless-devs/downloads": "^0.0.7",
33+
"@serverless-devs/load-component": "^0.0.9",
34+
"@serverless-devs/utils": "^0.0.17",
3535
"@serverless-devs/zip": "^0.0.3-beta.8",
3636
"ajv": "^8.17.1",
3737
"aliyun-sdk": "^1.12.10",

src/interface/function.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type IRuntime = `${Runtime}`;
55
export interface ILifecycleHook {
66
handler?: string;
77
timeout?: number;
8+
command?: string[];
89
}
910

1011
export interface IHealthCheckConfig {
@@ -125,6 +126,7 @@ export interface IFunction {
125126
memorySize?: number;
126127
timeout?: number;
127128
sessionAffinity?: string;
129+
enableLongLiving?: boolean;
128130

129131
logConfig?: 'auto' | ILogConfig;
130132
nasConfig?: 'auto' | INasConfig;

src/resources/fc/impl/replace-function-config.ts

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,50 +92,86 @@ export default function (_local: any, _remote: any) {
9292
// 适配钩子函数配置
9393
if (!(_.isEmpty(local?.instanceLifecycleConfig) && _.isEmpty(remote?.instanceLifecycleConfig))) {
9494
const { initializer, preStop } = local.instanceLifecycleConfig || {};
95+
if (initializer?.handler && initializer?.command) {
96+
throw new Error(
97+
'fc3 pre check: command and handler can not be set at the same time in lifecycle Lifecycle.Initializer',
98+
);
99+
}
100+
if (preStop?.handler && preStop?.command) {
101+
throw new Error(
102+
'fc3 pre check: command and handler can not be set at the same time in lifecycle Lifecycle.PreStop',
103+
);
104+
}
95105
const initializerTimeout = _.get(remote, 'instanceLifecycleConfig.initializer.timeout', 3);
96106
if (
97107
remote?.instanceLifecycleConfig?.initializer?.handler ||
108+
remote?.instanceLifecycleConfig?.initializer?.command ||
98109
remote?.instanceLifecycleConfig?.initializer?.timeout
99110
) {
100-
if (initializer?.handler) {
111+
if (initializer?.handler || initializer?.command) {
112+
if (remote?.instanceLifecycleConfig?.initializer?.handler && initializer?.command) {
113+
_.set(local, 'instanceLifecycleConfig.initializer.handler', '');
114+
}
115+
if (remote?.instanceLifecycleConfig?.initializer?.command && initializer?.handler) {
116+
_.set(local, 'instanceLifecycleConfig.initializer.command', []);
117+
}
101118
if (!initializer.timeout) {
102119
_.set(local, 'instanceLifecycleConfig.initializer.timeout', initializerTimeout);
103120
}
104121
} else {
105122
_.set(local, 'instanceLifecycleConfig.initializer.handler', '');
123+
_.set(local, 'instanceLifecycleConfig.initializer.command', []);
106124
_.set(local, 'instanceLifecycleConfig.initializer.timeout', 3);
107125
}
108-
} else if (initializer?.handler && !initializer.timeout) {
126+
} else if ((initializer?.command || initializer?.handler) && !initializer.timeout) {
109127
_.set(local, 'instanceLifecycleConfig.initializer.timeout', initializerTimeout);
110128
}
111129

112130
const preStopTimeout = _.get(remote, 'instanceLifecycleConfig.preStop.timeout', 3);
113131
if (
114132
remote?.instanceLifecycleConfig?.preStop?.handler ||
133+
remote?.instanceLifecycleConfig?.preStop?.command ||
115134
remote?.instanceLifecycleConfig?.preStop?.timeout
116135
) {
117-
if (preStop?.handler) {
136+
if (preStop?.handler || preStop?.command) {
137+
if (remote?.instanceLifecycleConfig?.preStop?.handler && preStop?.command) {
138+
_.set(local, 'instanceLifecycleConfig.preStop.handler', '');
139+
}
140+
if (remote?.instanceLifecycleConfig?.preStop?.command && preStop?.handler) {
141+
_.set(local, 'instanceLifecycleConfig.preStop.command', []);
142+
}
118143
if (!preStop.timeout) {
119144
_.set(local, 'instanceLifecycleConfig.preStop.timeout', preStopTimeout);
120145
}
121146
} else {
122147
_.set(local, 'instanceLifecycleConfig.preStop.handler', '');
148+
_.set(local, 'instanceLifecycleConfig.preStop.command', []);
123149
_.set(local, 'instanceLifecycleConfig.preStop.timeout', 3);
124150
}
125-
} else if (preStop?.handler && !preStop.timeout) {
151+
} else if ((preStop?.command || preStop?.handler) && !preStop.timeout) {
126152
_.set(local, 'instanceLifecycleConfig.preStop.timeout', preStopTimeout);
127153
}
128154
}
129155

130-
// 如果 local 和 remote 都是 handler 为 '', 则从 props 中删除
156+
// 如果 local 和 remote 都是 handler 和 command 为 '', 则从 props 中删除
131157
if (local?.instanceLifecycleConfig && remote?.instanceLifecycleConfig) {
132158
const { initializer: initializerL, preStop: preStopL } = local.instanceLifecycleConfig || {};
133159
const { initializer: initializerR, preStop: preStopR } = remote.instanceLifecycleConfig || {};
134-
if (initializerL?.handler === initializerR?.handler && initializerL?.handler === '') {
160+
if (
161+
initializerL?.handler === initializerR?.handler &&
162+
initializerL?.handler === '' &&
163+
_.isEqual(initializerL?.command, initializerR?.command) &&
164+
_.isEmpty(initializerL?.command)
165+
) {
135166
_.unset(local, 'instanceLifecycleConfig.initializer');
136167
_.unset(remote, 'instanceLifecycleConfig.initializer');
137168
}
138-
if (preStopL?.handler === preStopR?.handler && preStopL?.handler === '') {
169+
if (
170+
preStopL?.handler === preStopR?.handler &&
171+
preStopL?.handler === '' &&
172+
_.isEqual(preStopL?.command, preStopR?.command) &&
173+
_.isEmpty(preStopL?.command)
174+
) {
139175
_.unset(local, 'instanceLifecycleConfig.preStop');
140176
_.unset(remote, 'instanceLifecycleConfig.preStop');
141177
}

src/schema.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,12 @@
509509
},
510510
"ILifecycleHook": {
511511
"properties": {
512+
"command": {
513+
"items": {
514+
"type": "string"
515+
},
516+
"type": "array"
517+
},
512518
"handler": {
513519
"type": "string"
514520
},
@@ -1148,6 +1154,9 @@
11481154
],
11491155
"type": "number"
11501156
},
1157+
"enableLongLiving": {
1158+
"type": "boolean"
1159+
},
11511160
"endpoint": {
11521161
"type": "string"
11531162
},

src/subCommands/deploy/impl/function.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ export default class Service extends Base {
138138
return;
139139
}
140140

141+
if (_.get(this.remote, 'disableOndemand') === false) {
142+
_.unset(this.remote, 'disableOndemand');
143+
}
144+
141145
_.unset(this.local, 'endpoint');
142146
const { code } = this.local;
143147
_.unset(this.local, 'code');

src/subCommands/plan/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ export default class Plan {
115115
_.unset(local, 'concurrencyConfig');
116116
_.unset(local, 'customContainerConfig.registryConfig');
117117
_.unset(remote, 'functionArn');
118-
118+
if (_.get(remote, 'disableOndemand') === false) {
119+
_.unset(remote, 'disableOndemand');
120+
}
119121
const config = FC.replaceFunctionConfig(local, remote);
120122
return diffConvertPlanYaml(config.remote, config.local, { deep: 0, complete: true });
121123
}

0 commit comments

Comments
 (0)