@geek-fun/serverlessinsight 0.9.1 → 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.serverlessinsight/state-cdn-flow-app-cdn-flow-service.json.backup +1 -1
- package/.serverlessinsight/state-iam-test-app-iam-test-service.json.backup +2 -2
- package/.serverlessinsight/state-insight-poc-app-insight-poc-tencent.json.backup +2 -2
- package/.serverlessinsight/state-insight-poc-app-log-insight-poc-tencent-log.json.backup +3 -3
- package/.serverlessinsight/state-insight-poc-deploy-app-insight-poc-deploy.json.backup +4 -4
- package/.serverlessinsight/state-insight-poc-destroy-app-insight-poc-destroy.json.backup +1 -1
- package/.serverlessinsight/state-insight-poc-timer-app-insight-poc-timer.json.backup +109 -0
- package/.serverlessinsight/state-insight-volc-app-insight-volc.json.backup +2 -2
- package/.serverlessinsight/state-insight-volc-nested-app-insight-volc-nested.json.backup +3 -3
- package/.serverlessinsight/state-nested-drift-app-nested-drift-service.json.backup +2 -2
- package/.serverlessinsight/state-role-reconcile-test-app-role-reconcile-test-service.json.backup +3 -3
- package/CHANGELOG.md +17 -0
- package/README.md +76 -0
- package/README.zh-CN.md +67 -0
- package/codecov +0 -0
- package/codecov.SHA256SUM +1 -0
- package/codecov.SHA256SUM.sig +16 -0
- package/dist/package.json +1 -1
- package/dist/src/commands/index.js +75 -23
- package/dist/src/commands/plan.js +21 -1
- package/dist/src/commands/schema.js +24 -0
- package/dist/src/commands/show.js +64 -10
- package/dist/src/commands/validate.js +34 -2
- package/dist/src/common/aliyunClient/fc3Operations.js +32 -1
- package/dist/src/common/constants.js +7 -1
- package/dist/src/common/jsonOutput.js +77 -0
- package/dist/src/common/logger.js +6 -3
- package/dist/src/common/noColor.js +9 -0
- package/dist/src/common/planFormatter.js +12 -6
- package/dist/src/common/siCron.js +415 -0
- package/dist/src/common/stateBackend/saasStateBackend.js +5 -4
- package/dist/src/common/tencentClient/scfOperations.js +2 -0
- package/dist/src/common/volcengineClient/vefaasOperations.js +45 -0
- package/dist/src/lang/en.js +43 -0
- package/dist/src/lang/index.js +36 -1
- package/dist/src/lang/zh-CN.js +43 -0
- package/dist/src/parser/functionParser.js +47 -1
- package/dist/src/stack/aliyunStack/fc3Planner.js +26 -0
- package/dist/src/stack/aliyunStack/fc3Resource.js +159 -2
- package/dist/src/stack/localStack/event.js +2 -13
- package/dist/src/stack/localStack/function.js +2 -13
- package/dist/src/stack/localStack/index.js +8 -1
- package/dist/src/stack/localStack/timer.js +162 -0
- package/dist/src/stack/localStack/utils.js +19 -1
- package/dist/src/stack/scfStack/scfPlanner.js +52 -18
- package/dist/src/stack/scfStack/scfResource.js +170 -1
- package/dist/src/stack/volcengineStack/vefaasPlanner.js +49 -0
- package/dist/src/stack/volcengineStack/vefaasResource.js +215 -2
- package/dist/src/validator/functionSchema.js +25 -2
- package/dist/src/validator/iacSchema.js +2 -1
- package/dist/src/validator/index.js +4 -1
- package/dist/src/validator/rootSchema.js +49 -18
- package/dist/src/validator/schemaExport.js +60 -0
- package/dist/src/validator/semanticValidation.js +79 -0
- package/dist/src/validator/templateRefSchema.js +9 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/samples/aliyun-poc-timer.yml +39 -0
package/dist/src/lang/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.lang = void 0;
|
|
3
|
+
exports.lang = exports.lookupErrorCode = exports.hasLangKey = void 0;
|
|
4
4
|
const child_process_1 = require("child_process");
|
|
5
5
|
const i18n_1 = require("i18n");
|
|
6
6
|
const en_1 = require("./en");
|
|
@@ -39,3 +39,38 @@ const lang = new i18n_1.I18n({
|
|
|
39
39
|
objectNotation: true,
|
|
40
40
|
});
|
|
41
41
|
exports.lang = lang;
|
|
42
|
+
let templateIndex = null;
|
|
43
|
+
const buildTemplateIndex = () => [...Object.entries(en_1.en), ...Object.entries(zh_CN_1.zhCN)].flatMap(([key, value]) => typeof value === 'string' ? [{ key, segments: value.split(/{{[^}]*}}/g) }] : []);
|
|
44
|
+
const matchTemplate = (message, segments) => {
|
|
45
|
+
if (segments.length === 1) {
|
|
46
|
+
return message === segments[0];
|
|
47
|
+
}
|
|
48
|
+
let rest = message;
|
|
49
|
+
for (let i = 0; i < segments.length; i++) {
|
|
50
|
+
const segment = segments[i];
|
|
51
|
+
if (i === 0) {
|
|
52
|
+
if (!rest.startsWith(segment))
|
|
53
|
+
return false;
|
|
54
|
+
rest = rest.slice(segment.length);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (i === segments.length - 1) {
|
|
58
|
+
return rest.endsWith(segment);
|
|
59
|
+
}
|
|
60
|
+
const at = rest.indexOf(segment);
|
|
61
|
+
if (at < 0)
|
|
62
|
+
return false;
|
|
63
|
+
rest = rest.slice(at + segment.length);
|
|
64
|
+
}
|
|
65
|
+
return true;
|
|
66
|
+
};
|
|
67
|
+
const langKeySet = () => new Set(Object.keys({ ...en_1.en, ...zh_CN_1.zhCN }));
|
|
68
|
+
const hasLangKey = (key) => langKeySet().has(key);
|
|
69
|
+
exports.hasLangKey = hasLangKey;
|
|
70
|
+
const lookupErrorCode = (message) => {
|
|
71
|
+
if (!message)
|
|
72
|
+
return undefined;
|
|
73
|
+
templateIndex = templateIndex ?? buildTemplateIndex();
|
|
74
|
+
return templateIndex.find((template) => matchTemplate(message, template.segments))?.key;
|
|
75
|
+
};
|
|
76
|
+
exports.lookupErrorCode = lookupErrorCode;
|
package/dist/src/lang/zh-CN.js
CHANGED
|
@@ -49,6 +49,10 @@ exports.zhCN = {
|
|
|
49
49
|
LOCAL_GATEWAY_ERROR: '本地网关错误',
|
|
50
50
|
ERROR_STOPPING_LOCAL_SERVER: '停止本地服务器时出错',
|
|
51
51
|
RUN_LOCAL_STARTING: '启动本地运行: stage={{stage}} debug={{debug}} watch={{watch}}',
|
|
52
|
+
LOCAL_TIMER_SCHEDULED: "本地定时器 '{{triggerName}}' 已为函数 '{{functionName}}' 调度(cron: {{cron}},UTC)",
|
|
53
|
+
LOCAL_TIMER_FIRED: "本地定时器 '{{triggerName}}' 正在触发函数 '{{functionName}}'...",
|
|
54
|
+
LOCAL_TIMER_DISABLED: "本地定时器 '{{triggerName}}' 已停用(enable: false),跳过调度",
|
|
55
|
+
LOCAL_TIMER_INVALID_CRON: "本地定时器 '{{triggerName}}' 的 cron '{{cron}}' 非法,跳过调度",
|
|
52
56
|
// Function execution
|
|
53
57
|
FUNCTION_REQUEST_RECEIVED: '本地服务器收到函数请求 -> {{method}} {{identifier}} ',
|
|
54
58
|
INVOKING_WORKER_WITH_EVENT_TYPE: '使用事件类型调用工作进程: {{eventType}} 和上下文',
|
|
@@ -868,6 +872,37 @@ exports.zhCN = {
|
|
|
868
872
|
EVENT_FLUSH_FAILED: '部署事件冲刷失败(部署 {{deploymentId}},{{count}} 条):{{error}}',
|
|
869
873
|
EVENT_REPLAY_FAILED: '事件队列文件 {{file}} 重放失败:{{error}}',
|
|
870
874
|
INVALID_HTTP_TRIGGER_AUTH_TYPE: "无效的 HTTP 触发器 auth_type '{{authType}}',必须为 'public' 或 'iam'",
|
|
875
|
+
// Timer trigger messages (issue #258)
|
|
876
|
+
TIMER_TRIGGER_NAME_REQUIRED: "函数 '{{functionName}}' 的定时触发器缺少必填字段 'name'",
|
|
877
|
+
TIMER_TRIGGER_CRON_REQUIRED: "定时触发器 '{{name}}' 缺少必填字段 'cron'",
|
|
878
|
+
TIMER_TRIGGER_DUPLICATE_NAME: "函数 '{{functionName}}' 存在重名的定时触发器 '{{name}}'",
|
|
879
|
+
TIMER_TRIGGER_LIMIT_EXCEEDED: "函数 '{{functionName}}' 配置了 {{count}} 个定时触发器,云平台 {{provider}} 同函数上限为 {{max}} 个",
|
|
880
|
+
SEMANTIC_TIMER_CRON_INVALID: "函数 '{{functionName}}' 的定时触发器 '{{name}}' cron 表达式 '{{cron}}' 非法:{{reason}}",
|
|
881
|
+
TIMER_TRIGGER_NOT_SUPPORTED_PROVIDER: "当前云平台 '{{provider}}' 暂不支持定时触发器",
|
|
882
|
+
TIMER_TRIGGER_PAYLOAD_NOT_SUPPORTED: "定时触发器 '{{name}}' 在云平台 {{provider}} 上不支持 'payload' 字段",
|
|
883
|
+
CREATING_TIMER_TRIGGER: "正在为函数 '{{functionName}}' 创建定时触发器 '{{triggerName}}'...",
|
|
884
|
+
TIMER_TRIGGER_CREATED: "定时触发器 '{{triggerName}}' 已为函数 '{{functionName}}' 创建成功",
|
|
885
|
+
UPDATING_TIMER_TRIGGER: "正在重新创建函数 '{{functionName}}' 的定时触发器 '{{triggerName}}'...",
|
|
886
|
+
DELETING_TIMER_TRIGGER: "正在删除函数 '{{functionName}}' 的定时触发器 '{{triggerName}}'...",
|
|
887
|
+
TIMER_TRIGGER_DELETED: "定时触发器 '{{triggerName}}' 已删除",
|
|
888
|
+
TIMER_TRIGGER_NOT_FOUND: "定时触发器 '{{triggerName}}' 在云平台中不存在,跳过删除",
|
|
889
|
+
TIMER_TRIGGER_ALREADY_ATTACHED: "定时触发器 '{{triggerName}}' 已挂在函数 '{{functionName}}' 上,跳过创建",
|
|
890
|
+
PLAN_DRIFT_TIMER_TRIGGER: '云端定时触发器被修改',
|
|
891
|
+
PLAN_TIMER_TRIGGER_PROBE_FAILED: "探测函数 '{{functionName}}' 的定时触发器失败:{{error}}",
|
|
892
|
+
// si-cron messages (issue #258)
|
|
893
|
+
SI_CRON_EMPTY: 'si-cron 表达式为空',
|
|
894
|
+
SI_CRON_WRONG_FIELD_COUNT: '应为 5 段(分 时 日 月 周)、带秒前缀的 6 段,或 @every <时长>;实际 {{count}} 段',
|
|
895
|
+
SI_CRON_FIELD_INVALID: "字段 '{{field}}' 存在非法值 '{{value}}'",
|
|
896
|
+
SI_CRON_FIELD_EMPTY: "字段 '{{field}}' 为空",
|
|
897
|
+
SI_CRON_FIELD_RANGE: "字段 '{{field}}' 的值 '{{value}}' 超出范围 {{min}}-{{max}}",
|
|
898
|
+
SI_CRON_STEP_INVALID: "字段 '{{field}}' 的步长 '{{step}}' 非法",
|
|
899
|
+
SI_CRON_RANGE_DESCENDING: "字段 '{{field}}' 的区间 '{{range}}' 起止倒置",
|
|
900
|
+
SI_CRON_EVERY_INVALID: "'@every {{duration}}' 不是合法时长;请使用形如 '30s'、'5m'、'1h'、'1d' 的写法",
|
|
901
|
+
SI_CRON_EVERY_NOT_EXPRESSIBLE: '该 @every 间隔无法表达为 {{dialect}} 的 cron;请使用能整除的时长(15s、30s、1m、5m、15m、30m、1h、2h、6h、12h、1d)',
|
|
902
|
+
SI_CRON_SHIFT_UNSUPPORTED: "cron '{{cron}}' 平移到 UTC+8 后会改变触发日期;请避免日/星期维度与 UTC 16 点及以后的整点组合跨月边界",
|
|
903
|
+
SI_CRON_SECONDS_UNSUPPORTED: "cron '{{cron}}' 含非零秒位,5 段 crontab 云平台不支持",
|
|
904
|
+
// Nested live-repair failures (issue #234 executor reconciles)
|
|
905
|
+
NESTED_REPAIR_FAILED: "修复嵌套资源 '{{resource}}' 失败:{{error}}",
|
|
871
906
|
// ===== Issue #251: previously hardcoded messages (now i18n) =====
|
|
872
907
|
UNSUPPORTED_PROVIDER: '不支持的 provider:{{provider}}',
|
|
873
908
|
FORCE_UNLOCK_NO_LOCATION_MODE: '无 location 模式不支持该操作',
|
|
@@ -877,6 +912,7 @@ exports.zhCN = {
|
|
|
877
912
|
SHOW_NO_RESOURCES: '状态中没有任何资源。',
|
|
878
913
|
SHOW_STATE_LOCATION: '状态文件位置:{{stateLocation}}',
|
|
879
914
|
SHOW_BACKEND_REMOTE: '后端:远端',
|
|
915
|
+
SHOW_BACKEND_SAAS: 'Console(SaaS 状态存储)',
|
|
880
916
|
SHOW_DEPLOY_HINT: '要部署资源,请执行:si deploy',
|
|
881
917
|
SHOW_HEADER_DEPLOYED: '已部署资源',
|
|
882
918
|
SHOW_CATEGORY_HEADER: '📦 {{label}}',
|
|
@@ -1032,4 +1068,11 @@ exports.zhCN = {
|
|
|
1032
1068
|
OPT_PARAMETER: '覆盖参数',
|
|
1033
1069
|
OPT_DEBUG: '开启调试模式',
|
|
1034
1070
|
OPT_WATCH: '开启文件监听',
|
|
1071
|
+
// ===== Issue #250: agent-friendly CLI surface =====
|
|
1072
|
+
CMD_SCHEMA_DESC: '导出用于校验的 IaC JSON Schema(draft-07,自包含)',
|
|
1073
|
+
OPT_SCHEMA_OUTPUT: '将 JSON Schema 写入指定文件而不是 stdout',
|
|
1074
|
+
OPT_JSON: '将结果以机器可读 JSON 输出到 stdout(日志保持在 stderr)',
|
|
1075
|
+
OPT_NO_COLOR: '禁用彩色输出(同时遵循 NO_COLOR 环境变量)',
|
|
1076
|
+
SCHEMA_WRITTEN_TO: 'JSON Schema 已写入:{{path}}',
|
|
1077
|
+
ERROR_CODE: '错误码:{{code}}',
|
|
1035
1078
|
};
|
|
@@ -4,6 +4,7 @@ exports.parseFunction = void 0;
|
|
|
4
4
|
const lodash_1 = require("lodash");
|
|
5
5
|
const parseUtils_1 = require("./parseUtils");
|
|
6
6
|
const lang_1 = require("../lang");
|
|
7
|
+
const siCron_1 = require("../common/siCron");
|
|
7
8
|
const parseHttpTrigger = (raw, fnKey) => {
|
|
8
9
|
if (!raw)
|
|
9
10
|
return undefined;
|
|
@@ -19,6 +20,43 @@ const parseHttpTrigger = (raw, fnKey) => {
|
|
|
19
20
|
access: raw.access?.map(String),
|
|
20
21
|
};
|
|
21
22
|
};
|
|
23
|
+
const parseTimerTriggers = (raw, fnKey) => {
|
|
24
|
+
if (!raw || raw.length === 0) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
// template refs (${vars.cron}) resolve at runtime — grammar is checked after
|
|
28
|
+
const TEMPLATE_REF_PATTERN = /^\$\{[^}]+\}$/;
|
|
29
|
+
const timers = raw.map((item) => {
|
|
30
|
+
if (item.name === undefined || String(item.name).trim() === '') {
|
|
31
|
+
throw new Error(lang_1.lang.__('TIMER_TRIGGER_NAME_REQUIRED', { functionName: fnKey }));
|
|
32
|
+
}
|
|
33
|
+
const name = String(item.name);
|
|
34
|
+
if (item.cron === undefined || String(item.cron).trim() === '') {
|
|
35
|
+
throw new Error(lang_1.lang.__('TIMER_TRIGGER_CRON_REQUIRED', { name }));
|
|
36
|
+
}
|
|
37
|
+
const cron = String(item.cron);
|
|
38
|
+
if (!TEMPLATE_REF_PATTERN.test(cron)) {
|
|
39
|
+
(0, siCron_1.validateSiCron)(cron);
|
|
40
|
+
}
|
|
41
|
+
// empty strings normalize to unset — the provider reconcile treats '' and
|
|
42
|
+
// absent as the same, so keeping '' here would recreate the trigger on
|
|
43
|
+
// every deploy
|
|
44
|
+
const payload = item.payload !== undefined ? String(item.payload) : undefined;
|
|
45
|
+
const description = item.description !== undefined ? String(item.description) : undefined;
|
|
46
|
+
return {
|
|
47
|
+
name,
|
|
48
|
+
cron,
|
|
49
|
+
...(payload ? { payload } : {}),
|
|
50
|
+
enable: (0, parseUtils_1.parseBooleanWithDefault)(item.enable, true),
|
|
51
|
+
...(description ? { description } : {}),
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
const duplicate = timers.find((timer, index) => timers.findIndex((peer) => peer.name === timer.name) !== index);
|
|
55
|
+
if (duplicate) {
|
|
56
|
+
throw new Error(lang_1.lang.__('TIMER_TRIGGER_DUPLICATE_NAME', { name: duplicate.name, functionName: fnKey }));
|
|
57
|
+
}
|
|
58
|
+
return timers;
|
|
59
|
+
};
|
|
22
60
|
const parseDomain = (raw) => {
|
|
23
61
|
if (!raw)
|
|
24
62
|
return undefined;
|
|
@@ -34,6 +72,7 @@ const parseFunction = (functions) => {
|
|
|
34
72
|
}
|
|
35
73
|
return Object.entries(functions).map(([key, func]) => {
|
|
36
74
|
const httpTrigger = parseHttpTrigger(func.triggers?.http, key);
|
|
75
|
+
const timerTriggers = parseTimerTriggers(func.triggers?.timer, key);
|
|
37
76
|
const domain = parseDomain(func.domain);
|
|
38
77
|
return {
|
|
39
78
|
key,
|
|
@@ -99,7 +138,14 @@ const parseFunction = (functions) => {
|
|
|
99
138
|
storage_class: nasItem.storage_class,
|
|
100
139
|
})),
|
|
101
140
|
},
|
|
102
|
-
...(httpTrigger
|
|
141
|
+
...(httpTrigger || timerTriggers
|
|
142
|
+
? {
|
|
143
|
+
triggers: {
|
|
144
|
+
...(httpTrigger ? { http: httpTrigger } : {}),
|
|
145
|
+
...(timerTriggers ? { timer: timerTriggers } : {}),
|
|
146
|
+
},
|
|
147
|
+
}
|
|
148
|
+
: {}),
|
|
103
149
|
...(domain ? { domain } : {}),
|
|
104
150
|
};
|
|
105
151
|
});
|
|
@@ -9,6 +9,7 @@ const ecsOperations_1 = require("../../common/aliyunClient/ecsOperations");
|
|
|
9
9
|
const refreshCache_1 = require("../../common/refreshCache");
|
|
10
10
|
const planCompare_1 = require("../../common/planCompare");
|
|
11
11
|
const concurrency_1 = require("../../common/concurrency");
|
|
12
|
+
const siCron_1 = require("../../common/siCron");
|
|
12
13
|
const fc3Types_1 = require("./fc3Types");
|
|
13
14
|
const fc3Resource_1 = require("./fc3Resource");
|
|
14
15
|
const lang_1 = require("../../lang");
|
|
@@ -278,6 +279,31 @@ const detectFunctionNestedDrift = async (context, client, fn, currentState) => {
|
|
|
278
279
|
}
|
|
279
280
|
}
|
|
280
281
|
}
|
|
282
|
+
// Issue #258: timer trigger drift — the live trigger list is compared
|
|
283
|
+
// against the desired timers (name-keyed). A trigger missing from the cloud
|
|
284
|
+
// (manual delete), an edited cron/payload/enable, or an extra undeclared
|
|
285
|
+
// timer all count as drift.
|
|
286
|
+
const timerStateInstances = currentState.instances?.filter((i) => i.type === 'ALIYUN_FC3_TIMER_TRIGGER');
|
|
287
|
+
const desiredTimers = fn.triggers?.timer ?? [];
|
|
288
|
+
if (desiredTimers.length > 0 || timerStateInstances.length > 0) {
|
|
289
|
+
const liveTriggerList = await (0, refreshCache_1.cachedRefreshRead)(context, `fc3.listTriggers:${fn.name}`, () => client.fc3.listTriggers(fn.name));
|
|
290
|
+
const liveTimers = (liveTriggerList ?? []).filter((trigger) => trigger.triggerType === 'timer' && trigger.triggerName);
|
|
291
|
+
const desiredNames = new Set(desiredTimers.map((timer) => timer.name));
|
|
292
|
+
const extraTimer = liveTimers.some((trigger) => !desiredNames.has(trigger.triggerName));
|
|
293
|
+
const desiredChanged = desiredTimers.some((timer) => {
|
|
294
|
+
const live = liveTimers.find((trigger) => trigger.triggerName === timer.name);
|
|
295
|
+
if (!live) {
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
const liveConfig = (live.triggerConfig ?? {});
|
|
299
|
+
return (liveConfig.cronExpression !== (0, siCron_1.translateSiCron)(timer.cron, 'aliyun') ||
|
|
300
|
+
(liveConfig.payload ?? '') !== (timer.payload ?? '') ||
|
|
301
|
+
(liveConfig.enable === true || liveConfig.enable === 'true') !== timer.enable);
|
|
302
|
+
});
|
|
303
|
+
if (extraTimer || desiredChanged) {
|
|
304
|
+
return 'PLAN_DRIFT_TIMER_TRIGGER';
|
|
305
|
+
}
|
|
306
|
+
}
|
|
281
307
|
return null;
|
|
282
308
|
};
|
|
283
309
|
const generateFunctionPlan = async (context, state, functions) => {
|
|
@@ -10,6 +10,7 @@ const node_path_1 = __importDefault(require("node:path"));
|
|
|
10
10
|
const common_1 = require("../../common");
|
|
11
11
|
const constants_1 = require("../../common/constants");
|
|
12
12
|
const types_1 = require("../../types");
|
|
13
|
+
const siCron_1 = require("../../common/siCron");
|
|
13
14
|
const fc3Types_1 = require("./fc3Types");
|
|
14
15
|
const slsOperations_1 = require("../../common/aliyunClient/slsOperations");
|
|
15
16
|
const ecsOperations_1 = require("../../common/aliyunClient/ecsOperations");
|
|
@@ -298,6 +299,136 @@ const buildHttpTriggerConfig = (trigger) => {
|
|
|
298
299
|
...(disableURLInternet !== undefined ? { disableURLInternet } : {}),
|
|
299
300
|
};
|
|
300
301
|
};
|
|
302
|
+
const TIMER_TRIGGER_INSTANCE_TYPE = 'ALIYUN_FC3_TIMER_TRIGGER';
|
|
303
|
+
/** FC3 timer dialect: 6-field cron (UTC+8), payload passthrough, enable flag. */
|
|
304
|
+
const buildFc3TimerTriggerConfig = (timer) => ({
|
|
305
|
+
payload: timer.payload ?? '',
|
|
306
|
+
cronExpression: (0, siCron_1.translateSiCron)(timer.cron, 'aliyun'),
|
|
307
|
+
enable: timer.enable,
|
|
308
|
+
...(timer.description !== undefined ? { description: timer.description } : {}),
|
|
309
|
+
});
|
|
310
|
+
const normalizeLiveTimerConfig = (config, description) => ({
|
|
311
|
+
payload: typeof config?.payload === 'string' ? config.payload : '',
|
|
312
|
+
cronExpression: typeof config?.cronExpression === 'string' ? config.cronExpression : '',
|
|
313
|
+
enable: config?.enable === true || config?.enable === 'true',
|
|
314
|
+
...(description ? { description } : {}),
|
|
315
|
+
});
|
|
316
|
+
const timerConfigFromAttributes = (attrs) => {
|
|
317
|
+
const cron = typeof attrs.cron === 'string' ? attrs.cron : undefined;
|
|
318
|
+
if (!cron) {
|
|
319
|
+
return null;
|
|
320
|
+
}
|
|
321
|
+
try {
|
|
322
|
+
return {
|
|
323
|
+
payload: typeof attrs.payload === 'string' ? attrs.payload : '',
|
|
324
|
+
cronExpression: (0, siCron_1.translateSiCron)(cron, 'aliyun'),
|
|
325
|
+
enable: attrs.enable !== false,
|
|
326
|
+
...(typeof attrs.description === 'string' ? { description: attrs.description } : {}),
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
catch {
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
const buildTimerTriggerAttributes = (timer) => ({
|
|
334
|
+
cron: timer.cron,
|
|
335
|
+
...(timer.payload !== undefined ? { payload: timer.payload } : {}),
|
|
336
|
+
enable: timer.enable,
|
|
337
|
+
...(timer.description !== undefined ? { description: timer.description } : {}),
|
|
338
|
+
});
|
|
339
|
+
const buildTimerTriggerInstance = (context, fn, timer) => ({
|
|
340
|
+
type: TIMER_TRIGGER_INSTANCE_TYPE,
|
|
341
|
+
id: timer.name,
|
|
342
|
+
sid: (0, common_1.buildSid)('aliyun', 'fc3-timer-trigger', context.stage, `${fn.name}.${timer.name}`),
|
|
343
|
+
attributes: buildTimerTriggerAttributes(timer),
|
|
344
|
+
});
|
|
345
|
+
const isTriggerNotFound = (error) => error?.code === 'TriggerNotFound';
|
|
346
|
+
/**
|
|
347
|
+
* Reconcile the function's timer triggers against the provider (issue #258).
|
|
348
|
+
*
|
|
349
|
+
* The live trigger list is the reconcile baseline, so cloud-side edits
|
|
350
|
+
* (console cron change, manual delete, extra triggers) converge on every
|
|
351
|
+
* deploy — not just when local state disagrees. When the probe fails the
|
|
352
|
+
* state-recorded instances take over as the baseline. Name is the drift key:
|
|
353
|
+
* si-cron stays the stored form and is translated only at the edge.
|
|
354
|
+
*/
|
|
355
|
+
const reconcileFc3TimerTriggers = async (context, fn, previousInstances, client) => {
|
|
356
|
+
const desiredTimers = fn.triggers?.timer ?? [];
|
|
357
|
+
const previousTimers = previousInstances.filter((i) => i.type === TIMER_TRIGGER_INSTANCE_TYPE);
|
|
358
|
+
if (desiredTimers.length === 0 && previousTimers.length === 0) {
|
|
359
|
+
return [];
|
|
360
|
+
}
|
|
361
|
+
let liveTimers = [];
|
|
362
|
+
let liveAvailable = true;
|
|
363
|
+
try {
|
|
364
|
+
liveTimers = (await client.fc3.listTriggers(fn.name))
|
|
365
|
+
.filter((trigger) => trigger.triggerType === 'timer' && trigger.triggerName)
|
|
366
|
+
.map((trigger) => ({
|
|
367
|
+
name: trigger.triggerName,
|
|
368
|
+
config: normalizeLiveTimerConfig((trigger.triggerConfig ?? {}), trigger.description || undefined),
|
|
369
|
+
}));
|
|
370
|
+
}
|
|
371
|
+
catch (error) {
|
|
372
|
+
liveAvailable = false;
|
|
373
|
+
logger_1.logger.warn(lang_1.lang.__('PLAN_TIMER_TRIGGER_PROBE_FAILED', { functionName: fn.name, error: String(error) }));
|
|
374
|
+
}
|
|
375
|
+
const baseline = liveAvailable
|
|
376
|
+
? liveTimers
|
|
377
|
+
: previousTimers.flatMap((instance) => {
|
|
378
|
+
const config = timerConfigFromAttributes(instance.attributes ?? {});
|
|
379
|
+
return config ? [{ name: instance.id, config }] : [];
|
|
380
|
+
});
|
|
381
|
+
const resultingInstances = [];
|
|
382
|
+
for (const timer of desiredTimers) {
|
|
383
|
+
const desiredConfig = buildFc3TimerTriggerConfig(timer);
|
|
384
|
+
const live = baseline.find((entry) => entry.name === timer.name);
|
|
385
|
+
if (!live) {
|
|
386
|
+
logger_1.logger.info(lang_1.lang.__('CREATING_TIMER_TRIGGER', { triggerName: timer.name, functionName: fn.name }));
|
|
387
|
+
}
|
|
388
|
+
else if (!(0, common_1.attributesEqual)(live.config, desiredConfig)) {
|
|
389
|
+
logger_1.logger.info(lang_1.lang.__('UPDATING_TIMER_TRIGGER', { triggerName: timer.name, functionName: fn.name }));
|
|
390
|
+
try {
|
|
391
|
+
await client.fc3.deleteTrigger(fn.name, timer.name);
|
|
392
|
+
}
|
|
393
|
+
catch (error) {
|
|
394
|
+
if (!isTriggerNotFound(error))
|
|
395
|
+
throw error;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
resultingInstances.push(buildTimerTriggerInstance(context, fn, timer));
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
try {
|
|
403
|
+
await client.fc3.createTrigger(fn.name, timer.name, 'timer', desiredConfig, undefined, timer.description);
|
|
404
|
+
}
|
|
405
|
+
catch (error) {
|
|
406
|
+
// a leftover trigger from an adopted function may already carry the name
|
|
407
|
+
if (!(0, alreadyExists_1.isResourceAlreadyExistsError)(error))
|
|
408
|
+
throw error;
|
|
409
|
+
logger_1.logger.warn(lang_1.lang.__('TIMER_TRIGGER_ALREADY_ATTACHED', {
|
|
410
|
+
triggerName: timer.name,
|
|
411
|
+
functionName: fn.name,
|
|
412
|
+
}));
|
|
413
|
+
}
|
|
414
|
+
logger_1.logger.info(lang_1.lang.__('TIMER_TRIGGER_CREATED', { triggerName: timer.name, functionName: fn.name }));
|
|
415
|
+
resultingInstances.push(buildTimerTriggerInstance(context, fn, timer));
|
|
416
|
+
}
|
|
417
|
+
const desiredNames = new Set(desiredTimers.map((timer) => timer.name));
|
|
418
|
+
for (const stale of baseline.filter((entry) => !desiredNames.has(entry.name))) {
|
|
419
|
+
logger_1.logger.info(lang_1.lang.__('DELETING_TIMER_TRIGGER', { triggerName: stale.name, functionName: fn.name }));
|
|
420
|
+
try {
|
|
421
|
+
await client.fc3.deleteTrigger(fn.name, stale.name);
|
|
422
|
+
logger_1.logger.info(lang_1.lang.__('TIMER_TRIGGER_DELETED', { triggerName: stale.name }));
|
|
423
|
+
}
|
|
424
|
+
catch (error) {
|
|
425
|
+
if (!isTriggerNotFound(error))
|
|
426
|
+
throw error;
|
|
427
|
+
logger_1.logger.warn(lang_1.lang.__('TIMER_TRIGGER_NOT_FOUND', { triggerName: stale.name }));
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return resultingInstances;
|
|
431
|
+
};
|
|
301
432
|
const createDependentResources = async (context, fn, serviceName, existingInstances = [], state, options) => {
|
|
302
433
|
const client = (0, aliyunClient_1.createAliyunClient)(context);
|
|
303
434
|
const instances = [];
|
|
@@ -744,6 +875,7 @@ const createResource = async (context, fn, state) => {
|
|
|
744
875
|
attributes: { ...triggerConfig },
|
|
745
876
|
});
|
|
746
877
|
}
|
|
878
|
+
lifecycleInstances.push(...(await reconcileFc3TimerTriggers(context, fn, [], client)));
|
|
747
879
|
if (fn.domain) {
|
|
748
880
|
logger_1.logger.info(lang_1.lang.__('CREATING_CUSTOM_DOMAIN', { domainName: fn.domain.domain_name }));
|
|
749
881
|
let certConfig;
|
|
@@ -776,7 +908,13 @@ const createResource = async (context, fn, state) => {
|
|
|
776
908
|
mode: 'managed',
|
|
777
909
|
region: context.region,
|
|
778
910
|
definition,
|
|
779
|
-
|
|
911
|
+
// DependentInstance's optional sid is always filled by construction here —
|
|
912
|
+
// widened to ResourceInstance at the state boundary
|
|
913
|
+
instances: [
|
|
914
|
+
fcInstance,
|
|
915
|
+
...lifecycleInstances,
|
|
916
|
+
...dependentInstances,
|
|
917
|
+
],
|
|
780
918
|
lastUpdated: new Date().toISOString(),
|
|
781
919
|
status: 'ready',
|
|
782
920
|
};
|
|
@@ -1225,6 +1363,7 @@ const updateResource = async (context, fn, state, options) => {
|
|
|
1225
1363
|
}
|
|
1226
1364
|
const existingCustomDomain = existingInstances.find((i) => i.type === 'ALIYUN_FC3_CUSTOM_DOMAIN');
|
|
1227
1365
|
const desiredDomain = fn.domain;
|
|
1366
|
+
const timerInstances = await reconcileFc3TimerTriggers(context, fn, existingInstances, client);
|
|
1228
1367
|
if (desiredDomain && !existingCustomDomain) {
|
|
1229
1368
|
logger_1.logger.info(lang_1.lang.__('CREATING_CUSTOM_DOMAIN', { domainName: desiredDomain.domain_name }));
|
|
1230
1369
|
let certConfig;
|
|
@@ -1309,6 +1448,7 @@ const updateResource = async (context, fn, state, options) => {
|
|
|
1309
1448
|
attributes: { ...triggerConfig },
|
|
1310
1449
|
});
|
|
1311
1450
|
}
|
|
1451
|
+
lifecycleInstances.push(...timerInstances);
|
|
1312
1452
|
if (fn.domain) {
|
|
1313
1453
|
lifecycleInstances.push({
|
|
1314
1454
|
type: 'ALIYUN_FC3_CUSTOM_DOMAIN',
|
|
@@ -1320,6 +1460,7 @@ const updateResource = async (context, fn, state, options) => {
|
|
|
1320
1460
|
const existingDependentInstances = existingInstances
|
|
1321
1461
|
.filter((i) => i.type !== 'ALIYUN_FC3_FUNCTION')
|
|
1322
1462
|
.filter((i) => i.type !== 'ALIYUN_FC3_HTTP_TRIGGER')
|
|
1463
|
+
.filter((i) => i.type !== TIMER_TRIGGER_INSTANCE_TYPE)
|
|
1323
1464
|
.filter((i) => i.type !== 'ALIYUN_FC3_CUSTOM_DOMAIN')
|
|
1324
1465
|
.filter((i) => !(typeof fn.iam?.role === 'string' && i.type === 'ALIYUN_RAM_ROLE'))
|
|
1325
1466
|
.filter((i) => !(i.type === 'ALIYUN_RAM_ROLE' && droppedRamRoleIds.has(i.id)))
|
|
@@ -1371,7 +1512,23 @@ const deleteResource = async (context, functionName, logicalId, state) => {
|
|
|
1371
1512
|
const existingState = (0, common_1.getResource)(state, logicalId);
|
|
1372
1513
|
const existingInstances = (existingState?.instances ?? []);
|
|
1373
1514
|
const client = (0, aliyunClient_1.createAliyunClient)(context);
|
|
1374
|
-
// Delete HTTP trigger and custom domain before function (they depend on it)
|
|
1515
|
+
// Delete timers, HTTP trigger and custom domain before function (they depend on it)
|
|
1516
|
+
const timerTriggerInstances = existingInstances.filter((i) => i.type === TIMER_TRIGGER_INSTANCE_TYPE);
|
|
1517
|
+
for (const timerInstance of timerTriggerInstances) {
|
|
1518
|
+
logger_1.logger.info(lang_1.lang.__('DELETING_TIMER_TRIGGER', {
|
|
1519
|
+
triggerName: timerInstance.id,
|
|
1520
|
+
functionName,
|
|
1521
|
+
}));
|
|
1522
|
+
try {
|
|
1523
|
+
await client.fc3.deleteTrigger(functionName, timerInstance.id);
|
|
1524
|
+
logger_1.logger.info(lang_1.lang.__('TIMER_TRIGGER_DELETED', { triggerName: timerInstance.id }));
|
|
1525
|
+
}
|
|
1526
|
+
catch (err) {
|
|
1527
|
+
if (!isTriggerNotFound(err))
|
|
1528
|
+
throw err;
|
|
1529
|
+
logger_1.logger.warn(lang_1.lang.__('TIMER_TRIGGER_NOT_FOUND', { triggerName: timerInstance.id }));
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1375
1532
|
const httpTriggerInstance = existingInstances.find((i) => i.type === 'ALIYUN_FC3_HTTP_TRIGGER');
|
|
1376
1533
|
if (httpTriggerInstance) {
|
|
1377
1534
|
logger_1.logger.info(lang_1.lang.__('DELETING_HTTP_TRIGGER', { triggerName: httpTriggerInstance.id, functionName }));
|
|
@@ -10,7 +10,6 @@ const common_1 = require("../../common");
|
|
|
10
10
|
const function_1 = require("./function");
|
|
11
11
|
const aliyunFc_1 = require("./aliyunFc");
|
|
12
12
|
const functionRunner_1 = require("./functionRunner");
|
|
13
|
-
const node_path_1 = __importDefault(require("node:path"));
|
|
14
13
|
const node_fs_1 = __importDefault(require("node:fs"));
|
|
15
14
|
const utils_1 = require("./utils");
|
|
16
15
|
const lang_1 = require("../../lang");
|
|
@@ -78,18 +77,8 @@ const servEvent = async (req, parsed, iac) => {
|
|
|
78
77
|
let tempDir = null;
|
|
79
78
|
try {
|
|
80
79
|
const { event: aliyunEvent } = await (0, aliyunFc_1.transformToAliyunEvent)(req, parsed.url, parsed.query);
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
if (codePath.endsWith('.zip') && node_fs_1.default.existsSync(codePath)) {
|
|
84
|
-
tempDir = await (0, utils_1.extractZipFile)(codePath);
|
|
85
|
-
codeDir = tempDir;
|
|
86
|
-
}
|
|
87
|
-
else if (node_fs_1.default.existsSync(codePath) && node_fs_1.default.statSync(codePath).isDirectory()) {
|
|
88
|
-
codeDir = codePath;
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
codeDir = node_path_1.default.dirname(codePath);
|
|
92
|
-
}
|
|
80
|
+
const { codeDir, tempDir: resolvedTempDir } = await (0, utils_1.resolveCodeDir)(backendDef.code.path);
|
|
81
|
+
tempDir = resolvedTempDir;
|
|
93
82
|
const funOptions = {
|
|
94
83
|
codeDir,
|
|
95
84
|
functionKey: backendDef.key,
|
|
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.functionsHandler = void 0;
|
|
7
7
|
const common_1 = require("../../common");
|
|
8
8
|
const functionRunner_1 = require("./functionRunner");
|
|
9
|
-
const node_path_1 = __importDefault(require("node:path"));
|
|
10
9
|
const node_fs_1 = __importDefault(require("node:fs"));
|
|
11
10
|
const aliyunFc_1 = require("./aliyunFc");
|
|
12
11
|
const utils_1 = require("./utils");
|
|
@@ -31,18 +30,8 @@ const functionsHandler = async (req, parsed, iac) => {
|
|
|
31
30
|
}
|
|
32
31
|
let tempDir = null;
|
|
33
32
|
try {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
if (codePath.endsWith('.zip') && node_fs_1.default.existsSync(codePath)) {
|
|
37
|
-
tempDir = await (0, utils_1.extractZipFile)(codePath);
|
|
38
|
-
codeDir = tempDir;
|
|
39
|
-
}
|
|
40
|
-
else if (node_fs_1.default.existsSync(codePath) && node_fs_1.default.statSync(codePath).isDirectory()) {
|
|
41
|
-
codeDir = codePath;
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
codeDir = node_path_1.default.dirname(codePath);
|
|
45
|
-
}
|
|
33
|
+
const { codeDir, tempDir: resolvedTempDir } = await (0, utils_1.resolveCodeDir)(fcDef.code.path);
|
|
34
|
+
tempDir = resolvedTempDir;
|
|
46
35
|
const funOptions = {
|
|
47
36
|
codeDir,
|
|
48
37
|
functionKey: fcDef.key,
|
|
@@ -17,10 +17,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
exports.startLocalStack = exports.stopLocal = void 0;
|
|
18
18
|
const localStack_1 = require("../../types/localStack");
|
|
19
19
|
const localServer_1 = require("./localServer");
|
|
20
|
-
Object.defineProperty(exports, "stopLocal", { enumerable: true, get: function () { return localServer_1.stopLocal; } });
|
|
21
20
|
const event_1 = require("./event");
|
|
22
21
|
const function_1 = require("./function");
|
|
23
22
|
const bucket_1 = require("./bucket");
|
|
23
|
+
const timer_1 = require("./timer");
|
|
24
24
|
__exportStar(require("./event"), exports);
|
|
25
25
|
const handlers = [
|
|
26
26
|
{ kind: localStack_1.RouteKind.SI_FUNCTIONS, handler: function_1.functionsHandler },
|
|
@@ -29,5 +29,12 @@ const handlers = [
|
|
|
29
29
|
];
|
|
30
30
|
const startLocalStack = async (iac) => {
|
|
31
31
|
await (0, localServer_1.servLocal)(handlers, iac);
|
|
32
|
+
// functions.*.triggers.timer fire locally too (issue #258)
|
|
33
|
+
(0, timer_1.startLocalTimers)(iac);
|
|
32
34
|
};
|
|
33
35
|
exports.startLocalStack = startLocalStack;
|
|
36
|
+
const stopLocal = async () => {
|
|
37
|
+
(0, timer_1.stopLocalTimers)();
|
|
38
|
+
await (0, localServer_1.stopLocal)();
|
|
39
|
+
};
|
|
40
|
+
exports.stopLocal = stopLocal;
|