Skip to content

Commit a78ac24

Browse files
committed
support command in lifecycle
1 parent 00bb724 commit a78ac24

5 files changed

Lines changed: 112 additions & 39 deletions

File tree

package-lock.json

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

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
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: 1 addition & 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 {

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

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ export default function (_local: any, _remote: any) {
109109
_.set(local, 'instanceLifecycleConfig.initializer.timeout', initializerTimeout);
110110
}
111111

112+
if (
113+
remote?.instanceLifecycleConfig?.initializer?.command ||
114+
remote?.instanceLifecycleConfig?.initializer?.timeout
115+
) {
116+
if (initializer?.command) {
117+
if (!initializer.timeout) {
118+
_.set(local, 'instanceLifecycleConfig.initializer.timeout', initializerTimeout);
119+
}
120+
} else {
121+
_.set(local, 'instanceLifecycleConfig.initializer.command', []);
122+
_.set(local, 'instanceLifecycleConfig.initializer.timeout', 3);
123+
}
124+
} else if (initializer?.command && !initializer.timeout) {
125+
_.set(local, 'instanceLifecycleConfig.initializer.timeout', initializerTimeout);
126+
}
127+
112128
const preStopTimeout = _.get(remote, 'instanceLifecycleConfig.preStop.timeout', 3);
113129
if (
114130
remote?.instanceLifecycleConfig?.preStop?.handler ||
@@ -125,17 +141,43 @@ export default function (_local: any, _remote: any) {
125141
} else if (preStop?.handler && !preStop.timeout) {
126142
_.set(local, 'instanceLifecycleConfig.preStop.timeout', preStopTimeout);
127143
}
144+
145+
if (
146+
remote?.instanceLifecycleConfig?.preStop?.command ||
147+
remote?.instanceLifecycleConfig?.preStop?.timeout
148+
) {
149+
if (preStop?.command) {
150+
if (!preStop.timeout) {
151+
_.set(local, 'instanceLifecycleConfig.preStop.timeout', preStopTimeout);
152+
}
153+
} else {
154+
_.set(local, 'instanceLifecycleConfig.preStop.command', []);
155+
_.set(local, 'instanceLifecycleConfig.preStop.timeout', 3);
156+
}
157+
} else if (preStop?.command && !preStop.timeout) {
158+
_.set(local, 'instanceLifecycleConfig.preStop.timeout', preStopTimeout);
159+
}
128160
}
129161

130-
// 如果 local 和 remote 都是 handler 为 '', 则从 props 中删除
162+
// 如果 local 和 remote 都是 handler 和 command 为 '', 则从 props 中删除
131163
if (local?.instanceLifecycleConfig && remote?.instanceLifecycleConfig) {
132164
const { initializer: initializerL, preStop: preStopL } = local.instanceLifecycleConfig || {};
133165
const { initializer: initializerR, preStop: preStopR } = remote.instanceLifecycleConfig || {};
134-
if (initializerL?.handler === initializerR?.handler && initializerL?.handler === '') {
166+
if (
167+
initializerL?.handler === initializerR?.handler &&
168+
initializerL?.handler === '' &&
169+
initializerL?.command === initializerR?.command &&
170+
_.isEmpty(initializerL?.command)
171+
) {
135172
_.unset(local, 'instanceLifecycleConfig.initializer');
136173
_.unset(remote, 'instanceLifecycleConfig.initializer');
137174
}
138-
if (preStopL?.handler === preStopR?.handler && preStopL?.handler === '') {
175+
if (
176+
preStopL?.handler === preStopR?.handler &&
177+
preStopL?.handler === '' &&
178+
preStopL?.command === preStopR?.command &&
179+
_.isEmpty(preStopL?.command)
180+
) {
139181
_.unset(local, 'instanceLifecycleConfig.preStop');
140182
_.unset(remote, 'instanceLifecycleConfig.preStop');
141183
}

src/schema.json

Lines changed: 6 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
},

0 commit comments

Comments
 (0)