@dudousxd/nestjs-durable-store-drizzle 0.22.0 → 0.23.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.
@@ -1,6 +1,6 @@
1
1
  import * as drizzle_orm_pg_core from 'drizzle-orm/pg-core';
2
2
  import { PgDatabase } from 'drizzle-orm/pg-core';
3
- import { StateStore, WorkflowRun, RunStatus, RetentionPolicy, StepCheckpoint, StoreTransaction, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait } from '@dudousxd/nestjs-durable-core';
3
+ import { StateStore, WorkflowRun, RunStatus, RetentionPolicy, StepCheckpoint, StoreTransaction, OwnedLeaseReclaim, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait } from '@dudousxd/nestjs-durable-core';
4
4
  import { SQL } from 'drizzle-orm';
5
5
 
6
6
  /**
@@ -91,7 +91,7 @@ declare class DrizzlePgStateStore implements StateStore {
91
91
  transaction<T>(work: (tx: StoreTransaction) => Promise<T>): Promise<T>;
92
92
  /** Crash recovery. Namespace-scoped: a worker re-drives only its own tenant's runs. */
93
93
  listIncompleteRuns(namespace?: string): Promise<WorkflowRun[]>;
94
- listOrphanedRuns(nowMs: number, limit: number, namespace?: string): Promise<WorkflowRun[]>;
94
+ listOrphanedRuns(nowMs: number, limit: number, namespace?: string, ownedBy?: OwnedLeaseReclaim): Promise<WorkflowRun[]>;
95
95
  /** FIFO pick-up, served by `durable_workflow_runs_namespace_status_idx`. The engine then leases
96
96
  * each run with {@link tryLockRun}; the listing itself takes no locks (it runs outside a tx). */
97
97
  listPendingRuns(limit: number, namespace?: string): Promise<WorkflowRun[]>;
@@ -1,6 +1,6 @@
1
1
  import * as drizzle_orm_pg_core from 'drizzle-orm/pg-core';
2
2
  import { PgDatabase } from 'drizzle-orm/pg-core';
3
- import { StateStore, WorkflowRun, RunStatus, RetentionPolicy, StepCheckpoint, StoreTransaction, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait } from '@dudousxd/nestjs-durable-core';
3
+ import { StateStore, WorkflowRun, RunStatus, RetentionPolicy, StepCheckpoint, StoreTransaction, OwnedLeaseReclaim, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait } from '@dudousxd/nestjs-durable-core';
4
4
  import { SQL } from 'drizzle-orm';
5
5
 
6
6
  /**
@@ -91,7 +91,7 @@ declare class DrizzlePgStateStore implements StateStore {
91
91
  transaction<T>(work: (tx: StoreTransaction) => Promise<T>): Promise<T>;
92
92
  /** Crash recovery. Namespace-scoped: a worker re-drives only its own tenant's runs. */
93
93
  listIncompleteRuns(namespace?: string): Promise<WorkflowRun[]>;
94
- listOrphanedRuns(nowMs: number, limit: number, namespace?: string): Promise<WorkflowRun[]>;
94
+ listOrphanedRuns(nowMs: number, limit: number, namespace?: string, ownedBy?: OwnedLeaseReclaim): Promise<WorkflowRun[]>;
95
95
  /** FIFO pick-up, served by `durable_workflow_runs_namespace_status_idx`. The engine then leases
96
96
  * each run with {@link tryLockRun}; the listing itself takes no locks (it runs outside a tx). */
97
97
  listPendingRuns(limit: number, namespace?: string): Promise<WorkflowRun[]>;
package/dist/pg/index.js CHANGED
@@ -310,7 +310,14 @@ function anyOf(conditions) {
310
310
  }
311
311
  __name(anyOf, "anyOf");
