@h-ai/scheduler 1.0.0-beta.22 → 1.0.0-beta.24
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/README.en.md +1 -1
- package/README.ja.md +1 -1
- package/README.md +1 -1
- package/dist/index.d.ts +1 -14
- package/dist/index.js +20 -110
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/README.en.md
CHANGED
|
@@ -279,7 +279,7 @@ When a scheduled task sends Reach notifications, a scheduler lock or task comple
|
|
|
279
279
|
|
|
280
280
|
Definition refresh, registration, update and deletion are coordinated in arrival order within each process, including a mutation's refresh and commit. Valid concurrent registrations no longer fail merely because snapshots interleave; actual database failures still return errors. close stops entry points and the runner, cancels read waits and operations that have not started writing, then drains active persistent writes. Late snapshots cannot publish or repopulate state afterward. Business callbacks do not hold this queue. Nodes still rely on the shared database, versions and leases; local coordination is not a distributed lock.
|
|
281
281
|
|
|
282
|
-
|
|
282
|
+
Initialize RelDB before calling `scheduler.init()` to create and validate the task, execution and log tables. Incompatible tables cause initialization to fail. Manage execution records and observation logs according to their distinct purposes.
|
|
283
283
|
|
|
284
284
|
## Configuration
|
|
285
285
|
|
package/README.ja.md
CHANGED
|
@@ -279,7 +279,7 @@ Reach 通知を実行するタスクでは、調度ロックやタスク完了
|
|
|
279
279
|
|
|
280
280
|
定義の更新読込、登録、変更、削除は、変更に必要な読込と保存を含めプロセス内で受付順に調整します。正当な並行登録はスナップショットの交錯だけでは失敗せず、実際の DB 障害は失敗として返します。close は入口と runner を停止し、読取待機と未着手の書込を中止して、開始済みの永続書込を待ってから状態を解放します。遅延スナップショットは反映しません。業務コールバックはこのキューを占有しません。ノード間の整合性は引き続き共有 DB、バージョンとリースで保証し、この調整を分散ロックの代替にしません。
|
|
281
281
|
|
|
282
|
-
RelDB
|
|
282
|
+
RelDB の初期化後に `scheduler.init()` でタスク、実行、ログの各テーブルを作成・検証します。構造が一致しない場合は初期化に失敗します。実行台帳と観測ログは用途に応じて管理してください。
|
|
283
283
|
|
|
284
284
|
## 設定
|
|
285
285
|
|
package/README.md
CHANGED
|
@@ -279,7 +279,7 @@ API 任务的 `timeout`(默认 30000 毫秒)覆盖请求和完整响应体
|
|
|
279
279
|
|
|
280
280
|
任务定义的刷新、注册、更新和删除在同一进程内按接收顺序协调,包含写操作所需的刷新与提交。合法并发注册不会仅因快照交错而失败;真实数据库故障仍返回失败。close 先停止入口和 runner,取消只读等待和尚未开始写入的操作,再等待已开始的持久写入;迟到快照不发布,避免关闭后旧操作重新填充任务表。执行回调不占用这条队列;跨节点仍依靠共享数据库、版本与租约,不把进程内协调当分布式锁。
|
|
281
281
|
|
|
282
|
-
RelDB
|
|
282
|
+
先初始化 RelDB,再调用 `scheduler.init()` 创建和校验任务、执行及日志表。结构不符时返回初始化失败。执行账本与观测日志各有用途,应分别管理。
|
|
283
283
|
|
|
284
284
|
## 配置
|
|
285
285
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import * as _h_ai_core from '@h-ai/core';
|
|
3
3
|
import { PaginationOptionsInput, HaiResult, PaginatedResult } from '@h-ai/core';
|
|
4
|
-
import * as _h_ai_reldb from '@h-ai/reldb';
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* @h-ai/scheduler — 配置 Schema
|
|
@@ -855,17 +854,5 @@ interface SchedulerFunctions {
|
|
|
855
854
|
*/
|
|
856
855
|
declare const scheduler: SchedulerFunctions;
|
|
857
856
|
|
|
858
|
-
|
|
859
|
-
* 预检或升级调度器表;旧日志归档并幂等复制,新版执行账本与旧历史隔离。
|
|
860
|
-
* @example
|
|
861
|
-
* ```ts
|
|
862
|
-
* import { migrateScheduler } from '@h-ai/scheduler'
|
|
863
|
-
* const plan = await migrateScheduler({ dryRun: true })
|
|
864
|
-
* ```
|
|
865
|
-
*/
|
|
866
|
-
declare function migrateScheduler(options?: {
|
|
867
|
-
dryRun?: boolean;
|
|
868
|
-
}): Promise<_h_ai_core.HaiResult<_h_ai_reldb.SchemaMigrationPlan>>;
|
|
869
|
-
|
|
870
|
-
export { ExecutionResolutionSchema, HaiSchedulerError, SchedulerConfigSchema, TriggerTaskSchema, migrateScheduler, scheduler };
|
|
857
|
+
export { ExecutionResolutionSchema, HaiSchedulerError, SchedulerConfigSchema, TriggerTaskSchema, scheduler };
|
|
871
858
|
export type { ApiTaskConfig, ExecutionStatus, JsTaskConfig, JsTaskHandler, LogQueryOptions, SchedulerConfig, SchedulerConfigInput, SchedulerExecutionEntry, SchedulerExecutionOperations, SchedulerExecutionState, SchedulerFunctions, SchedulerInitInput, SchedulerLogCleanupPolicy, SchedulerTaskContext, SchedulerTaskExecuteEvent, SchedulerTaskFinishEvent, SchedulerTaskHooks, SchedulerTaskInterruptedEvent, SchedulerTaskStartEvent, TaskDefinition, TaskExecutionLog, TaskExecutionTargetType, TaskHandlerConfig, TaskHandlerKind, TaskLogPersistence, TaskParams, TaskRetryPolicy, TaskTriggerInfo, TaskTriggerType, TaskUpdateInput, TriggerTaskInput };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
import { core, ok, err
|
|
2
|
+
import { core, ok, err } from '@h-ai/core';
|
|
3
3
|
import { reldb, BaseReldbCrudRepository } from '@h-ai/reldb';
|
|
4
4
|
import { randomUUID, createHash } from 'crypto';
|
|
5
5
|
import { isDeepStrictEqual } from 'util';
|
|
@@ -277,6 +277,10 @@ var SchedulerExecutionRepository = class extends BaseReldbCrudRepository {
|
|
|
277
277
|
]
|
|
278
278
|
});
|
|
279
279
|
}
|
|
280
|
+
/** 等待执行表就绪并验证当前字段定义。 */
|
|
281
|
+
ready() {
|
|
282
|
+
return this.ensureSchema();
|
|
283
|
+
}
|
|
280
284
|
/**
|
|
281
285
|
* 只按已初始化的数据库类型选择存储端毫秒时钟。
|
|
282
286
|
* PostgreSQL/MySQL 使用执行时钟,不能使用固定在事务/语句开始处的时间快照。
|
|
@@ -577,6 +581,9 @@ var SchedulerExecutionRepository = class extends BaseReldbCrudRepository {
|
|
|
577
581
|
var logger = core.logger.child({ module: "scheduler", scope: "log-repository" });
|
|
578
582
|
var SCHEDULER_LOG_TABLE = "hai_scheduler_logs";
|
|
579
583
|
var SchedulerLogRepository = class extends BaseReldbCrudRepository {
|
|
584
|
+
ready() {
|
|
585
|
+
return this.ensureSchema();
|
|
586
|
+
}
|
|
580
587
|
constructor(db) {
|
|
581
588
|
super(db, {
|
|
582
589
|
table: SCHEDULER_LOG_TABLE,
|
|
@@ -870,6 +877,9 @@ var JsTaskConfigSchema = z.object({
|
|
|
870
877
|
});
|
|
871
878
|
var TaskHandlerConfigSchema = z.discriminatedUnion("kind", [ApiTaskConfigSchema, JsTaskConfigSchema]);
|
|
872
879
|
var SchedulerTaskRepository = class extends BaseReldbCrudRepository {
|
|
880
|
+
ready() {
|
|
881
|
+
return this.ensureSchema();
|
|
882
|
+
}
|
|
873
883
|
constructor(db) {
|
|
874
884
|
super(db, {
|
|
875
885
|
table: SCHEDULER_TASK_TABLE,
|
|
@@ -1683,108 +1693,6 @@ function compileJsTaskHandler(config) {
|
|
|
1683
1693
|
);
|
|
1684
1694
|
}
|
|
1685
1695
|
}
|
|
1686
|
-
function migrateScheduler(options = {}) {
|
|
1687
|
-
const steps = [
|
|
1688
|
-
{
|
|
1689
|
-
version: 1,
|
|
1690
|
-
name: "explicit-task-idempotency",
|
|
1691
|
-
async needed(sql) {
|
|
1692
|
-
const columns = await reldb.schemaColumns("hai_scheduler_tasks", sql);
|
|
1693
|
-
return columns.success ? ok(columns.data.length > 0 && !columns.data.includes("idempotent")) : columns;
|
|
1694
|
-
},
|
|
1695
|
-
async apply(sql) {
|
|
1696
|
-
return sql.execute("ALTER TABLE hai_scheduler_tasks ADD COLUMN idempotent BOOLEAN NOT NULL DEFAULT FALSE");
|
|
1697
|
-
}
|
|
1698
|
-
},
|
|
1699
|
-
{
|
|
1700
|
-
version: 2,
|
|
1701
|
-
name: "execution-history-snapshots",
|
|
1702
|
-
async needed(sql) {
|
|
1703
|
-
const columns = await reldb.schemaColumns("hai_scheduler_logs", sql);
|
|
1704
|
-
if (!columns.success)
|
|
1705
|
-
return columns;
|
|
1706
|
-
if (columns.data.length && !columns.data.includes("payload"))
|
|
1707
|
-
return ok(true);
|
|
1708
|
-
const archive = await reldb.schemaColumns("hai_scheduler_logs_alpha65", sql);
|
|
1709
|
-
if (!archive.success)
|
|
1710
|
-
return archive;
|
|
1711
|
-
if (!archive.data.length)
|
|
1712
|
-
return ok(false);
|
|
1713
|
-
if (!columns.data.length)
|
|
1714
|
-
return ok(true);
|
|
1715
|
-
const identity = reldb.config?.type === "mysql" ? "CAST(n.id AS BINARY)=CAST(CONCAT('legacy:',CAST(a.id AS CHAR)) AS BINARY)" : "n.id='legacy:' || CAST(a.id AS TEXT)";
|
|
1716
|
-
const missing = await sql.get(`SELECT id FROM hai_scheduler_logs_alpha65 a WHERE NOT EXISTS (SELECT 1 FROM hai_scheduler_logs n WHERE ${identity}) LIMIT 1`);
|
|
1717
|
-
return missing.success ? ok(Boolean(missing.data)) : missing;
|
|
1718
|
-
},
|
|
1719
|
-
async apply(sql) {
|
|
1720
|
-
const columns = await reldb.schemaColumns("hai_scheduler_logs", sql);
|
|
1721
|
-
if (!columns.success)
|
|
1722
|
-
return columns;
|
|
1723
|
-
if (columns.data.length && !columns.data.includes("payload")) {
|
|
1724
|
-
const archive = await reldb.schemaColumns("hai_scheduler_logs_alpha65", sql);
|
|
1725
|
-
if (!archive.success)
|
|
1726
|
-
return archive;
|
|
1727
|
-
if (archive.data.length)
|
|
1728
|
-
return err(HaiCommonError.INIT_FAILED, "Scheduler archive already exists");
|
|
1729
|
-
const renamed = await sql.execute("ALTER TABLE hai_scheduler_logs RENAME TO hai_scheduler_logs_alpha65");
|
|
1730
|
-
if (!renamed.success)
|
|
1731
|
-
return renamed;
|
|
1732
|
-
}
|
|
1733
|
-
const text = reldb.config?.type === "mysql" ? "VARCHAR(255)" : "TEXT";
|
|
1734
|
-
const json = reldb.config?.type === "postgresql" ? "JSONB" : reldb.config?.type === "mysql" ? "JSON" : "TEXT";
|
|
1735
|
-
const created = await sql.execute(`CREATE TABLE IF NOT EXISTS hai_scheduler_logs (id ${text} NOT NULL,occurrence_id ${text},task_id ${text} NOT NULL,task_name ${text} NOT NULL,task_type ${text} NOT NULL,trigger_type ${text} NOT NULL,trigger_source ${text},status ${text} NOT NULL,payload ${json} NOT NULL,started_at BIGINT NOT NULL,finished_at BIGINT NOT NULL,duration INTEGER NOT NULL,CONSTRAINT pk_hai_scheduler_log_v2 PRIMARY KEY(id))`);
|
|
1736
|
-
if (!created.success)
|
|
1737
|
-
return created;
|
|
1738
|
-
for (let offset = 0; ; offset += 100) {
|
|
1739
|
-
const rows = await sql.query(`SELECT CAST(id AS ${reldb.config?.type === "mysql" ? "CHAR" : "TEXT"}) AS id,task_id,task_name,task_type,trigger_type,trigger_source,status,result,error,started_at,finished_at,duration FROM hai_scheduler_logs_alpha65 ORDER BY id LIMIT 100 OFFSET ?`, [offset]);
|
|
1740
|
-
if (!rows.success)
|
|
1741
|
-
return rows;
|
|
1742
|
-
for (const row of rows.data) {
|
|
1743
|
-
const id = `legacy:${row.id}`;
|
|
1744
|
-
const existing = await sql.get("SELECT id FROM hai_scheduler_logs WHERE id=?", [id]);
|
|
1745
|
-
if (!existing.success)
|
|
1746
|
-
return existing;
|
|
1747
|
-
if (existing.data)
|
|
1748
|
-
continue;
|
|
1749
|
-
const payload = {
|
|
1750
|
-
id,
|
|
1751
|
-
taskId: row.task_id,
|
|
1752
|
-
taskName: row.task_name,
|
|
1753
|
-
taskType: row.task_type,
|
|
1754
|
-
triggerType: row.trigger_type,
|
|
1755
|
-
triggerSource: row.trigger_source,
|
|
1756
|
-
status: row.status,
|
|
1757
|
-
result: row.result,
|
|
1758
|
-
error: row.error,
|
|
1759
|
-
startedAt: Number(row.started_at),
|
|
1760
|
-
finishedAt: Number(row.finished_at),
|
|
1761
|
-
duration: Number(row.duration),
|
|
1762
|
-
persistence: { status: "persisted" }
|
|
1763
|
-
};
|
|
1764
|
-
const copied = await sql.execute("INSERT INTO hai_scheduler_logs(id,task_id,task_name,task_type,trigger_type,trigger_source,status,payload,started_at,finished_at,duration) VALUES(?,?,?,?,?,?,?,?,?,?,?)", [
|
|
1765
|
-
id,
|
|
1766
|
-
row.task_id,
|
|
1767
|
-
row.task_name,
|
|
1768
|
-
row.task_type,
|
|
1769
|
-
row.trigger_type,
|
|
1770
|
-
row.trigger_source,
|
|
1771
|
-
row.status,
|
|
1772
|
-
JSON.stringify(payload),
|
|
1773
|
-
row.started_at,
|
|
1774
|
-
row.finished_at,
|
|
1775
|
-
row.duration
|
|
1776
|
-
]);
|
|
1777
|
-
if (!copied.success)
|
|
1778
|
-
return copied;
|
|
1779
|
-
}
|
|
1780
|
-
if (rows.data.length < 100)
|
|
1781
|
-
return ok(void 0);
|
|
1782
|
-
}
|
|
1783
|
-
}
|
|
1784
|
-
}
|
|
1785
|
-
];
|
|
1786
|
-
return reldb.migrateSchema("scheduler", steps, options);
|
|
1787
|
-
}
|
|
1788
1696
|
|
|
1789
1697
|
// src/scheduler-cancellation.ts
|
|
1790
1698
|
async function waitWithSignal(operation, signal) {
|
|
@@ -2528,15 +2436,17 @@ async function initDatabase(parsed) {
|
|
|
2528
2436
|
logger5.error("enableDb is true but @h-ai/reldb is not initialized");
|
|
2529
2437
|
throw new Error(schedulerM("scheduler_reldbNotInitialized"));
|
|
2530
2438
|
}
|
|
2531
|
-
const migration = await migrateScheduler();
|
|
2532
|
-
if (!migration.success)
|
|
2533
|
-
throw migration.error;
|
|
2534
2439
|
taskRepo = new SchedulerTaskRepository(reldb);
|
|
2535
2440
|
logRepo = new SchedulerLogRepository(reldb);
|
|
2536
2441
|
executionRepo = new SchedulerExecutionRepository(reldb);
|
|
2537
|
-
const ready
|
|
2538
|
-
|
|
2539
|
-
|
|
2442
|
+
for (const ready of await Promise.all([
|
|
2443
|
+
taskRepo.ready(),
|
|
2444
|
+
logRepo.ready(),
|
|
2445
|
+
executionRepo.ready()
|
|
2446
|
+
])) {
|
|
2447
|
+
if (!ready.success)
|
|
2448
|
+
throw ready.error;
|
|
2449
|
+
}
|
|
2540
2450
|
setTaskRepository(taskRepo);
|
|
2541
2451
|
setLogRepository(logRepo, {
|
|
2542
2452
|
maxLogs: parsed.maxLogs,
|
|
@@ -2805,6 +2715,6 @@ var scheduler = {
|
|
|
2805
2715
|
}
|
|
2806
2716
|
};
|
|
2807
2717
|
|
|
2808
|
-
export { ExecutionResolutionSchema, HaiSchedulerError, SchedulerConfigSchema, TriggerTaskSchema,
|
|
2718
|
+
export { ExecutionResolutionSchema, HaiSchedulerError, SchedulerConfigSchema, TriggerTaskSchema, scheduler };
|
|
2809
2719
|
//# sourceMappingURL=index.js.map
|
|
2810
2720
|
//# sourceMappingURL=index.js.map
|