@sonamu-kit/tasks 0.0.1
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/.swcrc +17 -0
- package/README.md +7 -0
- package/dist/backend.d.ts +107 -0
- package/dist/backend.d.ts.map +1 -0
- package/dist/backend.js +3 -0
- package/dist/backend.js.map +1 -0
- package/dist/chaos.test.d.ts +2 -0
- package/dist/chaos.test.d.ts.map +1 -0
- package/dist/chaos.test.js +92 -0
- package/dist/chaos.test.js.map +1 -0
- package/dist/client.d.ts +178 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +223 -0
- package/dist/client.js.map +1 -0
- package/dist/client.test.d.ts +2 -0
- package/dist/client.test.d.ts.map +1 -0
- package/dist/client.test.js +339 -0
- package/dist/client.test.js.map +1 -0
- package/dist/config.d.ts +22 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +23 -0
- package/dist/config.js.map +1 -0
- package/dist/config.test.d.ts +2 -0
- package/dist/config.test.d.ts.map +1 -0
- package/dist/config.test.js +24 -0
- package/dist/config.test.js.map +1 -0
- package/dist/core/duration.d.ts +22 -0
- package/dist/core/duration.d.ts.map +1 -0
- package/dist/core/duration.js +64 -0
- package/dist/core/duration.js.map +1 -0
- package/dist/core/duration.test.d.ts +2 -0
- package/dist/core/duration.test.d.ts.map +1 -0
- package/dist/core/duration.test.js +265 -0
- package/dist/core/duration.test.js.map +1 -0
- package/dist/core/error.d.ts +15 -0
- package/dist/core/error.d.ts.map +1 -0
- package/dist/core/error.js +25 -0
- package/dist/core/error.js.map +1 -0
- package/dist/core/error.test.d.ts +2 -0
- package/dist/core/error.test.d.ts.map +1 -0
- package/dist/core/error.test.js +63 -0
- package/dist/core/error.test.js.map +1 -0
- package/dist/core/json.d.ts +5 -0
- package/dist/core/json.d.ts.map +1 -0
- package/dist/core/json.js +3 -0
- package/dist/core/json.js.map +1 -0
- package/dist/core/result.d.ts +22 -0
- package/dist/core/result.d.ts.map +1 -0
- package/dist/core/result.js +22 -0
- package/dist/core/result.js.map +1 -0
- package/dist/core/result.test.d.ts +2 -0
- package/dist/core/result.test.d.ts.map +1 -0
- package/dist/core/result.test.js +19 -0
- package/dist/core/result.test.js.map +1 -0
- package/dist/core/retry.d.ts +21 -0
- package/dist/core/retry.d.ts.map +1 -0
- package/dist/core/retry.js +25 -0
- package/dist/core/retry.js.map +1 -0
- package/dist/core/retry.test.d.ts +2 -0
- package/dist/core/retry.test.d.ts.map +1 -0
- package/dist/core/retry.test.js +37 -0
- package/dist/core/retry.test.js.map +1 -0
- package/dist/core/schema.d.ts +57 -0
- package/dist/core/schema.d.ts.map +1 -0
- package/dist/core/schema.js +4 -0
- package/dist/core/schema.js.map +1 -0
- package/dist/core/step.d.ts +96 -0
- package/dist/core/step.d.ts.map +1 -0
- package/dist/core/step.js +78 -0
- package/dist/core/step.js.map +1 -0
- package/dist/core/step.test.d.ts +2 -0
- package/dist/core/step.test.d.ts.map +1 -0
- package/dist/core/step.test.js +356 -0
- package/dist/core/step.test.js.map +1 -0
- package/dist/core/workflow.d.ts +78 -0
- package/dist/core/workflow.d.ts.map +1 -0
- package/dist/core/workflow.js +46 -0
- package/dist/core/workflow.js.map +1 -0
- package/dist/core/workflow.test.d.ts +2 -0
- package/dist/core/workflow.test.d.ts.map +1 -0
- package/dist/core/workflow.test.js +172 -0
- package/dist/core/workflow.test.js.map +1 -0
- package/dist/database/backend.d.ts +60 -0
- package/dist/database/backend.d.ts.map +1 -0
- package/dist/database/backend.js +387 -0
- package/dist/database/backend.js.map +1 -0
- package/dist/database/backend.test.d.ts +2 -0
- package/dist/database/backend.test.d.ts.map +1 -0
- package/dist/database/backend.test.js +17 -0
- package/dist/database/backend.test.js.map +1 -0
- package/dist/database/backend.testsuite.d.ts +20 -0
- package/dist/database/backend.testsuite.d.ts.map +1 -0
- package/dist/database/backend.testsuite.js +1174 -0
- package/dist/database/backend.testsuite.js.map +1 -0
- package/dist/database/base.d.ts +12 -0
- package/dist/database/base.d.ts.map +1 -0
- package/dist/database/base.js +19 -0
- package/dist/database/base.js.map +1 -0
- package/dist/database/migrations/20251212000000_0_init.js +9 -0
- package/dist/database/migrations/20251212000000_0_init.js.map +1 -0
- package/dist/database/migrations/20251212000000_1_tables.js +88 -0
- package/dist/database/migrations/20251212000000_1_tables.js.map +1 -0
- package/dist/database/migrations/20251212000000_2_fk.js +48 -0
- package/dist/database/migrations/20251212000000_2_fk.js.map +1 -0
- package/dist/database/migrations/20251212000000_3_indexes.js +107 -0
- package/dist/database/migrations/20251212000000_3_indexes.js.map +1 -0
- package/dist/database/pubsub.d.ts +17 -0
- package/dist/database/pubsub.d.ts.map +1 -0
- package/dist/database/pubsub.js +70 -0
- package/dist/database/pubsub.js.map +1 -0
- package/dist/database/pubsub.test.d.ts +2 -0
- package/dist/database/pubsub.test.d.ts.map +1 -0
- package/dist/database/pubsub.test.js +86 -0
- package/dist/database/pubsub.test.js.map +1 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +21 -0
- package/dist/errors.js.map +1 -0
- package/dist/execution.d.ts +82 -0
- package/dist/execution.d.ts.map +1 -0
- package/dist/execution.js +182 -0
- package/dist/execution.js.map +1 -0
- package/dist/execution.test.d.ts +2 -0
- package/dist/execution.test.d.ts.map +1 -0
- package/dist/execution.test.js +556 -0
- package/dist/execution.test.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +12 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +5 -0
- package/dist/internal.js.map +1 -0
- package/dist/practices/01-remote-workflow.d.ts +2 -0
- package/dist/practices/01-remote-workflow.d.ts.map +1 -0
- package/dist/practices/01-remote-workflow.js +69 -0
- package/dist/practices/01-remote-workflow.js.map +1 -0
- package/dist/practices/01-remote.d.ts +2 -0
- package/dist/practices/01-remote.d.ts.map +1 -0
- package/dist/practices/01-remote.js +87 -0
- package/dist/practices/01-remote.js.map +1 -0
- package/dist/practices/02-local.d.ts +2 -0
- package/dist/practices/02-local.d.ts.map +1 -0
- package/dist/practices/02-local.js +84 -0
- package/dist/practices/02-local.js.map +1 -0
- package/dist/practices/03-local-retry.d.ts +2 -0
- package/dist/practices/03-local-retry.d.ts.map +1 -0
- package/dist/practices/03-local-retry.js +85 -0
- package/dist/practices/03-local-retry.js.map +1 -0
- package/dist/practices/04-scheduler-dispose.d.ts +2 -0
- package/dist/practices/04-scheduler-dispose.d.ts.map +1 -0
- package/dist/practices/04-scheduler-dispose.js +65 -0
- package/dist/practices/04-scheduler-dispose.js.map +1 -0
- package/dist/practices/05-router.d.ts +2 -0
- package/dist/practices/05-router.d.ts.map +1 -0
- package/dist/practices/05-router.js +80 -0
- package/dist/practices/05-router.js.map +1 -0
- package/dist/registry.d.ts +33 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +54 -0
- package/dist/registry.js.map +1 -0
- package/dist/registry.test.d.ts +2 -0
- package/dist/registry.test.d.ts.map +1 -0
- package/dist/registry.test.js +95 -0
- package/dist/registry.test.js.map +1 -0
- package/dist/scheduler.d.ts +22 -0
- package/dist/scheduler.d.ts.map +1 -0
- package/dist/scheduler.js +117 -0
- package/dist/scheduler.js.map +1 -0
- package/dist/tasks/index.d.ts +4 -0
- package/dist/tasks/index.d.ts.map +1 -0
- package/dist/tasks/index.js +5 -0
- package/dist/tasks/index.js.map +1 -0
- package/dist/tasks/local-task.d.ts +6 -0
- package/dist/tasks/local-task.d.ts.map +1 -0
- package/dist/tasks/local-task.js +95 -0
- package/dist/tasks/local-task.js.map +1 -0
- package/dist/tasks/remote-task.d.ts +11 -0
- package/dist/tasks/remote-task.d.ts.map +1 -0
- package/dist/tasks/remote-task.js +213 -0
- package/dist/tasks/remote-task.js.map +1 -0
- package/dist/tasks/shared.d.ts +8 -0
- package/dist/tasks/shared.d.ts.map +1 -0
- package/dist/tasks/shared.js +41 -0
- package/dist/tasks/shared.js.map +1 -0
- package/dist/testing/connection.d.ts +7 -0
- package/dist/testing/connection.d.ts.map +1 -0
- package/dist/testing/connection.js +38 -0
- package/dist/testing/connection.js.map +1 -0
- package/dist/types/config.d.ts +44 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +3 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/context.d.ts +18 -0
- package/dist/types/context.d.ts.map +1 -0
- package/dist/types/context.js +4 -0
- package/dist/types/context.js.map +1 -0
- package/dist/types/events.d.ts +43 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/events.js +3 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/task-items.d.ts +12 -0
- package/dist/types/task-items.d.ts.map +1 -0
- package/dist/types/task-items.js +3 -0
- package/dist/types/task-items.js.map +1 -0
- package/dist/types/utils.d.ts +4 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/types/utils.js +8 -0
- package/dist/types/utils.js.map +1 -0
- package/dist/worker.d.ts +61 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +206 -0
- package/dist/worker.js.map +1 -0
- package/dist/worker.test.d.ts +2 -0
- package/dist/worker.test.d.ts.map +1 -0
- package/dist/worker.test.js +1163 -0
- package/dist/worker.test.js.map +1 -0
- package/dist/workflow.d.ts +44 -0
- package/dist/workflow.d.ts.map +1 -0
- package/dist/workflow.js +21 -0
- package/dist/workflow.js.map +1 -0
- package/dist/workflow.test.d.ts +2 -0
- package/dist/workflow.test.d.ts.map +1 -0
- package/dist/workflow.test.js +73 -0
- package/dist/workflow.test.js.map +1 -0
- package/nodemon.json +6 -0
- package/package.json +63 -0
- package/scripts/migrate.ts +11 -0
- package/src/backend.ts +133 -0
- package/src/chaos.test.ts +108 -0
- package/src/client.test.ts +297 -0
- package/src/client.ts +331 -0
- package/src/config.test.ts +23 -0
- package/src/config.ts +35 -0
- package/src/core/duration.test.ts +326 -0
- package/src/core/duration.ts +86 -0
- package/src/core/error.test.ts +77 -0
- package/src/core/error.ts +30 -0
- package/src/core/json.ts +2 -0
- package/src/core/result.test.ts +13 -0
- package/src/core/result.ts +29 -0
- package/src/core/retry.test.ts +41 -0
- package/src/core/retry.ts +29 -0
- package/src/core/schema.ts +74 -0
- package/src/core/step.test.ts +362 -0
- package/src/core/step.ts +152 -0
- package/src/core/workflow.test.ts +184 -0
- package/src/core/workflow.ts +127 -0
- package/src/database/backend.test.ts +16 -0
- package/src/database/backend.testsuite.ts +1376 -0
- package/src/database/backend.ts +655 -0
- package/src/database/base.ts +23 -0
- package/src/database/migrations/20251212000000_0_init.ts +10 -0
- package/src/database/migrations/20251212000000_1_tables.ts +54 -0
- package/src/database/migrations/20251212000000_2_fk.ts +46 -0
- package/src/database/migrations/20251212000000_3_indexes.ts +82 -0
- package/src/database/pubsub.test.ts +92 -0
- package/src/database/pubsub.ts +92 -0
- package/src/execution.test.ts +508 -0
- package/src/execution.ts +291 -0
- package/src/index.ts +7 -0
- package/src/internal.ts +11 -0
- package/src/practices/01-remote-workflow.ts +61 -0
- package/src/registry.test.ts +122 -0
- package/src/registry.ts +65 -0
- package/src/testing/connection.ts +44 -0
- package/src/worker.test.ts +1138 -0
- package/src/worker.ts +281 -0
- package/src/workflow.test.ts +68 -0
- package/src/workflow.ts +84 -0
- package/table_ddl.sql +60 -0
- package/templates/openworkflow.config.ts +22 -0
- package/tsconfig.json +40 -0
- package/tsconfig.test.json +4 -0
- package/vite.config.ts +13 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Knex } from "knex";
|
|
2
|
+
import { DEFAULT_SCHEMA } from "../base";
|
|
3
|
+
|
|
4
|
+
export async function up(knex: Knex): Promise<void> {
|
|
5
|
+
await knex.schema.withSchema(DEFAULT_SCHEMA).createTable("workflow_runs", (table) => {
|
|
6
|
+
table.text("namespace_id").notNullable();
|
|
7
|
+
table.text("id").notNullable();
|
|
8
|
+
table.text("workflow_name").notNullable();
|
|
9
|
+
table.text("version");
|
|
10
|
+
table.text("status").notNullable();
|
|
11
|
+
table.text("idempotency_key");
|
|
12
|
+
table.jsonb("config").notNullable();
|
|
13
|
+
table.jsonb("context");
|
|
14
|
+
table.jsonb("input");
|
|
15
|
+
table.jsonb("output");
|
|
16
|
+
table.jsonb("error");
|
|
17
|
+
table.integer("attempts").notNullable();
|
|
18
|
+
table.text("parent_step_attempt_namespace_id");
|
|
19
|
+
table.text("parent_step_attempt_id");
|
|
20
|
+
table.text("worker_id");
|
|
21
|
+
table.timestamp("available_at", { useTz: true, precision: 3 });
|
|
22
|
+
table.timestamp("deadline_at", { useTz: true, precision: 3 });
|
|
23
|
+
table.timestamp("started_at", { useTz: true, precision: 3 });
|
|
24
|
+
table.timestamp("finished_at", { useTz: true, precision: 3 });
|
|
25
|
+
table.timestamp("created_at", { useTz: true, precision: 3 }).notNullable();
|
|
26
|
+
table.timestamp("updated_at", { useTz: true, precision: 3 }).notNullable();
|
|
27
|
+
table.primary(["namespace_id", "id"]);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
await knex.schema.withSchema(DEFAULT_SCHEMA).createTable("step_attempts", (table) => {
|
|
31
|
+
table.text("namespace_id").notNullable();
|
|
32
|
+
table.text("id").notNullable();
|
|
33
|
+
table.text("workflow_run_id").notNullable();
|
|
34
|
+
table.text("step_name").notNullable();
|
|
35
|
+
table.text("kind").notNullable();
|
|
36
|
+
table.text("status").notNullable();
|
|
37
|
+
table.jsonb("config").notNullable();
|
|
38
|
+
table.jsonb("context");
|
|
39
|
+
table.jsonb("output");
|
|
40
|
+
table.jsonb("error");
|
|
41
|
+
table.text("child_workflow_run_namespace_id");
|
|
42
|
+
table.text("child_workflow_run_id");
|
|
43
|
+
table.timestamp("started_at", { useTz: true, precision: 3 });
|
|
44
|
+
table.timestamp("finished_at", { useTz: true, precision: 3 });
|
|
45
|
+
table.timestamp("created_at", { useTz: true, precision: 3 }).notNullable();
|
|
46
|
+
table.timestamp("updated_at", { useTz: true, precision: 3 }).notNullable();
|
|
47
|
+
table.primary(["namespace_id", "id"]);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export async function down(knex: Knex): Promise<void> {
|
|
52
|
+
await knex.schema.withSchema(DEFAULT_SCHEMA).dropTable("workflow_runs");
|
|
53
|
+
await knex.schema.withSchema(DEFAULT_SCHEMA).dropTable("step_attempts");
|
|
54
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Knex } from "knex";
|
|
2
|
+
import { DEFAULT_SCHEMA } from "../base";
|
|
3
|
+
|
|
4
|
+
export async function up(knex: Knex): Promise<void> {
|
|
5
|
+
await knex.schema.withSchema(DEFAULT_SCHEMA).alterTable("step_attempts", (table) => {
|
|
6
|
+
table
|
|
7
|
+
.foreign(["namespace_id", "workflow_run_id"], "step_attempts_workflow_run_fk")
|
|
8
|
+
.references(["namespace_id", "id"])
|
|
9
|
+
.inTable(`${DEFAULT_SCHEMA}.workflow_runs`)
|
|
10
|
+
.onDelete("cascade");
|
|
11
|
+
table
|
|
12
|
+
.foreign(
|
|
13
|
+
["child_workflow_run_namespace_id", "child_workflow_run_id"],
|
|
14
|
+
"step_attempts_child_workflow_run_fk",
|
|
15
|
+
)
|
|
16
|
+
.references(["namespace_id", "id"])
|
|
17
|
+
.inTable(`${DEFAULT_SCHEMA}.workflow_runs`)
|
|
18
|
+
.onDelete("set null");
|
|
19
|
+
});
|
|
20
|
+
await knex.schema.withSchema(DEFAULT_SCHEMA).alterTable("workflow_runs", (table) => {
|
|
21
|
+
table
|
|
22
|
+
.foreign(
|
|
23
|
+
["parent_step_attempt_namespace_id", "parent_step_attempt_id"],
|
|
24
|
+
"workflow_runs_parent_step_attempt_fk",
|
|
25
|
+
)
|
|
26
|
+
.references(["namespace_id", "id"])
|
|
27
|
+
.inTable(`${DEFAULT_SCHEMA}.step_attempts`)
|
|
28
|
+
.onDelete("set null");
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function down(knex: Knex): Promise<void> {
|
|
33
|
+
await knex.schema.withSchema(DEFAULT_SCHEMA).alterTable("step_attempts", (table) => {
|
|
34
|
+
table.dropForeign(["namespace_id", "workflow_run_id"], "step_attempts_workflow_run_fk");
|
|
35
|
+
table.dropForeign(
|
|
36
|
+
["child_workflow_run_namespace_id", "child_workflow_run_id"],
|
|
37
|
+
"step_attempts_child_workflow_run_fk",
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
await knex.schema.withSchema(DEFAULT_SCHEMA).alterTable("workflow_runs", (table) => {
|
|
41
|
+
table.dropForeign(
|
|
42
|
+
["parent_step_attempt_namespace_id", "parent_step_attempt_id"],
|
|
43
|
+
"workflow_runs_parent_step_attempt_fk",
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { Knex } from "knex";
|
|
2
|
+
import { DEFAULT_SCHEMA } from "../base";
|
|
3
|
+
|
|
4
|
+
export async function up(knex: Knex): Promise<void> {
|
|
5
|
+
await knex.schema.withSchema(DEFAULT_SCHEMA).table("workflow_runs", (table) => {
|
|
6
|
+
table.index(
|
|
7
|
+
["namespace_id", "status", "available_at", "created_at"],
|
|
8
|
+
"workflow_runs_status_available_at_created_at_idx",
|
|
9
|
+
);
|
|
10
|
+
table.index(
|
|
11
|
+
["namespace_id", "workflow_name", "idempotency_key", "created_at"],
|
|
12
|
+
"workflow_runs_workflow_name_idempotency_key_created_at_idx",
|
|
13
|
+
);
|
|
14
|
+
table.index(
|
|
15
|
+
["parent_step_attempt_namespace_id", "parent_step_attempt_id"],
|
|
16
|
+
"workflow_runs_parent_step_idx",
|
|
17
|
+
);
|
|
18
|
+
table.index(["namespace_id", "created_at"], "workflow_runs_created_at_desc_idx");
|
|
19
|
+
table.index(
|
|
20
|
+
["namespace_id", "status", "created_at"],
|
|
21
|
+
"workflow_runs_status_created_at_desc_idx",
|
|
22
|
+
);
|
|
23
|
+
table.index(
|
|
24
|
+
["namespace_id", "workflow_name", "status", "created_at"],
|
|
25
|
+
"workflow_runs_workflow_name_status_created_at_desc_idx",
|
|
26
|
+
);
|
|
27
|
+
});
|
|
28
|
+
await knex.schema.withSchema(DEFAULT_SCHEMA).table("step_attempts", (table) => {
|
|
29
|
+
table.index(
|
|
30
|
+
["namespace_id", "workflow_run_id", "created_at"],
|
|
31
|
+
"step_attempts_workflow_run_created_at_idx",
|
|
32
|
+
);
|
|
33
|
+
table.index(
|
|
34
|
+
["namespace_id", "workflow_run_id", "step_name", "created_at"],
|
|
35
|
+
"step_attempts_workflow_run_step_name_created_at_idx",
|
|
36
|
+
);
|
|
37
|
+
table.index(
|
|
38
|
+
["child_workflow_run_namespace_id", "child_workflow_run_id"],
|
|
39
|
+
"step_attempts_child_workflow_run_idx",
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function down(knex: Knex): Promise<void> {
|
|
45
|
+
await knex.schema.withSchema(DEFAULT_SCHEMA).table("workflow_runs", (table) => {
|
|
46
|
+
table.dropIndex(
|
|
47
|
+
["namespace_id", "status", "available_at", "created_at"],
|
|
48
|
+
"workflow_runs_status_available_at_created_at_idx",
|
|
49
|
+
);
|
|
50
|
+
table.dropIndex(
|
|
51
|
+
["namespace_id", "workflow_name", "idempotency_key", "created_at"],
|
|
52
|
+
"workflow_runs_workflow_name_idempotency_key_created_at_idx",
|
|
53
|
+
);
|
|
54
|
+
table.dropIndex(
|
|
55
|
+
["parent_step_attempt_namespace_id", "parent_step_attempt_id"],
|
|
56
|
+
"workflow_runs_parent_step_idx",
|
|
57
|
+
);
|
|
58
|
+
table.dropIndex(["namespace_id", "created_at"], "workflow_runs_created_at_desc_idx");
|
|
59
|
+
table.dropIndex(
|
|
60
|
+
["namespace_id", "status", "created_at"],
|
|
61
|
+
"workflow_runs_status_created_at_desc_idx",
|
|
62
|
+
);
|
|
63
|
+
table.dropIndex(
|
|
64
|
+
["namespace_id", "workflow_name", "status", "created_at"],
|
|
65
|
+
"workflow_runs_workflow_name_status_created_at_desc_idx",
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
await knex.schema.withSchema(DEFAULT_SCHEMA).table("step_attempts", (table) => {
|
|
69
|
+
table.dropIndex(
|
|
70
|
+
["namespace_id", "workflow_run_id", "created_at"],
|
|
71
|
+
"step_attempts_workflow_run_created_at_idx",
|
|
72
|
+
);
|
|
73
|
+
table.dropIndex(
|
|
74
|
+
["namespace_id", "workflow_run_id", "step_name", "created_at"],
|
|
75
|
+
"step_attempts_workflow_run_step_name_created_at_idx",
|
|
76
|
+
);
|
|
77
|
+
table.dropIndex(
|
|
78
|
+
["child_workflow_run_namespace_id", "child_workflow_run_id"],
|
|
79
|
+
"step_attempts_child_workflow_run_idx",
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import knex, { type Knex } from "knex";
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
3
|
+
import type { Result } from "../core/result";
|
|
4
|
+
import { KNEX_GLOBAL_CONFIG } from "../testing/connection";
|
|
5
|
+
import { type OnSubscribed, PostgresPubSub } from "./pubsub";
|
|
6
|
+
|
|
7
|
+
describe("PostgresPubSub", () => {
|
|
8
|
+
let knexInstance: Knex;
|
|
9
|
+
|
|
10
|
+
beforeEach(async () => {
|
|
11
|
+
knexInstance = knex(KNEX_GLOBAL_CONFIG);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
afterEach(async () => {
|
|
15
|
+
await knexInstance.destroy();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("should create a new pubsub and connect to the database", async () => {
|
|
19
|
+
const pubsub = await PostgresPubSub.create(knexInstance);
|
|
20
|
+
expect(pubsub.destroyed).toBe(false);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("should destroy the pubsub and close the connection", async () => {
|
|
24
|
+
const pubsub = await PostgresPubSub.create(knexInstance);
|
|
25
|
+
expect(pubsub.destroyed).toBe(false);
|
|
26
|
+
await pubsub.destroy();
|
|
27
|
+
expect(pubsub.destroyed).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should not add the same pubsub multiple times", async () => {
|
|
31
|
+
const pubsub = await PostgresPubSub.create(knexInstance);
|
|
32
|
+
const results: Result<string | null>[] = [];
|
|
33
|
+
const callback: OnSubscribed = async (result) => {
|
|
34
|
+
results.push(result);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
pubsub.listenEvent("test", callback);
|
|
38
|
+
pubsub.listenEvent("test", callback);
|
|
39
|
+
|
|
40
|
+
await knexInstance.raw("NOTIFY test, 'test'");
|
|
41
|
+
await sleep(100);
|
|
42
|
+
|
|
43
|
+
expect(results.length).toBe(1);
|
|
44
|
+
expect(results[0]).toStrictEqual({ ok: true, value: "test" });
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("should route notifications to the correct pubsubs by channel", async () => {
|
|
48
|
+
const pubsub = await PostgresPubSub.create(knexInstance);
|
|
49
|
+
const results1: Result<string | null>[] = [];
|
|
50
|
+
const results2: Result<string | null>[] = [];
|
|
51
|
+
|
|
52
|
+
pubsub.listenEvent("test1", (result) => {
|
|
53
|
+
results1.push(result);
|
|
54
|
+
});
|
|
55
|
+
pubsub.listenEvent("test2", (result) => {
|
|
56
|
+
results2.push(result);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
await knexInstance.raw("NOTIFY test1, '!!!'");
|
|
60
|
+
await sleep(100);
|
|
61
|
+
|
|
62
|
+
expect(results1.length).toBe(1);
|
|
63
|
+
expect(results1[0]).toStrictEqual({ ok: true, value: "!!!" });
|
|
64
|
+
expect(results2.length).toBe(0);
|
|
65
|
+
|
|
66
|
+
await knexInstance.raw("NOTIFY test2, '###'");
|
|
67
|
+
await sleep(100);
|
|
68
|
+
|
|
69
|
+
expect(results1.length).toBe(1);
|
|
70
|
+
expect(results2.length).toBe(1);
|
|
71
|
+
expect(results2[0]).toStrictEqual({ ok: true, value: "###" });
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("should payload be null if the payload is empty string", async () => {
|
|
75
|
+
const pubsub = await PostgresPubSub.create(knexInstance);
|
|
76
|
+
const results: Result<string | null>[] = [];
|
|
77
|
+
const callback: OnSubscribed = async (result) => {
|
|
78
|
+
results.push(result);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
pubsub.listenEvent("test", callback);
|
|
82
|
+
await knexInstance.raw(`NOTIFY test`);
|
|
83
|
+
await sleep(100);
|
|
84
|
+
|
|
85
|
+
expect(results.length).toBe(1);
|
|
86
|
+
expect(results[0]).toStrictEqual({ ok: true, value: null });
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
function sleep(ms: number) {
|
|
91
|
+
return new Promise<void>((resolve) => setTimeout(resolve, ms));
|
|
92
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import assert from "assert";
|
|
2
|
+
import type { Knex } from "knex";
|
|
3
|
+
import { err, ok, type Result } from "../core/result";
|
|
4
|
+
|
|
5
|
+
export type OnSubscribed = (result: Result<string | null>) => void | Promise<void>;
|
|
6
|
+
|
|
7
|
+
export class PostgresPubSub {
|
|
8
|
+
private _destroyed = false;
|
|
9
|
+
private _onClosed: () => Promise<void>;
|
|
10
|
+
private _listeners = new Map<string, Set<OnSubscribed>>();
|
|
11
|
+
|
|
12
|
+
// biome-ignore lint/suspicious/noExplicitAny: Knex exposes a connection as any
|
|
13
|
+
private _connection: any | null = null;
|
|
14
|
+
|
|
15
|
+
private constructor(private readonly knex: Knex) {
|
|
16
|
+
// Re-connect to the database when the connection is closed and not destroyed manually
|
|
17
|
+
this._onClosed = (async () => {
|
|
18
|
+
if (this._destroyed) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
await this.connect();
|
|
23
|
+
}).bind(this);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get destroyed() {
|
|
27
|
+
return this._destroyed;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// acquire new raw connection and set up listeners
|
|
31
|
+
async connect() {
|
|
32
|
+
const connection = await this.knex.client.acquireRawConnection();
|
|
33
|
+
connection.on("close", this._onClosed);
|
|
34
|
+
connection.on(
|
|
35
|
+
"notification",
|
|
36
|
+
async ({ channel, payload: rawPayload }: { channel: string; payload: unknown }) => {
|
|
37
|
+
const payload =
|
|
38
|
+
typeof rawPayload === "string" && rawPayload.length !== 0 ? rawPayload : null;
|
|
39
|
+
const listeners = this._listeners.get(channel);
|
|
40
|
+
if (!listeners) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const result = ok(payload);
|
|
45
|
+
await Promise.allSettled(
|
|
46
|
+
Array.from(listeners.values()).map((listener) => Promise.resolve(listener(result))),
|
|
47
|
+
);
|
|
48
|
+
},
|
|
49
|
+
);
|
|
50
|
+
connection.on("error", async (error: Error) => {
|
|
51
|
+
const result = err(error);
|
|
52
|
+
await Promise.allSettled(
|
|
53
|
+
Array.from(this._listeners.values())
|
|
54
|
+
.flatMap((listeners) => Array.from(listeners))
|
|
55
|
+
.map((listener) => Promise.resolve(listener(result))),
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
for (const channel of this._listeners.keys()) {
|
|
60
|
+
connection.query(`LISTEN ${channel}`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
this._connection = connection;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// destroy the listener and close the connection, do not destroy the knex connection
|
|
67
|
+
async destroy() {
|
|
68
|
+
this._destroyed = true;
|
|
69
|
+
this._connection.off("close", this._onClosed);
|
|
70
|
+
await this.knex.client.destroyRawConnection(this._connection);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// create a new listener and connect to the database
|
|
74
|
+
static async create(knex: Knex) {
|
|
75
|
+
const listener = new PostgresPubSub(knex);
|
|
76
|
+
await listener.connect();
|
|
77
|
+
return listener;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// add a new listener to the channel
|
|
81
|
+
listenEvent(channel: string, callback: OnSubscribed) {
|
|
82
|
+
if (!this._listeners.has(channel)) {
|
|
83
|
+
this._connection?.query(`LISTEN ${channel}`);
|
|
84
|
+
this._listeners.set(channel, new Set<OnSubscribed>().add(callback));
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const listeners = this._listeners.get(channel);
|
|
89
|
+
assert(listeners, "Listener channel not found");
|
|
90
|
+
listeners.add(callback);
|
|
91
|
+
}
|
|
92
|
+
}
|