@dudousxd/nestjs-durable-store-mikro-orm 0.34.0 → 0.35.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/dist/index.cjs +410 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +23 -2
- package/dist/index.d.ts +23 -2
- package/dist/index.js +410 -2
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as _mikro_orm_core from '@mikro-orm/core';
|
|
2
2
|
import { EntityRepository, EntityRepositoryType, EntitySchema, MikroORM } from '@mikro-orm/core';
|
|
3
|
-
import { StepKind, RunStatus, StateStore, WorkflowRun, RetentionPolicy,
|
|
3
|
+
import { StepKind, RunStatus, StateStore, StepResultSettlement, StepCheckpoint, WorkflowRun, RetentionPolicy, StepSettlement, OwnedLeaseReclaim, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait, BufferedSignalRow } from '@dudousxd/nestjs-durable-core';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Custom repositories for the durable entities, so a host app can `@InjectRepository`/inject a
|
|
@@ -31,6 +31,8 @@ declare class BufferedEventRepository extends EntityRepository<BufferedEventEnti
|
|
|
31
31
|
}
|
|
32
32
|
declare class ScheduleRepository extends EntityRepository<ScheduleEntity> {
|
|
33
33
|
}
|
|
34
|
+
declare class SignalIdRepository extends EntityRepository<SignalIdEntity> {
|
|
35
|
+
}
|
|
34
36
|
|
|
35
37
|
/**
|
|
36
38
|
* How property names map to physical column names. The durable tables are adapter-agnostic: a run
|
|
@@ -130,6 +132,14 @@ declare class BufferedEventEntity {
|
|
|
130
132
|
payload?: unknown;
|
|
131
133
|
publishedAt: Date;
|
|
132
134
|
}
|
|
135
|
+
/** A signal dedupe id (`engine.signal(token, payload, { id })`): deduplicates until `expiresAt`. */
|
|
136
|
+
declare class SignalIdEntity {
|
|
137
|
+
[EntityRepositoryType]?: SignalIdRepository;
|
|
138
|
+
token: string;
|
|
139
|
+
signalId: string;
|
|
140
|
+
/** Epoch ms as a `bigint` column — exact, unlike a DATETIME. Drivers may hand it back as a string. */
|
|
141
|
+
expiresAt: number | string | bigint;
|
|
142
|
+
}
|
|
133
143
|
/** A persisted schedule (`engine.schedules`). `spec`/`state` are engine-owned JSON documents. */
|
|
134
144
|
declare class ScheduleEntity {
|
|
135
145
|
[EntityRepositoryType]?: ScheduleRepository;
|
|
@@ -280,6 +290,10 @@ declare class MikroOrmStateStore implements StateStore {
|
|
|
280
290
|
private readonly orm;
|
|
281
291
|
private readonly opts;
|
|
282
292
|
private readonly scopeNamespace;
|
|
293
|
+
readonly settleResultsIfPending?: (results: StepResultSettlement[]) => Promise<(StepCheckpoint | null)[]>;
|
|
294
|
+
readonly insertCheckpointIfAbsent?: (checkpoint: StepCheckpoint) => Promise<boolean>;
|
|
295
|
+
readonly tryLockRunAndGet?: (runId: string, owner: string, leaseUntilMs: number, nowMs: number) => Promise<WorkflowRun | null>;
|
|
296
|
+
readonly claimPendingRuns?: (limit: number, owner: string, leaseUntilMs: number, nowMs: number, namespace?: string) => Promise<WorkflowRun[]>;
|
|
283
297
|
constructor(orm: MikroORM, opts?: MikroOrmStateStoreOptions);
|
|
284
298
|
/**
|
|
285
299
|
* Derive a store confined to a single tenant `scope.namespace`, sharing this store's ORM (no new
|
|
@@ -438,6 +452,13 @@ declare class MikroOrmStateStore implements StateStore {
|
|
|
438
452
|
takeBufferedSignal(token: string): Promise<{
|
|
439
453
|
payload: unknown;
|
|
440
454
|
} | null>;
|
|
455
|
+
takeOldestBufferedSignal(tokens: string[]): Promise<{
|
|
456
|
+
token: string;
|
|
457
|
+
payload: unknown;
|
|
458
|
+
} | null>;
|
|
459
|
+
claimSignalId(token: string, id: string, expiresAt: number, nowMs: number): Promise<boolean>;
|
|
460
|
+
releaseSignalId(token: string, id: string): Promise<void>;
|
|
461
|
+
deleteExpiredSignalIds(nowMs: number, limit: number): Promise<number>;
|
|
441
462
|
listBufferedSignals(tokenPrefix: string, afterId: string | undefined, limit: number): Promise<BufferedSignalRow[]>;
|
|
442
463
|
deleteBufferedSignals(ids: string[]): Promise<void>;
|
|
443
464
|
bufferEvent(input: {
|
|
@@ -454,4 +475,4 @@ declare class MikroOrmStateStore implements StateStore {
|
|
|
454
475
|
removeBufferedEvent(id: string): Promise<boolean>;
|
|
455
476
|
}
|
|
456
477
|
|
|
457
|
-
export { BufferedEventEntity, BufferedEventRepository, BufferedSignalEntity, BufferedSignalRepository, DATETIME_PRECISION, DEFAULT_BLOCKING_ALTER_ROW_LIMIT, type DurableColumnNaming, type DurableSchemaLogger, ENTITIES, type EnsureMikroOrmDurableSchemaOptions, MikroOrmStateStore, type MikroOrmStateStoreOptions, RunAttributeEntity, RunAttributeRepository, ScheduleEntity, ScheduleRepository, SignalWaiterEntity, SignalWaiterRepository, StepCheckpointEntity, StepCheckpointRepository, WorkflowRunEntity, WorkflowRunRepository, durableEntities, durableManagedTables, ensureMikroOrmDurableSchema, mikroOrmDeferredAlterWarning, mikroOrmSchemaRepairWarning };
|
|
478
|
+
export { BufferedEventEntity, BufferedEventRepository, BufferedSignalEntity, BufferedSignalRepository, DATETIME_PRECISION, DEFAULT_BLOCKING_ALTER_ROW_LIMIT, type DurableColumnNaming, type DurableSchemaLogger, ENTITIES, type EnsureMikroOrmDurableSchemaOptions, MikroOrmStateStore, type MikroOrmStateStoreOptions, RunAttributeEntity, RunAttributeRepository, ScheduleEntity, ScheduleRepository, SignalIdEntity, SignalIdRepository, SignalWaiterEntity, SignalWaiterRepository, StepCheckpointEntity, StepCheckpointRepository, WorkflowRunEntity, WorkflowRunRepository, durableEntities, durableManagedTables, ensureMikroOrmDurableSchema, mikroOrmDeferredAlterWarning, mikroOrmSchemaRepairWarning };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as _mikro_orm_core from '@mikro-orm/core';
|
|
2
2
|
import { EntityRepository, EntityRepositoryType, EntitySchema, MikroORM } from '@mikro-orm/core';
|
|
3
|
-
import { StepKind, RunStatus, StateStore, WorkflowRun, RetentionPolicy,
|
|
3
|
+
import { StepKind, RunStatus, StateStore, StepResultSettlement, StepCheckpoint, WorkflowRun, RetentionPolicy, StepSettlement, OwnedLeaseReclaim, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait, BufferedSignalRow } from '@dudousxd/nestjs-durable-core';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Custom repositories for the durable entities, so a host app can `@InjectRepository`/inject a
|
|
@@ -31,6 +31,8 @@ declare class BufferedEventRepository extends EntityRepository<BufferedEventEnti
|
|
|
31
31
|
}
|
|
32
32
|
declare class ScheduleRepository extends EntityRepository<ScheduleEntity> {
|
|
33
33
|
}
|
|
34
|
+
declare class SignalIdRepository extends EntityRepository<SignalIdEntity> {
|
|
35
|
+
}
|
|
34
36
|
|
|
35
37
|
/**
|
|
36
38
|
* How property names map to physical column names. The durable tables are adapter-agnostic: a run
|
|
@@ -130,6 +132,14 @@ declare class BufferedEventEntity {
|
|
|
130
132
|
payload?: unknown;
|
|
131
133
|
publishedAt: Date;
|
|
132
134
|
}
|
|
135
|
+
/** A signal dedupe id (`engine.signal(token, payload, { id })`): deduplicates until `expiresAt`. */
|
|
136
|
+
declare class SignalIdEntity {
|
|
137
|
+
[EntityRepositoryType]?: SignalIdRepository;
|
|
138
|
+
token: string;
|
|
139
|
+
signalId: string;
|
|
140
|
+
/** Epoch ms as a `bigint` column — exact, unlike a DATETIME. Drivers may hand it back as a string. */
|
|
141
|
+
expiresAt: number | string | bigint;
|
|
142
|
+
}
|
|
133
143
|
/** A persisted schedule (`engine.schedules`). `spec`/`state` are engine-owned JSON documents. */
|
|
134
144
|
declare class ScheduleEntity {
|
|
135
145
|
[EntityRepositoryType]?: ScheduleRepository;
|
|
@@ -280,6 +290,10 @@ declare class MikroOrmStateStore implements StateStore {
|
|
|
280
290
|
private readonly orm;
|
|
281
291
|
private readonly opts;
|
|
282
292
|
private readonly scopeNamespace;
|
|
293
|
+
readonly settleResultsIfPending?: (results: StepResultSettlement[]) => Promise<(StepCheckpoint | null)[]>;
|
|
294
|
+
readonly insertCheckpointIfAbsent?: (checkpoint: StepCheckpoint) => Promise<boolean>;
|
|
295
|
+
readonly tryLockRunAndGet?: (runId: string, owner: string, leaseUntilMs: number, nowMs: number) => Promise<WorkflowRun | null>;
|
|
296
|
+
readonly claimPendingRuns?: (limit: number, owner: string, leaseUntilMs: number, nowMs: number, namespace?: string) => Promise<WorkflowRun[]>;
|
|
283
297
|
constructor(orm: MikroORM, opts?: MikroOrmStateStoreOptions);
|
|
284
298
|
/**
|
|
285
299
|
* Derive a store confined to a single tenant `scope.namespace`, sharing this store's ORM (no new
|
|
@@ -438,6 +452,13 @@ declare class MikroOrmStateStore implements StateStore {
|
|
|
438
452
|
takeBufferedSignal(token: string): Promise<{
|
|
439
453
|
payload: unknown;
|
|
440
454
|
} | null>;
|
|
455
|
+
takeOldestBufferedSignal(tokens: string[]): Promise<{
|
|
456
|
+
token: string;
|
|
457
|
+
payload: unknown;
|
|
458
|
+
} | null>;
|
|
459
|
+
claimSignalId(token: string, id: string, expiresAt: number, nowMs: number): Promise<boolean>;
|
|
460
|
+
releaseSignalId(token: string, id: string): Promise<void>;
|
|
461
|
+
deleteExpiredSignalIds(nowMs: number, limit: number): Promise<number>;
|
|
441
462
|
listBufferedSignals(tokenPrefix: string, afterId: string | undefined, limit: number): Promise<BufferedSignalRow[]>;
|
|
442
463
|
deleteBufferedSignals(ids: string[]): Promise<void>;
|
|
443
464
|
bufferEvent(input: {
|
|
@@ -454,4 +475,4 @@ declare class MikroOrmStateStore implements StateStore {
|
|
|
454
475
|
removeBufferedEvent(id: string): Promise<boolean>;
|
|
455
476
|
}
|
|
456
477
|
|
|
457
|
-
export { BufferedEventEntity, BufferedEventRepository, BufferedSignalEntity, BufferedSignalRepository, DATETIME_PRECISION, DEFAULT_BLOCKING_ALTER_ROW_LIMIT, type DurableColumnNaming, type DurableSchemaLogger, ENTITIES, type EnsureMikroOrmDurableSchemaOptions, MikroOrmStateStore, type MikroOrmStateStoreOptions, RunAttributeEntity, RunAttributeRepository, ScheduleEntity, ScheduleRepository, SignalWaiterEntity, SignalWaiterRepository, StepCheckpointEntity, StepCheckpointRepository, WorkflowRunEntity, WorkflowRunRepository, durableEntities, durableManagedTables, ensureMikroOrmDurableSchema, mikroOrmDeferredAlterWarning, mikroOrmSchemaRepairWarning };
|
|
478
|
+
export { BufferedEventEntity, BufferedEventRepository, BufferedSignalEntity, BufferedSignalRepository, DATETIME_PRECISION, DEFAULT_BLOCKING_ALTER_ROW_LIMIT, type DurableColumnNaming, type DurableSchemaLogger, ENTITIES, type EnsureMikroOrmDurableSchemaOptions, MikroOrmStateStore, type MikroOrmStateStoreOptions, RunAttributeEntity, RunAttributeRepository, ScheduleEntity, ScheduleRepository, SignalIdEntity, SignalIdRepository, SignalWaiterEntity, SignalWaiterRepository, StepCheckpointEntity, StepCheckpointRepository, WorkflowRunEntity, WorkflowRunRepository, durableEntities, durableManagedTables, ensureMikroOrmDurableSchema, mikroOrmDeferredAlterWarning, mikroOrmSchemaRepairWarning };
|
package/dist/index.js
CHANGED
|
@@ -41,6 +41,11 @@ var ScheduleRepository = class extends EntityRepository {
|
|
|
41
41
|
__name(this, "ScheduleRepository");
|
|
42
42
|
}
|
|
43
43
|
};
|
|
44
|
+
var SignalIdRepository = class extends EntityRepository {
|
|
45
|
+
static {
|
|
46
|
+
__name(this, "SignalIdRepository");
|
|
47
|
+
}
|
|
48
|
+
};
|
|
44
49
|
|
|
45
50
|
// src/entities.ts
|
|
46
51
|
function snakeCase(property) {
|
|
@@ -141,6 +146,16 @@ var BufferedEventEntity = class {
|
|
|
141
146
|
payload;
|
|
142
147
|
publishedAt;
|
|
143
148
|
};
|
|
149
|
+
var SignalIdEntity = class {
|
|
150
|
+
static {
|
|
151
|
+
__name(this, "SignalIdEntity");
|
|
152
|
+
}
|
|
153
|
+
[EntityRepositoryType];
|
|
154
|
+
token;
|
|
155
|
+
signalId;
|
|
156
|
+
/** Epoch ms as a `bigint` column — exact, unlike a DATETIME. Drivers may hand it back as a string. */
|
|
157
|
+
expiresAt;
|
|
158
|
+
};
|
|
144
159
|
var ScheduleEntity = class {
|
|
145
160
|
static {
|
|
146
161
|
__name(this, "ScheduleEntity");
|
|
@@ -589,6 +604,36 @@ function durableEntities(options = {}) {
|
|
|
589
604
|
}
|
|
590
605
|
}
|
|
591
606
|
});
|
|
607
|
+
const signalIds = new EntitySchema({
|
|
608
|
+
class: SignalIdEntity,
|
|
609
|
+
tableName: "durable_signal_ids",
|
|
610
|
+
repository: /* @__PURE__ */ __name(() => SignalIdRepository, "repository"),
|
|
611
|
+
// The expired-id sweep (`expires_at <= now`).
|
|
612
|
+
indexes: [
|
|
613
|
+
{
|
|
614
|
+
name: "durable_signal_ids_expires_at_idx",
|
|
615
|
+
properties: [
|
|
616
|
+
"expiresAt"
|
|
617
|
+
]
|
|
618
|
+
}
|
|
619
|
+
],
|
|
620
|
+
properties: {
|
|
621
|
+
token: {
|
|
622
|
+
type: "string",
|
|
623
|
+
primary: true,
|
|
624
|
+
fieldName: col("token")
|
|
625
|
+
},
|
|
626
|
+
signalId: {
|
|
627
|
+
type: "string",
|
|
628
|
+
primary: true,
|
|
629
|
+
fieldName: col("signalId")
|
|
630
|
+
},
|
|
631
|
+
expiresAt: {
|
|
632
|
+
type: "bigint",
|
|
633
|
+
fieldName: col("expiresAt")
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
});
|
|
592
637
|
const schedules = new EntitySchema({
|
|
593
638
|
class: ScheduleEntity,
|
|
594
639
|
tableName: "durable_schedules",
|
|
@@ -660,6 +705,7 @@ function durableEntities(options = {}) {
|
|
|
660
705
|
signalWaiters,
|
|
661
706
|
bufferedSignals,
|
|
662
707
|
bufferedEvents,
|
|
708
|
+
signalIds,
|
|
663
709
|
schedules
|
|
664
710
|
];
|
|
665
711
|
}
|
|
@@ -667,8 +713,8 @@ __name(durableEntities, "durableEntities");
|
|
|
667
713
|
var ENTITIES = durableEntities();
|
|
668
714
|
|
|
669
715
|
// src/mikro-orm-state-store.ts
|
|
670
|
-
import { ENGINE_MINTED_TAG_PREFIXES, LIFECYCLE_REGISTRY_WORKFLOW, RUN_VALUE_FACET_LIMIT, SINGLETON_ACTIVE_STATUSES, SINGLETON_LOCK_CHUNK, TERMINAL_RUN_STATUSES, attributePredicateOperands, axisIsRunColumn, clearTerminalSingletonAdmission, facetOrigin, mergeRunFacetRows, normalizeAttributeRows, parseDuration, retentionKeeps, singletonAdmissionDenialPatch, singletonAdmissionTags, singletonClaimantIds } from "@dudousxd/nestjs-durable-core";
|
|
671
|
-
import { LockMode, raw } from "@mikro-orm/core";
|
|
716
|
+
import { ENGINE_MINTED_TAG_PREFIXES, LIFECYCLE_REGISTRY_WORKFLOW, RUN_VALUE_FACET_LIMIT, SINGLETON_ACTIVE_STATUSES, SINGLETON_LOCK_CHUNK, TERMINAL_RUN_STATUSES, attributePredicateOperands, axisIsRunColumn, clearTerminalSingletonAdmission, facetOrigin, mergeRunFacetRows, normalizeAttributeRows, parseDuration, retentionKeeps, settleResultsWithPlainCalls, singletonAdmissionDenialPatch, singletonAdmissionTags, singletonClaimantIds } from "@dudousxd/nestjs-durable-core";
|
|
717
|
+
import { LockMode, UniqueConstraintViolationException, raw } from "@mikro-orm/core";
|
|
672
718
|
|
|
673
719
|
// src/schema.ts
|
|
674
720
|
import { createHash } from "node:crypto";
|
|
@@ -679,6 +725,7 @@ var DURABLE_TABLE_NAMES = /* @__PURE__ */ new Set([
|
|
|
679
725
|
"durable_signal_waiters",
|
|
680
726
|
"durable_buffered_signals",
|
|
681
727
|
"durable_buffered_events",
|
|
728
|
+
"durable_signal_ids",
|
|
682
729
|
"durable_schedules"
|
|
683
730
|
]);
|
|
684
731
|
function durableManagedTables() {
|
|
@@ -1059,10 +1106,31 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
|
|
|
1059
1106
|
orm;
|
|
1060
1107
|
opts;
|
|
1061
1108
|
scopeNamespace;
|
|
1109
|
+
// The round-trip-saving optional methods, bound on Postgres and MySQL (see
|
|
1110
|
+
// `MikroOrmRoundTrips`); absent on SQLite, where the engine's read-then-write calls are used.
|
|
1111
|
+
settleResultsIfPending;
|
|
1112
|
+
insertCheckpointIfAbsent;
|
|
1113
|
+
tryLockRunAndGet;
|
|
1114
|
+
claimPendingRuns;
|
|
1062
1115
|
constructor(orm, opts = {}) {
|
|
1063
1116
|
this.orm = orm;
|
|
1064
1117
|
this.opts = opts;
|
|
1065
1118
|
this.scopeNamespace = opts.scope?.namespace;
|
|
1119
|
+
let platform = "";
|
|
1120
|
+
try {
|
|
1121
|
+
platform = String(orm.em.getPlatform().constructor.name).toLowerCase();
|
|
1122
|
+
} catch {
|
|
1123
|
+
return;
|
|
1124
|
+
}
|
|
1125
|
+
const dialect = platform.includes("postgre") ? "postgres" : platform.includes("mysql") ? "mysql" : void 0;
|
|
1126
|
+
if (!dialect) return;
|
|
1127
|
+
const fast = new MikroOrmRoundTrips(() => this.orm.em.fork(), dialect, this.scopeNamespace, this);
|
|
1128
|
+
this.insertCheckpointIfAbsent = (cp) => fast.insertCheckpointIfAbsent(cp);
|
|
1129
|
+
this.settleResultsIfPending = (results) => fast.settleResultsIfPending(results);
|
|
1130
|
+
this.claimPendingRuns = (limit, owner, until, now, ns) => fast.claimPendingRuns(limit, owner, until, now, ns);
|
|
1131
|
+
if (dialect === "postgres") {
|
|
1132
|
+
this.tryLockRunAndGet = (runId, owner, until, now) => fast.tryLockRunAndGet(runId, owner, until, now);
|
|
1133
|
+
}
|
|
1066
1134
|
}
|
|
1067
1135
|
/**
|
|
1068
1136
|
* Derive a store confined to a single tenant `scope.namespace`, sharing this store's ORM (no new
|
|
@@ -1413,6 +1481,24 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
|
|
|
1413
1481
|
}
|
|
1414
1482
|
async settleCheckpointsIfPending(settlements) {
|
|
1415
1483
|
if (settlements.length === 0) return [];
|
|
1484
|
+
if (settlements.length === 1) {
|
|
1485
|
+
const s = settlements[0];
|
|
1486
|
+
const changed = await this.fork().nativeUpdate(StepCheckpointEntity, {
|
|
1487
|
+
runId: s.runId,
|
|
1488
|
+
seq: s.seq,
|
|
1489
|
+
status: "pending"
|
|
1490
|
+
}, {
|
|
1491
|
+
status: s.status,
|
|
1492
|
+
output: s.output ?? null,
|
|
1493
|
+
error: s.error ?? null,
|
|
1494
|
+
events: s.events ?? null,
|
|
1495
|
+
startedAt: s.startedAt,
|
|
1496
|
+
finishedAt: s.finishedAt
|
|
1497
|
+
});
|
|
1498
|
+
return [
|
|
1499
|
+
changed === 1
|
|
1500
|
+
];
|
|
1501
|
+
}
|
|
1416
1502
|
return this.fork().transactional(async (em) => {
|
|
1417
1503
|
const pending = await lockPendingCheckpoints(em, settlements, [
|
|
1418
1504
|
"runId",
|
|
@@ -2301,6 +2387,92 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
|
|
|
2301
2387
|
payload
|
|
2302
2388
|
};
|
|
2303
2389
|
}
|
|
2390
|
+
async takeOldestBufferedSignal(tokens) {
|
|
2391
|
+
if (tokens.length === 0) return null;
|
|
2392
|
+
const unique = [
|
|
2393
|
+
...new Set(tokens)
|
|
2394
|
+
];
|
|
2395
|
+
for (; ; ) {
|
|
2396
|
+
const em = this.fork();
|
|
2397
|
+
const entity = await em.findOne(BufferedSignalEntity, {
|
|
2398
|
+
token: {
|
|
2399
|
+
$in: unique
|
|
2400
|
+
}
|
|
2401
|
+
}, {
|
|
2402
|
+
orderBy: {
|
|
2403
|
+
id: "asc"
|
|
2404
|
+
}
|
|
2405
|
+
});
|
|
2406
|
+
if (!entity) return null;
|
|
2407
|
+
if (await em.nativeDelete(BufferedSignalEntity, {
|
|
2408
|
+
id: entity.id
|
|
2409
|
+
}) === 1) {
|
|
2410
|
+
return {
|
|
2411
|
+
token: entity.token,
|
|
2412
|
+
payload: entity.payload ?? void 0
|
|
2413
|
+
};
|
|
2414
|
+
}
|
|
2415
|
+
}
|
|
2416
|
+
}
|
|
2417
|
+
async claimSignalId(token, id, expiresAt, nowMs) {
|
|
2418
|
+
for (; ; ) {
|
|
2419
|
+
const em = this.fork();
|
|
2420
|
+
try {
|
|
2421
|
+
await em.insert(SignalIdEntity, {
|
|
2422
|
+
token,
|
|
2423
|
+
signalId: id,
|
|
2424
|
+
expiresAt
|
|
2425
|
+
});
|
|
2426
|
+
return true;
|
|
2427
|
+
} catch (err) {
|
|
2428
|
+
if (!(err instanceof UniqueConstraintViolationException)) throw err;
|
|
2429
|
+
}
|
|
2430
|
+
const taken = await em.nativeUpdate(SignalIdEntity, {
|
|
2431
|
+
token,
|
|
2432
|
+
signalId: id,
|
|
2433
|
+
expiresAt: {
|
|
2434
|
+
$lte: nowMs
|
|
2435
|
+
}
|
|
2436
|
+
}, {
|
|
2437
|
+
expiresAt
|
|
2438
|
+
});
|
|
2439
|
+
if (taken === 1) return true;
|
|
2440
|
+
if (await em.count(SignalIdEntity, {
|
|
2441
|
+
token,
|
|
2442
|
+
signalId: id
|
|
2443
|
+
}) > 0) return false;
|
|
2444
|
+
}
|
|
2445
|
+
}
|
|
2446
|
+
async releaseSignalId(token, id) {
|
|
2447
|
+
await this.fork().nativeDelete(SignalIdEntity, {
|
|
2448
|
+
token,
|
|
2449
|
+
signalId: id
|
|
2450
|
+
});
|
|
2451
|
+
}
|
|
2452
|
+
async deleteExpiredSignalIds(nowMs, limit) {
|
|
2453
|
+
const em = this.fork();
|
|
2454
|
+
const rows = await em.find(SignalIdEntity, {
|
|
2455
|
+
expiresAt: {
|
|
2456
|
+
$lte: nowMs
|
|
2457
|
+
}
|
|
2458
|
+
}, {
|
|
2459
|
+
fields: [
|
|
2460
|
+
"token",
|
|
2461
|
+
"signalId"
|
|
2462
|
+
],
|
|
2463
|
+
limit
|
|
2464
|
+
});
|
|
2465
|
+
if (rows.length === 0) return 0;
|
|
2466
|
+
return em.nativeDelete(SignalIdEntity, {
|
|
2467
|
+
$or: rows.map((r) => ({
|
|
2468
|
+
token: r.token,
|
|
2469
|
+
signalId: r.signalId
|
|
2470
|
+
})),
|
|
2471
|
+
expiresAt: {
|
|
2472
|
+
$lte: nowMs
|
|
2473
|
+
}
|
|
2474
|
+
});
|
|
2475
|
+
}
|
|
2304
2476
|
async listBufferedSignals(tokenPrefix, afterId, limit) {
|
|
2305
2477
|
const em = this.fork();
|
|
2306
2478
|
const rows = await em.find(BufferedSignalEntity, {
|
|
@@ -2613,6 +2785,240 @@ function fromScheduleEntity(e) {
|
|
|
2613
2785
|
};
|
|
2614
2786
|
}
|
|
2615
2787
|
__name(fromScheduleEntity, "fromScheduleEntity");
|
|
2788
|
+
var MikroOrmRoundTrips = class MikroOrmRoundTrips2 {
|
|
2789
|
+
static {
|
|
2790
|
+
__name(this, "MikroOrmRoundTrips");
|
|
2791
|
+
}
|
|
2792
|
+
fork;
|
|
2793
|
+
scope;
|
|
2794
|
+
store;
|
|
2795
|
+
pg;
|
|
2796
|
+
cpTypes;
|
|
2797
|
+
constructor(fork, dialect, scope, store) {
|
|
2798
|
+
this.fork = fork;
|
|
2799
|
+
this.scope = scope;
|
|
2800
|
+
this.store = store;
|
|
2801
|
+
this.pg = dialect === "postgres";
|
|
2802
|
+
}
|
|
2803
|
+
q(em, name) {
|
|
2804
|
+
return em.getPlatform().quoteIdentifier(name);
|
|
2805
|
+
}
|
|
2806
|
+
meta(em, entity) {
|
|
2807
|
+
return em.getMetadata().get(entity);
|
|
2808
|
+
}
|
|
2809
|
+
field(em, entity, property) {
|
|
2810
|
+
const prop = this.meta(em, entity).properties[property];
|
|
2811
|
+
const name = prop?.fieldNames[0];
|
|
2812
|
+
if (!name) throw new Error(`no column for ${property}`);
|
|
2813
|
+
return name;
|
|
2814
|
+
}
|
|
2815
|
+
c(em, entity, property) {
|
|
2816
|
+
return this.q(em, this.field(em, entity, property));
|
|
2817
|
+
}
|
|
2818
|
+
table(em, entity) {
|
|
2819
|
+
return this.q(em, this.meta(em, entity).tableName);
|
|
2820
|
+
}
|
|
2821
|
+
/** `value` as MikroORM binds it for `property` (its custom type's conversion, if any). */
|
|
2822
|
+
bind(em, entity, property, value) {
|
|
2823
|
+
if (value == null) return null;
|
|
2824
|
+
const type = this.meta(em, entity).properties[property]?.customType;
|
|
2825
|
+
return type ? type.convertToDatabaseValue(value, em.getPlatform(), {
|
|
2826
|
+
mode: "query"
|
|
2827
|
+
}) : value;
|
|
2828
|
+
}
|
|
2829
|
+
async run(em, sql, params, method = "all") {
|
|
2830
|
+
const connection = em.getConnection();
|
|
2831
|
+
return connection.execute(sql, params, method, em.getTransactionContext());
|
|
2832
|
+
}
|
|
2833
|
+
rows(out) {
|
|
2834
|
+
if (Array.isArray(out)) return out;
|
|
2835
|
+
const rows = out?.rows;
|
|
2836
|
+
return Array.isArray(rows) ? rows : [];
|
|
2837
|
+
}
|
|
2838
|
+
affected(out) {
|
|
2839
|
+
return Number(out?.affectedRows ?? 0);
|
|
2840
|
+
}
|
|
2841
|
+
runFromRow(em, raw2) {
|
|
2842
|
+
return fromRunEntity(em.map(WorkflowRunEntity, raw2));
|
|
2843
|
+
}
|
|
2844
|
+
checkpointFromRow(em, raw2) {
|
|
2845
|
+
return fromCheckpointEntity(em.map(StepCheckpointEntity, raw2));
|
|
2846
|
+
}
|
|
2847
|
+
/** The checkpoint columns but `input`, quoted, each prefixed with `prefix`. */
|
|
2848
|
+
settledColumns(em, prefix) {
|
|
2849
|
+
return Object.keys(this.meta(em, StepCheckpointEntity).properties).filter((p) => p !== "input").map((p) => `${prefix}${this.c(em, StepCheckpointEntity, p)}`).join(", ");
|
|
2850
|
+
}
|
|
2851
|
+
async tryLockRunAndGet(runId, owner, leaseUntilMs, nowMs) {
|
|
2852
|
+
const em = this.fork();
|
|
2853
|
+
const R = WorkflowRunEntity;
|
|
2854
|
+
const until = this.c(em, R, "lockedUntil");
|
|
2855
|
+
const scope = this.scope !== void 0 ? ` AND ${this.c(em, R, "namespace")} = ?` : "";
|
|
2856
|
+
const out = await this.run(em, `UPDATE ${this.table(em, R)} SET ${this.c(em, R, "lockedBy")} = ?, ${until} = ? WHERE ${this.c(em, R, "id")} = ? AND (${until} IS NULL OR ${until} <= ?)${scope} RETURNING *`, [
|
|
2857
|
+
owner,
|
|
2858
|
+
this.bind(em, R, "lockedUntil", new Date(leaseUntilMs)),
|
|
2859
|
+
runId,
|
|
2860
|
+
this.bind(em, R, "lockedUntil", new Date(nowMs)),
|
|
2861
|
+
...this.scope !== void 0 ? [
|
|
2862
|
+
this.scope
|
|
2863
|
+
] : []
|
|
2864
|
+
]);
|
|
2865
|
+
const [row] = this.rows(out);
|
|
2866
|
+
return row ? this.runFromRow(em, row) : null;
|
|
2867
|
+
}
|
|
2868
|
+
async insertCheckpointIfAbsent(checkpoint) {
|
|
2869
|
+
try {
|
|
2870
|
+
await this.fork().insert(StepCheckpointEntity, toCheckpointEntity(checkpoint));
|
|
2871
|
+
return true;
|
|
2872
|
+
} catch (err) {
|
|
2873
|
+
if (err instanceof UniqueConstraintViolationException) return false;
|
|
2874
|
+
throw err;
|
|
2875
|
+
}
|
|
2876
|
+
}
|
|
2877
|
+
async cpType(em, property) {
|
|
2878
|
+
const field = this.field(em, StepCheckpointEntity, property);
|
|
2879
|
+
this.cpTypes ??= this.run(em, "SELECT column_name AS name, data_type AS type FROM information_schema.columns WHERE table_name = ? AND table_schema = current_schema()", [
|
|
2880
|
+
this.meta(em, StepCheckpointEntity).tableName
|
|
2881
|
+
]).then((out) => new Map(this.rows(out).map((r) => [
|
|
2882
|
+
String(r.name),
|
|
2883
|
+
String(r.type)
|
|
2884
|
+
])));
|
|
2885
|
+
return (await this.cpTypes).get(field) ?? "text";
|
|
2886
|
+
}
|
|
2887
|
+
async settleResultsIfPending(results) {
|
|
2888
|
+
const out = results.map(() => null);
|
|
2889
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2890
|
+
const sent = results.flatMap((r, i) => {
|
|
2891
|
+
const key = checkpointKey(r.runId, r.seq);
|
|
2892
|
+
if (seen.has(key)) return [];
|
|
2893
|
+
seen.add(key);
|
|
2894
|
+
return [
|
|
2895
|
+
i
|
|
2896
|
+
];
|
|
2897
|
+
});
|
|
2898
|
+
if (sent.length === 0) return out;
|
|
2899
|
+
const em = this.fork();
|
|
2900
|
+
const K = StepCheckpointEntity;
|
|
2901
|
+
const R = WorkflowRunEntity;
|
|
2902
|
+
const c = /* @__PURE__ */ __name((prop) => this.c(em, K, prop), "c");
|
|
2903
|
+
const finished = `NOT EXISTS (SELECT 1 FROM ${this.table(em, R)} r WHERE r.${this.c(em, R, "id")} = c.${c("runId")} AND r.${this.c(em, R, "status")} IN ('cancelled', 'completed'))`;
|
|
2904
|
+
if (!this.pg && sent.length > 1) {
|
|
2905
|
+
return settleResultsWithPlainCalls(this.store, results);
|
|
2906
|
+
}
|
|
2907
|
+
if (!this.pg) {
|
|
2908
|
+
for (const i of sent) {
|
|
2909
|
+
const s = results[i];
|
|
2910
|
+
const done = s.status === "completed";
|
|
2911
|
+
const wrote = this.affected(await this.run(em, `UPDATE ${this.table(em, K)} c SET c.${c("status")} = ?, c.${c("output")} = ${done ? "?" : `c.${c("output")}`}, c.${c("error")} = ?, c.${c("events")} = COALESCE(?, c.${c("events")}), c.${c("startedAt")} = COALESCE(?, c.${c("startedAt")}), c.${c("finishedAt")} = ? WHERE c.${c("runId")} = ? AND c.${c("seq")} = ? AND c.${c("status")} = 'pending' AND ${finished}`, [
|
|
2912
|
+
s.status,
|
|
2913
|
+
...done ? [
|
|
2914
|
+
this.bind(em, K, "output", s.output)
|
|
2915
|
+
] : [],
|
|
2916
|
+
this.bind(em, K, "error", s.error),
|
|
2917
|
+
this.bind(em, K, "events", s.events),
|
|
2918
|
+
this.bind(em, K, "startedAt", s.startedAt),
|
|
2919
|
+
this.bind(em, K, "finishedAt", s.finishedAt),
|
|
2920
|
+
s.runId,
|
|
2921
|
+
s.seq
|
|
2922
|
+
], "run"));
|
|
2923
|
+
if (!wrote) continue;
|
|
2924
|
+
const [row] = this.rows(await this.run(em, `SELECT ${this.settledColumns(em, "")} FROM ${this.table(em, K)} WHERE ${c("runId")} = ? AND ${c("seq")} = ?`, [
|
|
2925
|
+
s.runId,
|
|
2926
|
+
s.seq
|
|
2927
|
+
]));
|
|
2928
|
+
out[i] = row ? this.checkpointFromRow(em, row) : null;
|
|
2929
|
+
}
|
|
2930
|
+
return out;
|
|
2931
|
+
}
|
|
2932
|
+
const props = [
|
|
2933
|
+
"runId",
|
|
2934
|
+
"seq",
|
|
2935
|
+
"status",
|
|
2936
|
+
"output",
|
|
2937
|
+
"error",
|
|
2938
|
+
"events",
|
|
2939
|
+
"startedAt",
|
|
2940
|
+
"finishedAt"
|
|
2941
|
+
];
|
|
2942
|
+
const types = new Map(await Promise.all(props.map(async (p) => [
|
|
2943
|
+
p,
|
|
2944
|
+
await this.cpType(em, p)
|
|
2945
|
+
])));
|
|
2946
|
+
for (let at = 0; at < sent.length; at += 500) {
|
|
2947
|
+
const chunk = sent.slice(at, at + 500);
|
|
2948
|
+
const params = [];
|
|
2949
|
+
const rows = chunk.map((i) => {
|
|
2950
|
+
const s = results[i];
|
|
2951
|
+
params.push(s.runId, s.seq, s.status, this.bind(em, K, "output", s.status === "completed" ? s.output : null), this.bind(em, K, "error", s.error), this.bind(em, K, "events", s.events), this.bind(em, K, "startedAt", s.startedAt), this.bind(em, K, "finishedAt", s.finishedAt));
|
|
2952
|
+
return `(${props.map((p) => `?::${types.get(p)}`).join(", ")})`;
|
|
2953
|
+
});
|
|
2954
|
+
const settled = this.rows(await this.run(em, `UPDATE ${this.table(em, K)} AS c SET ${c("status")} = v.status, ${c("output")} = CASE WHEN v.status = 'completed' THEN v.output ELSE c.${c("output")} END, ${c("error")} = v.error, ${c("events")} = COALESCE(v.events, c.${c("events")}), ${c("startedAt")} = COALESCE(v.started_at, c.${c("startedAt")}), ${c("finishedAt")} = v.finished_at FROM (VALUES ${rows.join(", ")}) AS v(run_id, seq, status, output, error, events, started_at, finished_at) WHERE c.${c("runId")} = v.run_id AND c.${c("seq")} = v.seq AND c.${c("status")} = 'pending' AND ${finished} RETURNING ${this.settledColumns(em, "c.")}`, params));
|
|
2955
|
+
const byKey = new Map(chunk.map((i) => {
|
|
2956
|
+
const s = results[i];
|
|
2957
|
+
return [
|
|
2958
|
+
checkpointKey(s.runId, s.seq),
|
|
2959
|
+
i
|
|
2960
|
+
];
|
|
2961
|
+
}));
|
|
2962
|
+
for (const raw2 of settled) {
|
|
2963
|
+
const cp = this.checkpointFromRow(em, raw2);
|
|
2964
|
+
const i = byKey.get(checkpointKey(cp.runId, cp.seq));
|
|
2965
|
+
if (i !== void 0) out[i] = cp;
|
|
2966
|
+
}
|
|
2967
|
+
}
|
|
2968
|
+
return out;
|
|
2969
|
+
}
|
|
2970
|
+
async claimPendingRuns(limit, owner, leaseUntilMs, nowMs, namespace) {
|
|
2971
|
+
if (limit <= 0) return [];
|
|
2972
|
+
const em = this.fork();
|
|
2973
|
+
const R = WorkflowRunEntity;
|
|
2974
|
+
const c = /* @__PURE__ */ __name((prop) => this.c(em, R, prop), "c");
|
|
2975
|
+
const until = this.bind(em, R, "lockedUntil", new Date(leaseUntilMs));
|
|
2976
|
+
const now = this.bind(em, R, "lockedUntil", new Date(nowMs));
|
|
2977
|
+
const filters = [
|
|
2978
|
+
...namespace !== void 0 ? [
|
|
2979
|
+
namespace
|
|
2980
|
+
] : [],
|
|
2981
|
+
...this.scope !== void 0 ? [
|
|
2982
|
+
this.scope
|
|
2983
|
+
] : []
|
|
2984
|
+
];
|
|
2985
|
+
const where = `${c("status")} = 'pending' AND (${c("lockedUntil")} IS NULL OR ${c("lockedUntil")} <= ?)${filters.map(() => ` AND ${c("namespace")} = ?`).join("")}`;
|
|
2986
|
+
const sort = /* @__PURE__ */ __name((runs) => runs.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime() || a.id.localeCompare(b.id)), "sort");
|
|
2987
|
+
if (this.pg) {
|
|
2988
|
+
const out = await this.run(
|
|
2989
|
+
em,
|
|
2990
|
+
`WITH claimed AS MATERIALIZED (SELECT ${c("id")} AS claimed_id FROM ${this.table(em, R)} WHERE ${where} ORDER BY ${c("createdAt")}, ${c("id")} LIMIT ? FOR UPDATE SKIP LOCKED) UPDATE ${this.table(em, R)} AS r SET ${c("lockedBy")} = ?, ${c("lockedUntil")} = ? FROM claimed WHERE r.${c("id")} = claimed.claimed_id RETURNING r.*`,
|
|
2991
|
+
// `?` placeholders bind in order: the CTE's (now, namespace filters, limit) come first.
|
|
2992
|
+
[
|
|
2993
|
+
now,
|
|
2994
|
+
...filters,
|
|
2995
|
+
limit,
|
|
2996
|
+
owner,
|
|
2997
|
+
until
|
|
2998
|
+
]
|
|
2999
|
+
);
|
|
3000
|
+
return sort(this.rows(out).map((raw2) => this.runFromRow(em, raw2)));
|
|
3001
|
+
}
|
|
3002
|
+
return em.transactional(async (tx) => {
|
|
3003
|
+
const picked = this.rows(await this.run(tx, `SELECT * FROM ${this.table(tx, R)} WHERE ${where} ORDER BY ${c("createdAt")}, ${c("id")} LIMIT ? FOR UPDATE SKIP LOCKED`, [
|
|
3004
|
+
now,
|
|
3005
|
+
...filters,
|
|
3006
|
+
limit
|
|
3007
|
+
])).map((raw2) => this.runFromRow(tx, raw2));
|
|
3008
|
+
if (picked.length === 0) return [];
|
|
3009
|
+
await this.run(tx, `UPDATE ${this.table(tx, R)} SET ${c("lockedBy")} = ?, ${c("lockedUntil")} = ? WHERE ${c("id")} IN (${picked.map(() => "?").join(", ")})`, [
|
|
3010
|
+
owner,
|
|
3011
|
+
until,
|
|
3012
|
+
...picked.map((run) => run.id)
|
|
3013
|
+
], "run");
|
|
3014
|
+
return sort(picked.map((run) => ({
|
|
3015
|
+
...run,
|
|
3016
|
+
lockedBy: owner,
|
|
3017
|
+
lockedUntil: leaseUntilMs
|
|
3018
|
+
})));
|
|
3019
|
+
});
|
|
3020
|
+
}
|
|
3021
|
+
};
|
|
2616
3022
|
export {
|
|
2617
3023
|
BufferedEventEntity,
|
|
2618
3024
|
BufferedEventRepository,
|
|
@@ -2626,6 +3032,8 @@ export {
|
|
|
2626
3032
|
RunAttributeRepository,
|
|
2627
3033
|
ScheduleEntity,
|
|
2628
3034
|
ScheduleRepository,
|
|
3035
|
+
SignalIdEntity,
|
|
3036
|
+
SignalIdRepository,
|
|
2629
3037
|
SignalWaiterEntity,
|
|
2630
3038
|
SignalWaiterRepository,
|
|
2631
3039
|
StepCheckpointEntity,
|