@h-ai/scheduler 0.1.0-alpha5
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/LICENSE +202 -0
- package/README.md +201 -0
- package/dist/index.d.ts +364 -0
- package/dist/index.js +1416 -0
- package/dist/index.js.map +1 -0
- package/package.json +46 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import * as _h_ai_core from '@h-ai/core';
|
|
3
|
+
import { PaginationOptionsInput, HaiResult, PaginatedResult } from '@h-ai/core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @h-ai/scheduler — 配置 Schema
|
|
7
|
+
*
|
|
8
|
+
* 本文件定义定时任务模块的配置结构,使用 Zod 进行运行时校验。
|
|
9
|
+
* @module scheduler-config
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 调度器配置 Zod Schema
|
|
14
|
+
*
|
|
15
|
+
* 所有字段均可选,提供合理默认值。
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const config = SchedulerConfigSchema.parse({
|
|
20
|
+
* enableDb: true,
|
|
21
|
+
* tickInterval: 1000,
|
|
22
|
+
* })
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare const SchedulerConfigSchema: z.ZodObject<{
|
|
26
|
+
enableDb: z.ZodDefault<z.ZodBoolean>;
|
|
27
|
+
tickInterval: z.ZodDefault<z.ZodNumber>;
|
|
28
|
+
lockExpireMs: z.ZodDefault<z.ZodNumber>;
|
|
29
|
+
nodeId: z.ZodOptional<z.ZodString>;
|
|
30
|
+
maxLogs: z.ZodOptional<z.ZodNumber>;
|
|
31
|
+
retentionDays: z.ZodOptional<z.ZodNumber>;
|
|
32
|
+
}, z.core.$strip>;
|
|
33
|
+
/** 调度器配置类型 */
|
|
34
|
+
type SchedulerConfig = z.infer<typeof SchedulerConfigSchema>;
|
|
35
|
+
/** 调度器配置输入类型 */
|
|
36
|
+
type SchedulerConfigInput = z.input<typeof SchedulerConfigSchema>;
|
|
37
|
+
|
|
38
|
+
declare const HaiSchedulerError: {
|
|
39
|
+
readonly NOT_INITIALIZED: _h_ai_core.HaiErrorDef;
|
|
40
|
+
readonly INIT_FAILED: _h_ai_core.HaiErrorDef;
|
|
41
|
+
readonly CONFIG_ERROR: _h_ai_core.HaiErrorDef;
|
|
42
|
+
readonly TASK_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
43
|
+
readonly TASK_ALREADY_EXISTS: _h_ai_core.HaiErrorDef;
|
|
44
|
+
readonly INVALID_CRON: _h_ai_core.HaiErrorDef;
|
|
45
|
+
readonly EXECUTION_FAILED: _h_ai_core.HaiErrorDef;
|
|
46
|
+
readonly JS_EXECUTION_FAILED: _h_ai_core.HaiErrorDef;
|
|
47
|
+
readonly API_EXECUTION_FAILED: _h_ai_core.HaiErrorDef;
|
|
48
|
+
readonly DB_SAVE_FAILED: _h_ai_core.HaiErrorDef;
|
|
49
|
+
readonly ALREADY_RUNNING: _h_ai_core.HaiErrorDef;
|
|
50
|
+
readonly NOT_RUNNING: _h_ai_core.HaiErrorDef;
|
|
51
|
+
readonly LOCK_ACQUIRE_FAILED: _h_ai_core.HaiErrorDef;
|
|
52
|
+
readonly JS_COMPILE_FAILED: _h_ai_core.HaiErrorDef;
|
|
53
|
+
readonly HOOK_EXECUTION_FAILED: _h_ai_core.HaiErrorDef;
|
|
54
|
+
};
|
|
55
|
+
/** 任务参数字典(键值对) */
|
|
56
|
+
type TaskParams = Record<string, unknown>;
|
|
57
|
+
/** 任务触发类型 */
|
|
58
|
+
type TaskTriggerType = 'scheduled' | 'manual';
|
|
59
|
+
/** 任务处理器类型 */
|
|
60
|
+
type TaskHandlerKind = 'api' | 'js';
|
|
61
|
+
/** 失败重试策略 */
|
|
62
|
+
interface TaskRetryPolicy {
|
|
63
|
+
/** 最大尝试次数(包含首次执行,最小 1) */
|
|
64
|
+
maxAttempts: number;
|
|
65
|
+
/** 每次重试前的退避时间(毫秒);按顺序对应第 2、3... 次尝试 */
|
|
66
|
+
backoffMs?: number[];
|
|
67
|
+
}
|
|
68
|
+
/** 任务执行目标类型 */
|
|
69
|
+
type TaskExecutionTargetType = TaskHandlerKind | 'hook';
|
|
70
|
+
/**
|
|
71
|
+
* API 任务配置
|
|
72
|
+
*/
|
|
73
|
+
interface ApiTaskConfig {
|
|
74
|
+
/** 处理器类型 */
|
|
75
|
+
kind: 'api';
|
|
76
|
+
/** 请求 URL */
|
|
77
|
+
url: string;
|
|
78
|
+
/** HTTP 方法(默认 GET) */
|
|
79
|
+
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
80
|
+
/** 请求头 */
|
|
81
|
+
headers?: Record<string, string>;
|
|
82
|
+
/** 请求体(POST/PUT/PATCH 时使用) */
|
|
83
|
+
body?: unknown;
|
|
84
|
+
/** 超时时间,单位毫秒(默认 30000) */
|
|
85
|
+
timeout?: number;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* JS 任务配置
|
|
89
|
+
*
|
|
90
|
+
* `code` 为 JS 函数字符串,例如:
|
|
91
|
+
* `(context) => ({ taskId: context.task.id, params: context.params })`
|
|
92
|
+
*/
|
|
93
|
+
interface JsTaskConfig {
|
|
94
|
+
/** 处理器类型 */
|
|
95
|
+
kind: 'js';
|
|
96
|
+
/** JS 函数字符串 */
|
|
97
|
+
code: string;
|
|
98
|
+
/** 软超时时间,单位毫秒 */
|
|
99
|
+
timeout?: number;
|
|
100
|
+
}
|
|
101
|
+
/** 统一任务处理器配置 */
|
|
102
|
+
type TaskHandlerConfig = ApiTaskConfig | JsTaskConfig;
|
|
103
|
+
/**
|
|
104
|
+
* 任务定义
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* const jsTask: TaskDefinition = {
|
|
109
|
+
* id: 'cleanup',
|
|
110
|
+
* name: '清理过期数据',
|
|
111
|
+
* cron: '0 2 * * *',
|
|
112
|
+
* params: { channel: 'nightly' },
|
|
113
|
+
* handler: {
|
|
114
|
+
* kind: 'js',
|
|
115
|
+
* code: '(context) => ({ taskId: context.task.id, params: context.params })',
|
|
116
|
+
* },
|
|
117
|
+
* }
|
|
118
|
+
*
|
|
119
|
+
* const apiTask: TaskDefinition = {
|
|
120
|
+
* id: 'health-check',
|
|
121
|
+
* name: '健康检查',
|
|
122
|
+
* cron: '*/5 * * * *',
|
|
123
|
+
* handler: {
|
|
124
|
+
* kind: 'api',
|
|
125
|
+
* url: 'https://api.example.com/health',
|
|
126
|
+
* method: 'GET',
|
|
127
|
+
* },
|
|
128
|
+
* }
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
interface TaskDefinition {
|
|
132
|
+
/** 任务唯一标识 */
|
|
133
|
+
id: string;
|
|
134
|
+
/** 任务名称 */
|
|
135
|
+
name: string;
|
|
136
|
+
/** 任务描述 */
|
|
137
|
+
description?: string;
|
|
138
|
+
/** cron 表达式(标准 5 字段:分 时 日 月 周) */
|
|
139
|
+
cron: string;
|
|
140
|
+
/** 是否启用(默认 true) */
|
|
141
|
+
enabled?: boolean;
|
|
142
|
+
/** 是否在执行后自动删除(默认 false) */
|
|
143
|
+
deleteAfterRun?: boolean;
|
|
144
|
+
/** 失败重试策略 */
|
|
145
|
+
retry?: TaskRetryPolicy;
|
|
146
|
+
/** 通用参数 */
|
|
147
|
+
params?: TaskParams;
|
|
148
|
+
/** 任务处理器;为空时可由全局事件回调统一处理 */
|
|
149
|
+
handler?: TaskHandlerConfig;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* 任务触发信息
|
|
153
|
+
*/
|
|
154
|
+
interface TaskTriggerInfo {
|
|
155
|
+
/** 触发类型 */
|
|
156
|
+
type: TaskTriggerType;
|
|
157
|
+
/** 触发来源;定时触发时通常为 null,手工触发时可指定渠道 */
|
|
158
|
+
source: string | null;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* 任务执行上下文
|
|
162
|
+
*/
|
|
163
|
+
interface SchedulerTaskContext {
|
|
164
|
+
/** 当前任务 */
|
|
165
|
+
task: TaskDefinition;
|
|
166
|
+
/** 任务 ID */
|
|
167
|
+
taskId: string;
|
|
168
|
+
/** 当前参数快照 */
|
|
169
|
+
params: TaskParams;
|
|
170
|
+
/** 触发信息 */
|
|
171
|
+
trigger: TaskTriggerInfo;
|
|
172
|
+
}
|
|
173
|
+
/** 编译后的 JS 任务函数 */
|
|
174
|
+
type JsTaskHandler = (context: SchedulerTaskContext) => unknown | Promise<unknown>;
|
|
175
|
+
/** 任务开始事件 */
|
|
176
|
+
interface SchedulerTaskStartEvent {
|
|
177
|
+
/** 当前任务 */
|
|
178
|
+
task: TaskDefinition;
|
|
179
|
+
/** 触发信息 */
|
|
180
|
+
trigger: TaskTriggerInfo;
|
|
181
|
+
/** 开始时间(Unix 时间戳,毫秒) */
|
|
182
|
+
startedAt: number;
|
|
183
|
+
}
|
|
184
|
+
/** 任务执行事件 */
|
|
185
|
+
interface SchedulerTaskExecuteEvent extends SchedulerTaskStartEvent {
|
|
186
|
+
/** 执行上下文 */
|
|
187
|
+
context: SchedulerTaskContext;
|
|
188
|
+
}
|
|
189
|
+
/** 任务中断事件 */
|
|
190
|
+
interface SchedulerTaskInterruptedEvent extends SchedulerTaskStartEvent {
|
|
191
|
+
/** 中断时间(Unix 时间戳,毫秒) */
|
|
192
|
+
interruptedAt: number;
|
|
193
|
+
/** 中断原因 */
|
|
194
|
+
reason: string;
|
|
195
|
+
}
|
|
196
|
+
/** 任务结束事件 */
|
|
197
|
+
interface SchedulerTaskFinishEvent extends SchedulerTaskStartEvent {
|
|
198
|
+
/** 结束时间(Unix 时间戳,毫秒) */
|
|
199
|
+
finishedAt: number;
|
|
200
|
+
/** 执行日志 */
|
|
201
|
+
log: TaskExecutionLog;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* 全局任务生命周期回调
|
|
205
|
+
*
|
|
206
|
+
* - `onTaskExecute` 可用于统一处理无内置 handler 的任务
|
|
207
|
+
* - `onTaskStart` / `onTaskInterrupted` / `onTaskFinish` 用于观测生命周期
|
|
208
|
+
*/
|
|
209
|
+
interface SchedulerTaskHooks {
|
|
210
|
+
/** 任务开始前触发 */
|
|
211
|
+
onTaskStart?: (event: SchedulerTaskStartEvent) => void | Promise<void>;
|
|
212
|
+
/** 当任务无内置 handler 时,可在此统一执行 */
|
|
213
|
+
onTaskExecute?: (event: SchedulerTaskExecuteEvent) => unknown | Promise<unknown>;
|
|
214
|
+
/** 任务被中断时触发 */
|
|
215
|
+
onTaskInterrupted?: (event: SchedulerTaskInterruptedEvent) => void | Promise<void>;
|
|
216
|
+
/** 任务结束时触发 */
|
|
217
|
+
onTaskFinish?: (event: SchedulerTaskFinishEvent) => void | Promise<void>;
|
|
218
|
+
}
|
|
219
|
+
/** 执行状态 */
|
|
220
|
+
type ExecutionStatus = 'success' | 'failed' | 'interrupted';
|
|
221
|
+
/**
|
|
222
|
+
* 任务执行日志
|
|
223
|
+
*/
|
|
224
|
+
interface TaskExecutionLog {
|
|
225
|
+
/** 日志 ID */
|
|
226
|
+
id: number;
|
|
227
|
+
/** 任务 ID */
|
|
228
|
+
taskId: string;
|
|
229
|
+
/** 任务名称 */
|
|
230
|
+
taskName: string;
|
|
231
|
+
/** 执行目标类型 */
|
|
232
|
+
taskType: TaskExecutionTargetType;
|
|
233
|
+
/** 触发类型 */
|
|
234
|
+
triggerType: TaskTriggerType;
|
|
235
|
+
/** 触发来源 */
|
|
236
|
+
triggerSource: string | null;
|
|
237
|
+
/** 执行状态 */
|
|
238
|
+
status: ExecutionStatus;
|
|
239
|
+
/** 执行结果(JSON 字符串) */
|
|
240
|
+
result: string | null;
|
|
241
|
+
/** 错误或中断信息 */
|
|
242
|
+
error: string | null;
|
|
243
|
+
/** 开始时间(Unix 时间戳,毫秒) */
|
|
244
|
+
startedAt: number;
|
|
245
|
+
/** 结束时间(Unix 时间戳,毫秒) */
|
|
246
|
+
finishedAt: number;
|
|
247
|
+
/** 执行耗时(毫秒) */
|
|
248
|
+
duration: number;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* 执行日志查询选项
|
|
252
|
+
*/
|
|
253
|
+
interface LogQueryOptions {
|
|
254
|
+
/** 按任务 ID 过滤 */
|
|
255
|
+
taskId?: string;
|
|
256
|
+
/** 按状态过滤 */
|
|
257
|
+
status?: ExecutionStatus;
|
|
258
|
+
/** 按触发类型过滤 */
|
|
259
|
+
triggerType?: TaskTriggerType;
|
|
260
|
+
/** 按触发来源过滤 */
|
|
261
|
+
triggerSource?: string;
|
|
262
|
+
/** 仅查询开始时间 >= startedAfter 的日志(Unix 时间戳毫秒) */
|
|
263
|
+
startedAfter?: number;
|
|
264
|
+
/** 仅查询开始时间 <= startedBefore 的日志(Unix 时间戳毫秒) */
|
|
265
|
+
startedBefore?: number;
|
|
266
|
+
/** 分页参数(page 从 1 开始,pageSize 默认 20) */
|
|
267
|
+
pagination?: PaginationOptionsInput;
|
|
268
|
+
}
|
|
269
|
+
/** 日志清理策略 */
|
|
270
|
+
interface SchedulerLogCleanupPolicy {
|
|
271
|
+
/** 最多保留日志条数 */
|
|
272
|
+
maxLogs?: number;
|
|
273
|
+
/** 最多保留天数 */
|
|
274
|
+
retentionDays?: number;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* 任务更新输入
|
|
278
|
+
*
|
|
279
|
+
* 可更新的字段包括:名称、cron 表达式、启用状态、参数、处理器。
|
|
280
|
+
* 任务 ID 不可更新。
|
|
281
|
+
*/
|
|
282
|
+
interface TaskUpdateInput {
|
|
283
|
+
/** 任务名称 */
|
|
284
|
+
name?: string;
|
|
285
|
+
/** 任务描述 */
|
|
286
|
+
description?: string;
|
|
287
|
+
/** cron 表达式 */
|
|
288
|
+
cron?: string;
|
|
289
|
+
/** 是否启用 */
|
|
290
|
+
enabled?: boolean;
|
|
291
|
+
/** 是否在执行后自动删除 */
|
|
292
|
+
deleteAfterRun?: boolean;
|
|
293
|
+
/** 失败重试策略;传 null 表示清空重试策略 */
|
|
294
|
+
retry?: TaskRetryPolicy | null;
|
|
295
|
+
/** 通用参数 */
|
|
296
|
+
params?: TaskParams;
|
|
297
|
+
/** 任务处理器;传 null 表示清空内置处理器 */
|
|
298
|
+
handler?: TaskHandlerConfig | null;
|
|
299
|
+
}
|
|
300
|
+
/** 手工触发输入 */
|
|
301
|
+
interface TriggerTaskInput {
|
|
302
|
+
/** 手工触发来源(例如 admin-console / api / cli) */
|
|
303
|
+
source?: string;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* 调度器初始化输入
|
|
307
|
+
*
|
|
308
|
+
* 包含调度器配置、可选的预定义任务列表,以及可选的全局生命周期回调。
|
|
309
|
+
*/
|
|
310
|
+
interface SchedulerInitInput extends SchedulerConfigInput {
|
|
311
|
+
/** 预定义任务列表(从配置中加载,初始化时自动注册) */
|
|
312
|
+
tasks?: TaskDefinition[];
|
|
313
|
+
/** 全局任务生命周期回调 */
|
|
314
|
+
hooks?: SchedulerTaskHooks;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* 调度器函数接口
|
|
318
|
+
*/
|
|
319
|
+
interface SchedulerFunctions {
|
|
320
|
+
/** 初始化调度器 */
|
|
321
|
+
init: (config?: SchedulerInitInput) => Promise<HaiResult<void>>;
|
|
322
|
+
/** 注册任务 */
|
|
323
|
+
register: (task: TaskDefinition) => Promise<HaiResult<void>>;
|
|
324
|
+
/** 注销任务 */
|
|
325
|
+
unregister: (taskId: string) => Promise<HaiResult<void>>;
|
|
326
|
+
/** 更新任务 */
|
|
327
|
+
updateTask: (taskId: string, updates: TaskUpdateInput) => Promise<HaiResult<void>>;
|
|
328
|
+
/** 启动调度 */
|
|
329
|
+
start: () => HaiResult<void>;
|
|
330
|
+
/** 停止调度 */
|
|
331
|
+
stop: () => HaiResult<void>;
|
|
332
|
+
/** 手动触发任务 */
|
|
333
|
+
trigger: (taskId: string, options?: TriggerTaskInput) => Promise<HaiResult<TaskExecutionLog>>;
|
|
334
|
+
/** 查询执行日志 */
|
|
335
|
+
getLogs: (options?: LogQueryOptions) => Promise<HaiResult<PaginatedResult<TaskExecutionLog>>>;
|
|
336
|
+
/** 设置全局任务生命周期回调 */
|
|
337
|
+
setHooks: (hooks: SchedulerTaskHooks) => HaiResult<void>;
|
|
338
|
+
/** 清空全局任务生命周期回调 */
|
|
339
|
+
clearHooks: () => HaiResult<void>;
|
|
340
|
+
/** 获取已注册任务列表 */
|
|
341
|
+
readonly tasks: ReadonlyMap<string, TaskDefinition>;
|
|
342
|
+
/** 当前配置 */
|
|
343
|
+
readonly config: SchedulerConfig | null;
|
|
344
|
+
/** 当前生命周期回调 */
|
|
345
|
+
readonly hooks: Readonly<SchedulerTaskHooks>;
|
|
346
|
+
/** 是否已初始化 */
|
|
347
|
+
readonly isInitialized: boolean;
|
|
348
|
+
/** 调度器是否正在运行 */
|
|
349
|
+
readonly isRunning: boolean;
|
|
350
|
+
/** 关闭调度器 */
|
|
351
|
+
close: () => Promise<void>;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* @h-ai/scheduler — 定时任务服务主入口
|
|
356
|
+
*
|
|
357
|
+
* 本文件仅负责生命周期管理(init / close)与 API 编排;
|
|
358
|
+
* 具体业务逻辑委托给 scheduler-functions.ts / scheduler-runner.ts。
|
|
359
|
+
* @module scheduler-main
|
|
360
|
+
*/
|
|
361
|
+
|
|
362
|
+
declare const scheduler: SchedulerFunctions;
|
|
363
|
+
|
|
364
|
+
export { type ApiTaskConfig, type ExecutionStatus, HaiSchedulerError, type JsTaskConfig, type JsTaskHandler, type LogQueryOptions, type SchedulerConfig, type SchedulerConfigInput, SchedulerConfigSchema, type SchedulerFunctions, type SchedulerInitInput, type SchedulerLogCleanupPolicy, type SchedulerTaskContext, type SchedulerTaskExecuteEvent, type SchedulerTaskFinishEvent, type SchedulerTaskHooks, type SchedulerTaskInterruptedEvent, type SchedulerTaskStartEvent, type TaskDefinition, type TaskExecutionLog, type TaskExecutionTargetType, type TaskHandlerConfig, type TaskHandlerKind, type TaskParams, type TaskRetryPolicy, type TaskTriggerInfo, type TaskTriggerType, type TaskUpdateInput, type TriggerTaskInput, scheduler };
|