312
312
  function namespaceFilter(namespace) {
313
- return namespace === void 0 ? void 0 : eq(durableWorkflowRuns.namespace, namespace);
313
+ if (namespace === void 0) return void 0;
314
+ if (namespace === DEFAULT_NAMESPACE || namespace === "") {
315
+ return inArray(durableWorkflowRuns.namespace, [
316
+ DEFAULT_NAMESPACE,
317
+ ""
318
+ ]);
319
+ }
320
+ return eq(durableWorkflowRuns.namespace, namespace);
314
321
  }
315
322
  __name(namespaceFilter, "namespaceFilter");
316
323
  function hasTag(tag) {
@@ -509,12 +516,17 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
509
516
  ]), namespaceFilter(namespace), this.scope()));
510
517
  return rows.map(fromRunRow);
511
518
  }
512
- async listOrphanedRuns(nowMs, limit, namespace) {
519
+ async listOrphanedRuns(nowMs, limit, namespace, ownedBy) {
513
520
  if (limit <= 0) return [];
514
521
  const rows = await this.db.select().from(durableWorkflowRuns).where(and(inArray(durableWorkflowRuns.status, [
515
522
  "running",
516
523
  "cancelling"
517
- ]), or(isNull(durableWorkflowRuns.lockedUntil), lte(durableWorkflowRuns.lockedUntil, new Date(nowMs))), namespaceFilter(namespace), this.scope())).orderBy(asc(durableWorkflowRuns.createdAt)).limit(limit);
524
+ ]), or(
525
+ isNull(durableWorkflowRuns.lockedUntil),
526
+ lte(durableWorkflowRuns.lockedUntil, new Date(nowMs)),
527
+ // A live lease left by a previous process with the caller's own instance id.
528
+ ownedBy ? and(eq(durableWorkflowRuns.lockedBy, ownedBy.owner), lt(durableWorkflowRuns.lockedUntil, new Date(ownedBy.lockedBefore))) : void 0
529
+ ), namespaceFilter(namespace), this.scope())).orderBy(asc(durableWorkflowRuns.createdAt)).limit(limit);
518
530
  return rows.map(fromRunRow);
519
531
  }
520
532
  /** FIFO pick-up, served by `durable_workflow_runs_namespace_status_idx`. The engine then leases
@@ -932,7 +944,7 @@ function toRunRow(run) {
932
944
  tags: run.tags ?? null,
933
945
  searchAttributes: run.searchAttributes ?? null,
934
946
  priority: run.priority ?? null,
935
- namespace: run.namespace ?? DEFAULT_NAMESPACE,
947
+ namespace: run.namespace || DEFAULT_NAMESPACE,
936
948
  origin: run.origin ?? null,
937
949
  createdAt: run.createdAt,
938
950
  updatedAt: run.updatedAt
@@ -955,7 +967,7 @@ function toRunPatch(patch) {
955
967
  if ("tags" in patch) row.tags = patch.tags ?? null;
956
968
  if ("searchAttributes" in patch) row.searchAttributes = patch.searchAttributes ?? null;
957
969
  if ("priority" in patch) row.priority = patch.priority ?? null;
958
- if ("namespace" in patch) row.namespace = patch.namespace ?? DEFAULT_NAMESPACE;
970
+ if ("namespace" in patch) row.namespace = patch.namespace || DEFAULT_NAMESPACE;
959
971
  if ("origin" in patch) row.origin = patch.origin ?? null;
960
972
  if (patch.createdAt != null) row.createdAt = patch.createdAt;
961
973
  if (patch.updatedAt != null) row.updatedAt = patch.updatedAt;
@@ -979,7 +991,7 @@ function fromRunRow(row) {
979
991
  tags: row.tags ?? void 0,
980
992
  searchAttributes: row.searchAttributes ?? void 0,
981
993
  priority: row.priority ?? void 0,
982
- namespace: row.namespace,
994
+ namespace: row.namespace || DEFAULT_NAMESPACE,
983
995
  origin: row.origin ?? void 0,
984
996
  createdAt: row.createdAt,
985
997
  updatedAt: row.updatedAt