@kubun/store-workflow 0.13.0 → 0.14.0

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/lib/api.d.ts CHANGED
@@ -51,8 +51,9 @@ export type ListFilter = {
51
51
  queue?: string;
52
52
  status?: WorkflowStatus;
53
53
  };
54
- export type WorkflowScheduleRow = Omit<WorkflowSchedule, 'params' | 'enabled'> & {
54
+ export type WorkflowScheduleRow = Omit<WorkflowSchedule, 'params' | 'policy' | 'enabled'> & {
55
55
  params: unknown;
56
+ policy: unknown;
56
57
  enabled: boolean;
57
58
  };
58
59
  export type InsertScheduleInput = {
@@ -63,6 +64,15 @@ export type InsertScheduleInput = {
63
64
  every?: string | null;
64
65
  nextRun: number;
65
66
  enabled: boolean;
67
+ kind?: 'interval' | 'adaptive';
68
+ policy?: unknown | null;
69
+ lastOutcome?: string | null;
70
+ consecutiveCount?: number;
71
+ };
72
+ export type AdvanceScheduleAdaptiveInput = {
73
+ nextRun: number;
74
+ lastOutcome: string | null;
75
+ consecutiveCount: number;
66
76
  };
67
77
  export type WorkflowStoreAPI = {
68
78
  insertInstance(input: InsertInstanceInput): Promise<InsertInstanceResult>;
@@ -82,6 +92,8 @@ export type WorkflowStoreAPI = {
82
92
  listSchedules(): Promise<Array<WorkflowScheduleRow>>;
83
93
  listDueSchedules(now: number): Promise<Array<WorkflowScheduleRow>>;
84
94
  advanceSchedule(id: string, nextRun: number, now: number): Promise<void>;
95
+ advanceScheduleAdaptive(id: string, input: AdvanceScheduleAdaptiveInput, now: number): Promise<void>;
85
96
  setScheduleEnabled(id: string, enabled: boolean, now: number): Promise<void>;
97
+ setSchedulePolicy(id: string, policy: unknown, now: number): Promise<void>;
86
98
  };
87
99
  export declare function createWorkflowStore(db: Kysely<WorkflowTables>, adapter: Adapter): WorkflowStoreAPI;
package/lib/api.js CHANGED
@@ -16,6 +16,7 @@ function rowToSchedule(row) {
16
16
  return {
17
17
  ...row,
18
18
  params: decodeJSON(row.params),
19
+ policy: decodeJSON(row.policy),
19
20
  // Stored as integer 0/1 across adapters.
20
21
  enabled: Number(row.enabled) === 1
21
22
  };
@@ -25,6 +26,7 @@ export function createWorkflowStore(db, adapter) {
25
26
  const encodeJSON = (value)=>adapter.encodeJSON(value);
26
27
  const encodeNextAction = (value)=>value != null ? adapter.encodeJSON(value) : null;
27
28
  const encodeParams = (value)=>adapter.encodeJSON(value);
29
+ const encodePolicy = (value)=>value != null ? adapter.encodeJSON(value) : null;
28
30
  async function readRow(tx, instanceID) {
29
31
  return await tx.selectFrom('kubun_workflow_instances').selectAll().where('instance_id', '=', instanceID).executeTakeFirst();
30
32
  }
@@ -219,6 +221,10 @@ export function createWorkflowStore(db, adapter) {
219
221
  params: encodeParams(input.params),
220
222
  cron: input.cron ?? null,
221
223
  every: input.every ?? null,
224
+ kind: input.kind ?? 'interval',
225
+ policy: encodePolicy(input.policy ?? null),
226
+ last_outcome: input.lastOutcome ?? null,
227
+ consecutive_count: input.consecutiveCount ?? 0,
222
228
  next_run: input.nextRun,
223
229
  enabled: input.enabled ? 1 : 0,
224
230
  created_at: now,
@@ -243,11 +249,25 @@ export function createWorkflowStore(db, adapter) {
243
249
  updated_at: now
244
250
  }).where('id', '=', id).execute();
245
251
  },
252
+ async advanceScheduleAdaptive (id, input, now) {
253
+ await db.updateTable('kubun_workflow_schedules').set({
254
+ next_run: input.nextRun,
255
+ last_outcome: input.lastOutcome,
256
+ consecutive_count: input.consecutiveCount,
257
+ updated_at: now
258
+ }).where('id', '=', id).execute();
259
+ },
246
260
  async setScheduleEnabled (id, enabled, now) {
247
261
  await db.updateTable('kubun_workflow_schedules').set({
248
262
  enabled: enabled ? 1 : 0,
249
263
  updated_at: now
250
264
  }).where('id', '=', id).execute();
265
+ },
266
+ async setSchedulePolicy (id, policy, now) {
267
+ await db.updateTable('kubun_workflow_schedules').set({
268
+ policy: encodePolicy(policy),
269
+ updated_at: now
270
+ }).where('id', '=', id).execute();
251
271
  }
252
272
  };
253
273
  }
package/lib/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { StoreProvider } from '@kubun/db';
2
2
  import type { WorkflowStoreAPI } from './api.js';
3
- export type { CheckpointInput, FailInput, FinishInput, InsertInstanceInput, InsertInstanceResult, InsertReason, InsertScheduleInput, ListFilter, WorkflowInstanceRow, WorkflowNextAction, WorkflowScheduleRow, WorkflowStatus, WorkflowStoreAPI, } from './api.js';
3
+ export type { AdvanceScheduleAdaptiveInput, CheckpointInput, FailInput, FinishInput, InsertInstanceInput, InsertInstanceResult, InsertReason, InsertScheduleInput, ListFilter, WorkflowInstanceRow, WorkflowNextAction, WorkflowScheduleRow, WorkflowStatus, WorkflowStoreAPI, } from './api.js';
4
4
  export { createWorkflowStore } from './api.js';
5
5
  export { workflowStoreDefinition } from './definition.js';
6
6
  export { WORKFLOW_SINGLETON_UNIQUE_INDEX } from './migrations.js';
package/lib/migrations.js CHANGED
@@ -40,7 +40,8 @@ export function getWorkflowMigrations(ctx) {
40
40
  ]).execute();
41
41
  await db.schema.createTable('kubun_workflow_schedules').ifNotExists().addColumn('id', t.text, (col)=>col.notNull().primaryKey()).addColumn('workflow_name', t.text, (col)=>col.notNull()).addColumn('params', t.json, (col)=>col.notNull())// Exactly one of cron / every is set; both nullable, the store persists
42
42
  // them without parsing.
43
- .addColumn('cron', t.text).addColumn('every', t.text)// ms-epoch as bigint (not the `timestamp` column type), matching the
43
+ .addColumn('cron', t.text).addColumn('every', t.text)// Default keeps existing interval inserts working unchanged.
44
+ .addColumn('kind', t.text, (col)=>col.notNull().defaultTo('interval')).addColumn('policy', t.json).addColumn('last_outcome', t.text).addColumn('consecutive_count', 'integer', (col)=>col.notNull().defaultTo(0))// ms-epoch as bigint (not the `timestamp` column type), matching the
44
45
  // instance time columns and compared against an explicit numeric `now`.
45
46
  .addColumn('next_run', t.bigint, (col)=>col.notNull())// 0/1: no portable boolean column type across the adapters.
46
47
  .addColumn('enabled', 'integer', (col)=>col.notNull().defaultTo(1)).addColumn('created_at', t.bigint, (col)=>col.notNull()).addColumn('updated_at', t.bigint, (col)=>col.notNull()).execute();
package/lib/tables.d.ts CHANGED
@@ -24,6 +24,10 @@ export type WorkflowScheduleTable = {
24
24
  params: unknown;
25
25
  cron: string | null;
26
26
  every: string | null;
27
+ kind: string;
28
+ policy: unknown | null;
29
+ last_outcome: string | null;
30
+ consecutive_count: number;
27
31
  next_run: number;
28
32
  enabled: number;
29
33
  created_at: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubun/store-workflow",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
4
4
  "keywords": [],
5
5
  "license": "see LICENSE.md",
6
6
  "sideEffects": false,
@@ -15,14 +15,14 @@
15
15
  "LICENSE.md"
16
16
  ],
17
17
  "dependencies": {
18
- "kysely": "^0.29.5",
19
- "@kubun/db": "^0.13.0",
20
- "@kubun/db-adapter": "^0.13.0"
18
+ "@kubun/db": "^0.14.0",
19
+ "@kubun/db-adapter": "^0.14.0",
20
+ "kysely": "^0.29.5"
21
21
  },
22
22
  "devDependencies": {
23
- "@testcontainers/postgresql": "^12.1.0",
24
- "@kubun/db-better-sqlite": "^0.13.0",
25
- "@kubun/db-postgres": "^0.13.0"
23
+ "@kubun/db-better-sqlite": "^0.14.0",
24
+ "@kubun/db-postgres": "^0.14.0",
25
+ "@testcontainers/postgresql": "^12.1.0"
26
26
  },
27
27
  "publishConfig": {
28
28
  "access": "public"