@ddd-ts-kit/kernel 0.10.0 → 0.12.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.
Files changed (226) hide show
  1. package/README.md +129 -4
  2. package/dist/application/background/background-job-execution.d.ts +27 -0
  3. package/dist/application/background/background-job-execution.d.ts.map +1 -0
  4. package/dist/application/background/background-job-execution.js +2 -0
  5. package/dist/application/background/background-job-execution.js.map +1 -0
  6. package/dist/application/background/background-job-options.d.ts +16 -0
  7. package/dist/application/background/background-job-options.d.ts.map +1 -0
  8. package/dist/application/background/background-job-options.js +2 -0
  9. package/dist/application/background/background-job-options.js.map +1 -0
  10. package/dist/application/background/background-job-policy.d.ts +12 -0
  11. package/dist/application/background/background-job-policy.d.ts.map +1 -0
  12. package/dist/application/background/background-job-policy.js +24 -0
  13. package/dist/application/background/background-job-policy.js.map +1 -0
  14. package/dist/application/background/background-job-type.d.ts +10 -0
  15. package/dist/application/background/background-job-type.d.ts.map +1 -0
  16. package/dist/application/background/background-job-type.js +2 -0
  17. package/dist/application/background/background-job-type.js.map +1 -0
  18. package/dist/application/background/background-job.d.ts +14 -0
  19. package/dist/application/background/background-job.d.ts.map +1 -0
  20. package/dist/application/background/background-job.js +2 -0
  21. package/dist/application/background/background-job.js.map +1 -0
  22. package/dist/application/background/enqueue-options.d.ts +20 -0
  23. package/dist/application/background/enqueue-options.d.ts.map +1 -0
  24. package/dist/application/background/enqueue-options.js +2 -0
  25. package/dist/application/background/enqueue-options.js.map +1 -0
  26. package/dist/application/background/enqueued-by.d.ts +13 -0
  27. package/dist/application/background/enqueued-by.d.ts.map +1 -0
  28. package/dist/application/background/enqueued-by.js +2 -0
  29. package/dist/application/background/enqueued-by.js.map +1 -0
  30. package/dist/application/background/enqueued-job.d.ts +7 -0
  31. package/dist/application/background/enqueued-job.d.ts.map +1 -0
  32. package/dist/application/background/enqueued-job.js +2 -0
  33. package/dist/application/background/enqueued-job.js.map +1 -0
  34. package/dist/application/background/execution-context-of-background-job.d.ts +14 -0
  35. package/dist/application/background/execution-context-of-background-job.d.ts.map +1 -0
  36. package/dist/application/background/execution-context-of-background-job.js +18 -0
  37. package/dist/application/background/execution-context-of-background-job.js.map +1 -0
  38. package/dist/application/background/index.d.ts +15 -0
  39. package/dist/application/background/index.d.ts.map +1 -0
  40. package/dist/application/background/index.js +15 -0
  41. package/dist/application/background/index.js.map +1 -0
  42. package/dist/application/background/job-backoff.d.ts +10 -0
  43. package/dist/application/background/job-backoff.d.ts.map +1 -0
  44. package/dist/application/background/job-backoff.js +2 -0
  45. package/dist/application/background/job-backoff.js.map +1 -0
  46. package/dist/application/background/job-progress.d.ts +7 -0
  47. package/dist/application/background/job-progress.d.ts.map +1 -0
  48. package/dist/application/background/job-progress.js +2 -0
  49. package/dist/application/background/job-progress.js.map +1 -0
  50. package/dist/application/background/queued-job-status.d.ts +18 -0
  51. package/dist/application/background/queued-job-status.d.ts.map +1 -0
  52. package/dist/application/background/queued-job-status.js +17 -0
  53. package/dist/application/background/queued-job-status.js.map +1 -0
  54. package/dist/application/background/queued-job.d.ts +25 -0
  55. package/dist/application/background/queued-job.d.ts.map +1 -0
  56. package/dist/application/background/queued-job.js +2 -0
  57. package/dist/application/background/queued-job.js.map +1 -0
  58. package/dist/application/background/retry-delay-ms.d.ts +4 -0
  59. package/dist/application/background/retry-delay-ms.d.ts.map +1 -0
  60. package/dist/application/background/retry-delay-ms.js +6 -0
  61. package/dist/application/background/retry-delay-ms.js.map +1 -0
  62. package/dist/application/cache/cache-purge-payload.d.ts +5 -0
  63. package/dist/application/cache/cache-purge-payload.d.ts.map +1 -0
  64. package/dist/application/cache/cache-purge-payload.js +2 -0
  65. package/dist/application/cache/cache-purge-payload.js.map +1 -0
  66. package/dist/application/cache/cache-purge-request.d.ts +19 -0
  67. package/dist/application/cache/cache-purge-request.d.ts.map +1 -0
  68. package/dist/application/cache/cache-purge-request.js +24 -0
  69. package/dist/application/cache/cache-purge-request.js.map +1 -0
  70. package/dist/application/cache/cache-tag.d.ts +8 -0
  71. package/dist/application/cache/cache-tag.d.ts.map +1 -0
  72. package/dist/application/cache/cache-tag.js +10 -0
  73. package/dist/application/cache/cache-tag.js.map +1 -0
  74. package/dist/application/cache/tenant-cache-tags.d.ts +7 -0
  75. package/dist/application/cache/tenant-cache-tags.d.ts.map +1 -0
  76. package/dist/application/cache/tenant-cache-tags.js +13 -0
  77. package/dist/application/cache/tenant-cache-tags.js.map +1 -0
  78. package/dist/application/execution/execution-origin.d.ts +1 -1
  79. package/dist/application/execution/execution-origin.d.ts.map +1 -1
  80. package/dist/application/persistence/expect-version.d.ts +13 -0
  81. package/dist/application/persistence/expect-version.d.ts.map +1 -0
  82. package/dist/application/persistence/expect-version.js +16 -0
  83. package/dist/application/persistence/expect-version.js.map +1 -0
  84. package/dist/application/ports/cache-invalidation.d.ts +11 -0
  85. package/dist/application/ports/cache-invalidation.d.ts.map +1 -0
  86. package/dist/application/ports/cache-invalidation.js +13 -0
  87. package/dist/application/ports/cache-invalidation.js.map +1 -0
  88. package/dist/application/ports/cache-purger.d.ts +9 -0
  89. package/dist/application/ports/cache-purger.d.ts.map +1 -0
  90. package/dist/application/ports/cache-purger.js +11 -0
  91. package/dist/application/ports/cache-purger.js.map +1 -0
  92. package/dist/application/ports/job-queue.d.ts +26 -0
  93. package/dist/application/ports/job-queue.d.ts.map +1 -0
  94. package/dist/application/ports/job-queue.js +16 -0
  95. package/dist/application/ports/job-queue.js.map +1 -0
  96. package/dist/application/ports/process-runner.d.ts +25 -0
  97. package/dist/application/ports/process-runner.d.ts.map +1 -0
  98. package/dist/application/ports/process-runner.js +11 -0
  99. package/dist/application/ports/process-runner.js.map +1 -0
  100. package/dist/application/ports/process-store.d.ts +12 -0
  101. package/dist/application/ports/process-store.d.ts.map +1 -0
  102. package/dist/application/ports/process-store.js +7 -0
  103. package/dist/application/ports/process-store.js.map +1 -0
  104. package/dist/application/process/index.d.ts +8 -0
  105. package/dist/application/process/index.d.ts.map +1 -0
  106. package/dist/application/process/index.js +8 -0
  107. package/dist/application/process/index.js.map +1 -0
  108. package/dist/application/process/process-decision.d.ts +18 -0
  109. package/dist/application/process/process-decision.d.ts.map +1 -0
  110. package/dist/application/process/process-decision.js +2 -0
  111. package/dist/application/process/process-decision.js.map +1 -0
  112. package/dist/application/process/process-manager.d.ts +13 -0
  113. package/dist/application/process/process-manager.d.ts.map +1 -0
  114. package/dist/application/process/process-manager.js +2 -0
  115. package/dist/application/process/process-manager.js.map +1 -0
  116. package/dist/application/process/process-timeout-due.d.ts +6 -0
  117. package/dist/application/process/process-timeout-due.d.ts.map +1 -0
  118. package/dist/application/process/process-timeout-due.js +2 -0
  119. package/dist/application/process/process-timeout-due.js.map +1 -0
  120. package/dist/application/process/process-timeout.d.ts +6 -0
  121. package/dist/application/process/process-timeout.d.ts.map +1 -0
  122. package/dist/application/process/process-timeout.js +2 -0
  123. package/dist/application/process/process-timeout.js.map +1 -0
  124. package/dist/application/process/process-type.d.ts +12 -0
  125. package/dist/application/process/process-type.d.ts.map +1 -0
  126. package/dist/application/process/process-type.js +2 -0
  127. package/dist/application/process/process-type.js.map +1 -0
  128. package/dist/application/process/stored-process.d.ts +15 -0
  129. package/dist/application/process/stored-process.d.ts.map +1 -0
  130. package/dist/application/process/stored-process.js +2 -0
  131. package/dist/application/process/stored-process.js.map +1 -0
  132. package/dist/application/process/unreadable-process-state-error.d.ts +6 -0
  133. package/dist/application/process/unreadable-process-state-error.d.ts.map +1 -0
  134. package/dist/application/process/unreadable-process-state-error.js +9 -0
  135. package/dist/application/process/unreadable-process-state-error.js.map +1 -0
  136. package/dist/declarations/decorators/background.d.ts +13 -0
  137. package/dist/declarations/decorators/background.d.ts.map +1 -0
  138. package/dist/declarations/decorators/background.js +41 -0
  139. package/dist/declarations/decorators/background.js.map +1 -0
  140. package/dist/declarations/model/background-job-declaration.d.ts +14 -0
  141. package/dist/declarations/model/background-job-declaration.d.ts.map +1 -0
  142. package/dist/declarations/model/background-job-declaration.js +2 -0
  143. package/dist/declarations/model/background-job-declaration.js.map +1 -0
  144. package/dist/declarations/model/declaration.d.ts +2 -1
  145. package/dist/declarations/model/declaration.d.ts.map +1 -1
  146. package/dist/declarations.d.ts +5 -0
  147. package/dist/declarations.d.ts.map +1 -1
  148. package/dist/declarations.js +1 -0
  149. package/dist/declarations.js.map +1 -1
  150. package/dist/domain/errors/precondition-failed-error.d.ts +12 -0
  151. package/dist/domain/errors/precondition-failed-error.d.ts.map +1 -0
  152. package/dist/domain/errors/precondition-failed-error.js +12 -0
  153. package/dist/domain/errors/precondition-failed-error.js.map +1 -0
  154. package/dist/domain/state-machine/define-state-machine.d.ts +22 -0
  155. package/dist/domain/state-machine/define-state-machine.d.ts.map +1 -0
  156. package/dist/domain/state-machine/define-state-machine.js +23 -0
  157. package/dist/domain/state-machine/define-state-machine.js.map +1 -0
  158. package/dist/domain/state-machine/forbidden-transition.d.ts +6 -0
  159. package/dist/domain/state-machine/forbidden-transition.d.ts.map +1 -0
  160. package/dist/domain/state-machine/forbidden-transition.js +2 -0
  161. package/dist/domain/state-machine/forbidden-transition.js.map +1 -0
  162. package/dist/domain/state-machine/index.d.ts +10 -0
  163. package/dist/domain/state-machine/index.d.ts.map +1 -0
  164. package/dist/domain/state-machine/index.js +10 -0
  165. package/dist/domain/state-machine/index.js.map +1 -0
  166. package/dist/domain/state-machine/invalid-state-machine-error.d.ts +6 -0
  167. package/dist/domain/state-machine/invalid-state-machine-error.d.ts.map +1 -0
  168. package/dist/domain/state-machine/invalid-state-machine-error.js +9 -0
  169. package/dist/domain/state-machine/invalid-state-machine-error.js.map +1 -0
  170. package/dist/domain/state-machine/mermaid-of-state-machine.d.ts +4 -0
  171. package/dist/domain/state-machine/mermaid-of-state-machine.d.ts.map +1 -0
  172. package/dist/domain/state-machine/mermaid-of-state-machine.js +16 -0
  173. package/dist/domain/state-machine/mermaid-of-state-machine.js.map +1 -0
  174. package/dist/domain/state-machine/state-machine-declaration.d.ts +22 -0
  175. package/dist/domain/state-machine/state-machine-declaration.d.ts.map +1 -0
  176. package/dist/domain/state-machine/state-machine-declaration.js +2 -0
  177. package/dist/domain/state-machine/state-machine-declaration.js.map +1 -0
  178. package/dist/domain/state-machine/state-machine-description.d.ts +15 -0
  179. package/dist/domain/state-machine/state-machine-description.d.ts.map +1 -0
  180. package/dist/domain/state-machine/state-machine-description.js +2 -0
  181. package/dist/domain/state-machine/state-machine-description.js.map +1 -0
  182. package/dist/domain/state-machine/state-machine.d.ts +33 -0
  183. package/dist/domain/state-machine/state-machine.d.ts.map +1 -0
  184. package/dist/domain/state-machine/state-machine.js +90 -0
  185. package/dist/domain/state-machine/state-machine.js.map +1 -0
  186. package/dist/domain/state-machine/transition-declaration.d.ts +13 -0
  187. package/dist/domain/state-machine/transition-declaration.d.ts.map +1 -0
  188. package/dist/domain/state-machine/transition-declaration.js +2 -0
  189. package/dist/domain/state-machine/transition-declaration.js.map +1 -0
  190. package/dist/domain/state-machine/transition-error.d.ts +13 -0
  191. package/dist/domain/state-machine/transition-error.d.ts.map +1 -0
  192. package/dist/domain/state-machine/transition-error.js +12 -0
  193. package/dist/domain/state-machine/transition-error.js.map +1 -0
  194. package/dist/index.d.ts +21 -0
  195. package/dist/index.d.ts.map +1 -1
  196. package/dist/index.js +21 -0
  197. package/dist/index.js.map +1 -1
  198. package/dist/infrastructure/cache/published-cache-invalidation.d.ts +33 -0
  199. package/dist/infrastructure/cache/published-cache-invalidation.d.ts.map +1 -0
  200. package/dist/infrastructure/cache/published-cache-invalidation.js +35 -0
  201. package/dist/infrastructure/cache/published-cache-invalidation.js.map +1 -0
  202. package/dist/infrastructure/cache/purging-publisher.d.ts +18 -0
  203. package/dist/infrastructure/cache/purging-publisher.d.ts.map +1 -0
  204. package/dist/infrastructure/cache/purging-publisher.js +33 -0
  205. package/dist/infrastructure/cache/purging-publisher.js.map +1 -0
  206. package/dist/infrastructure/cache/repeated-cache-purger.d.ts +25 -0
  207. package/dist/infrastructure/cache/repeated-cache-purger.d.ts.map +1 -0
  208. package/dist/infrastructure/cache/repeated-cache-purger.js +37 -0
  209. package/dist/infrastructure/cache/repeated-cache-purger.js.map +1 -0
  210. package/dist/infrastructure/domain-events/when-committed.d.ts +10 -0
  211. package/dist/infrastructure/domain-events/when-committed.d.ts.map +1 -0
  212. package/dist/infrastructure/domain-events/when-committed.js +30 -0
  213. package/dist/infrastructure/domain-events/when-committed.js.map +1 -0
  214. package/dist/infrastructure/in-memory/in-memory-cache-invalidation.d.ts +7 -0
  215. package/dist/infrastructure/in-memory/in-memory-cache-invalidation.d.ts.map +1 -0
  216. package/dist/infrastructure/in-memory/in-memory-cache-invalidation.js +8 -0
  217. package/dist/infrastructure/in-memory/in-memory-cache-invalidation.js.map +1 -0
  218. package/dist/infrastructure/in-memory/in-memory-cache-purger.d.ts +13 -0
  219. package/dist/infrastructure/in-memory/in-memory-cache-purger.d.ts.map +1 -0
  220. package/dist/infrastructure/in-memory/in-memory-cache-purger.js +23 -0
  221. package/dist/infrastructure/in-memory/in-memory-cache-purger.js.map +1 -0
  222. package/dist/infrastructure/in-memory/in-memory-process-store.d.ts +11 -0
  223. package/dist/infrastructure/in-memory/in-memory-process-store.d.ts.map +1 -0
  224. package/dist/infrastructure/in-memory/in-memory-process-store.js +29 -0
  225. package/dist/infrastructure/in-memory/in-memory-process-store.js.map +1 -0
  226. package/package.json +1 -1
package/README.md CHANGED
@@ -12,18 +12,18 @@ npm i @ddd-ts-kit/kernel
12
12
 
13
13
  | Area | Exports |
14
14
  |---|---|
15
- | Domain | `Entity`, `AggregateRoot` (version, recorded events), `ValueObject`, `DomainEvent`, `DomainError` with a stable `code`, `ConceptError` (the errors of one concept, each built by a static factory: see "Domain errors"), `isRefusal`, `CodeOf`, `RefusalOf`, `Refusal`, `RefusalFacts`, the generic `InvariantViolation`, `NotFoundError`, `ConflictError`, `ConcurrencyError`, `Result` with `ok`, `err`, `unwrap` |
15
+ | Domain | `Entity`, `AggregateRoot` (version, recorded events), `ValueObject`, `DomainEvent`, `DomainError` with a stable `code`, `ConceptError` (the errors of one concept, each built by a static factory: see "Domain errors"), `isRefusal`, `CodeOf`, `RefusalOf`, `Refusal`, `RefusalFacts`, the generic `InvariantViolation`, `NotFoundError`, `ConflictError`, `ConcurrencyError`, `PreconditionFailedError`, `Result` with `ok`, `err`, `unwrap` |
16
16
  | Ports | `Clock`, `IdGenerator`, `UnitOfWork`, `Repository` (abstract classes: each one is its own injection token), `UseCase`, `DependencyKind` (the roles a binding declares, read by di, tracing and aspects: `repository`, `gateway`, `external`, `use-case`, `publisher`, `consumer`, `domain-event-handler`, `job`) |
17
17
  | Buses | `Command`, `Query`, `CommandBus`, `QueryBus`, `BusMiddleware`, the in-process `InMemoryCommandBus` and `InMemoryQueryBus` (`register(type, handler, { context })` names the bounded context in `MessageInfo`), `unitOfWorkMiddleware` |
18
18
  | Events | `IntegrationEvent`, `IntegrationEventPublisher`, `IntegrationEventSubscriber`, `EventBus` (both at once), `EventSubscribers` (the subscriber of each bounded context), `ProcessedEventStore`, `idempotent`, `InMemoryEventBus`, `DomainEventDispatcher`, `IntegrationEventCallback` and `DomainEventCallback` (the functions a subscriber and a dispatcher are given), `executionContextOfEvent`, `inEventScope`, `scopedPublisher`, `EventDelivery` (what a provider says of one delivery: attempt, last attempt, transport, source, consumer), `messagingSystemOf` |
19
19
  | Domain events and the unit of work | `dispatchingOnSave` (the wrapper that makes a repository hand over what the aggregates it is given recorded), `announceDomainEvents`, `withinUnitOfWork` (what every `UnitOfWork.run` is built on: queue, dispatch before the commit, `after-commit` phase), `unitOfWorkStage` and `UnitOfWorkStage`, `DomainEventPhase`, `DomainEventSubscription`, `SaveAfterCommitError`, `AfterCommitFailure`, `reportAfterCommitFailures`, `InMemoryDomainEventDispatcher` |
20
20
  | Actor | `Actor`, `AuthorizedCommand`, `AuthorizedQuery`, `ActorContext` |
21
- | Standard interfaces | `Session`, `ExecutionContext` and `ExecutionScope`, `Logger`, `Metrics`, `ErrorReporter`, `Cache`, `Lock`, `RateLimiter`, `Page` and `PageRequest`, `AuditStamp`, `TenantScoped`, `AuditTrail`, `AuditReader` and `AuditEntry` |
21
+ | Standard interfaces | `Session`, `ExecutionContext` and `ExecutionScope`, `Logger`, `Metrics`, `ErrorReporter`, `Cache`, `CacheInvalidation` and `CachePurger` (with `cacheTag`, `tenantCacheTags`, `CachePurgeRequest`, `PublishedCacheInvalidation`, `PurgingPublisher`, `RepeatedCachePurger`: see "Purging the HTTP caches"), `Lock`, `RateLimiter`, `Page` and `PageRequest`, `AuditStamp`, `TenantScoped`, `AuditTrail`, `AuditReader` and `AuditEntry` |
22
22
  | Scheduled jobs | `JobScheduler`, `ScheduledJob`, `JobExecution`, `JobSchedule`, `JobOptions`, `JobDefinition`, `JobRun`, `JobDescription`, `parseJobSchedule`, `describeJobSchedule`, `jobPolicy`, `executionContextOfJob` |
23
23
  | Read side | `Criteria`, `defineCriteria`, `validateCriteria`, `scoped`, `planCriteria`, `evaluateCriteria`, `pageOfPlan`, `hydrate`, `ReadModelStore`, `stampOf`, `TableReadModelStore`, `ProjectionRebuild`, `InMemoryReadModelStore` (see "Criteria", "Hydration" and "Projections") |
24
- | Persistence helpers | `optimisticUpdate`, `schemaNameOf`, `stampCreation`, `stampUpdate`, `tenantScopeOf`, `encodeCursor`, `decodeCursor` |
24
+ | Persistence helpers | `optimisticUpdate`, `expectVersion` (the version a caller read, `If-Match` over HTTP, still the current one, else `PreconditionFailedError`: 412), `schemaNameOf`, `stampCreation`, `stampUpdate`, `tenantScopeOf`, `encodeCursor`, `decodeCursor` |
25
25
  | Error reporting | `ErrorReporter`, `ErrorReport`, `errorReportOf`, `reportOnce`, `isUnexpectedError`, `errorReportingMiddleware`, `DeadLetterReports`, `reportProcessFailures`, `NoopErrorReporter` |
26
- | Doubles | `FixedClock`, `SequentialIdGenerator`, `InMemoryUnitOfWork`, `FixedActorContext`, `FixedExecutionScope`, `InMemoryLogger`, `InMemoryMetrics`, `InMemoryErrorReporter`, `InMemoryCache`, `InMemoryLock`, `InMemoryRateLimiter` |
26
+ | Doubles | `FixedClock`, `SequentialIdGenerator`, `InMemoryUnitOfWork`, `FixedActorContext`, `FixedExecutionScope`, `InMemoryLogger`, `InMemoryMetrics`, `InMemoryErrorReporter`, `InMemoryCache`, `InMemoryCacheInvalidation`, `InMemoryCachePurger`, `InMemoryLock`, `InMemoryRateLimiter` |
27
27
  | Declarations (`@ddd-ts-kit/kernel/declarations`) | `Requires`, `Public`, `CommandHandler`, `QueryHandler`, `IntegrationEventHandler`, `DomainEventHandler`, `Scheduled`, `Projection`, `On`, `Repository`, `Gateway`, `Adapter`, `Policy`, `injected`, `injectedDependencies`, `declarationOf`, `listDeclarations`, `Token`, `describeToken`, `tokenIdentity`, `portDefect` |
28
28
 
29
29
  The rules these blocks serve are in `docs/ARCHITECTURE.md`.
@@ -115,6 +115,33 @@ A project says which form it writes with `"stack.refusals"` in `ddd-ts-kit.confi
115
115
  `errors/*` rules hold the first, and `ddd-ts-kit error-factories` moves a project from the
116
116
  second. Both forms can live in one project.
117
117
 
118
+ ## State machines
119
+
120
+ ```ts
121
+ export const parcelLifecycle = defineStateMachine({
122
+ states: Object.values(ParcelStatus),
123
+ initial: [ParcelStatus.Registered],
124
+ transitions: {
125
+ ship: { from: [ParcelStatus.Registered], to: ParcelStatus.Shipped, guard: ({ paid }) => (paid ? undefined : ParcelError.unpaid()) },
126
+ reroute: { from: [ParcelStatus.Shipped] },
127
+ deliver: { from: [ParcelStatus.Shipped], to: ParcelStatus.Delivered },
128
+ },
129
+ refused: ({ from, transition }) => ParcelError.cannot(from, transition),
130
+ });
131
+
132
+ this.state = { ...this.state, status: parcelLifecycle.next(this.state.status, 'ship', { paid }) };
133
+ ```
134
+
135
+ The lifecycle of an aggregate, in `domain/state-machines/<aggregate>.state-machine.ts`.
136
+ `next(from, transition, context)` answers the next status or throws: the refusal `refused` builds
137
+ from a static factory for a transition the state does not allow, or the refusal of a guard.
138
+ `start(state)` checks an initial state, `can(...)` answers without throwing, `allowedFrom(state)`
139
+ and `isFinal(state)` read the graph, `describe()` gives it as data and
140
+ `mermaidOfStateMachine(description)` as a Mermaid diagram. A transition without `to` keeps the
141
+ state. A declaration naming an unknown state throws `InvalidStateMachineError` when the file
142
+ loads. Why, and the rule that holds the aggregate to it:
143
+ [State machines](../../docs/concepts/state-machines.md).
144
+
118
145
  ## Domain events and the unit of work
119
146
 
120
147
  An effect of a domain event exists only if the state change that recorded it is committed.
@@ -516,6 +543,31 @@ receives: `scheduledAt`, `startedAt`, `attempt` and an abort `signal`. What the
516
543
  across a change of clocks, what one instance among several guarantees and how a job is tested
517
544
  are in the README of `@ddd-ts-kit/scheduling`.
518
545
 
546
+ ### Background
547
+
548
+ ```ts
549
+ // contexts/billing/application/background/compile-statements.background-job.ts
550
+ @Background({ maxAttempts: 3, timeout: '10m' })
551
+ export class CompileStatementsBackgroundJob implements BackgroundJob<CompileStatementsPayload> {
552
+ static readonly jobName = 'billing.compile-statements';
553
+
554
+ constructor(private readonly commands: CommandBus) {}
555
+
556
+ async run(payload: CompileStatementsPayload, execution: BackgroundJobExecution): Promise<void> {
557
+ await this.commands.execute(new CompileStatements(execution.jobId, payload.month));
558
+ }
559
+ }
560
+ ```
561
+
562
+ A background job is named by its static `jobName`, which is how a use case enqueues it
563
+ (`jobs.enqueue(CompileStatementsBackgroundJob, { month })`, the payload typed by `run`). The decorator
564
+ takes the options, all optional: `maxAttempts` (5), `backoff` (`{ baseMs: 1000, factor: 2,
565
+ maxMs: 300000 }`), `timeout` and `lease` (30 s), as milliseconds or `'10m'`. A name that is not
566
+ `<context>.<job-name>` and options it cannot read throw a `DeclarationError` when the file
567
+ loads. `BackgroundJobExecution` is what `run` receives: `jobId`, `attempt`, `enqueuedAt`,
568
+ `startedAt`, the `checkpoint` an earlier attempt saved, an abort `signal`, `saveCheckpoint(value)`
569
+ and `reportProgress({ done, total })`.
570
+
519
571
  ### Decorator modes
520
572
 
521
573
  Every decorator of this entry works under both decorator modes of TypeScript, and the same
@@ -711,6 +763,30 @@ The key carries the type of its value. `InMemoryCache(clock, { maxEntries? })` e
711
763
  `Clock` and is bounded. `tenantCacheKey<Quote>(scope, 'quote', pair)` is the same key confined to
712
764
  the tenant of the work in progress: what one tenant cached is never served to another.
713
765
 
766
+ ### Purging the HTTP caches
767
+
768
+ Two ports, so that a use case can never purge before its commit:
769
+
770
+ | Port | Called by | Does |
771
+ |---|---|---|
772
+ | `CacheInvalidation.invalidate(tags)` | a `before-commit` handler of the domain event that changed what the answers show | asks for the tags to be purged once the work commits; `domain-events/transactional-before-commit` allows it |
773
+ | `CachePurger.purge(tags)` | the relay of the outbox, never a use case (refused before the commit) | purges a shared cache now: `InMemoryCachePurger` here, Varnish and the CDNs in `@ddd-ts-kit/http/purgers` |
774
+
775
+ `cacheTag('calendar', id)` names a resource (`calendar:c-1`, the identifier percent-encoded),
776
+ `cacheTag('calendar')` every resource of a kind; the routes of `@ddd-ts-kit/http` tag their
777
+ answers with the same function. `PublishedCacheInvalidation({ publisher, ids, clock, scope, perTenant? })`,
778
+ what `kernelModule()` of `@ddd-ts-kit/di` binds, publishes one `CachePurgeRequest` on the
779
+ publisher, the tags under the tenant of the work (`tenantCacheTags`) when `perTenant` is set, as
780
+ a multi-tenant application whose answers are tagged per tenant sets it (work that names no tenant
781
+ is then refused with `NoCurrentTenantError`: its purge would meet no answer): behind an outbox it
782
+ is a row of the transaction, gone with a rollback, kept across a crash after the commit.
783
+ `new PurgingPublisher(purger, broker)` is what the relay publishes through: the purge requests
784
+ go to the purger, the other events to the broker, and a failed purge throws so that the relay
785
+ retries it. `new RepeatedCachePurger(purger, { againAfterMs, onFailure? })` purges, then the same
786
+ tags once more `againAfterMs` later, for a read that loaded the previous state before the commit
787
+ and reached the cache after the first purge; the second purge is best effort, reported to
788
+ `onFailure`. `InMemoryCacheInvalidation` remembers the tags asked for (`invalidated`), for specs.
789
+
714
790
  ### Lock
715
791
 
716
792
  ```ts
@@ -837,6 +913,55 @@ fills in the defaults. The kernel holds no scheduler: the in-process one, the co
837
913
  occurrences and the manual one for tests are `@ddd-ts-kit/scheduling`, and the contract of the
838
914
  port is `jobSchedulerContract` of `@ddd-ts-kit/testing/contracts`.
839
915
 
916
+ ### JobQueue
917
+
918
+ ```ts
919
+ abstract class JobQueue {
920
+ abstract enqueue<P>(job: BackgroundJobType<P>, payload: P, options?: EnqueueOptions): Promise<EnqueuedJob>;
921
+ abstract cancel(id: string): Promise<boolean>;
922
+ abstract find(id: string): Promise<QueuedJob | undefined>; // status, attempts, progress, last error code
923
+ }
924
+ ```
925
+
926
+ Where a use case hands work to a worker. `EnqueueOptions` are `delay` or `runAt`, an
927
+ `idempotencyKey` (a job of the same name and key, enqueued for the same tenant and already kept,
928
+ is answered, `duplicate: true`) and a `subject` (the id of what the job works on, which `find`
929
+ answers next to the tenant: a query checks the job belongs to the element it was asked about).
930
+ `QueuedJobStatus` is the closed vocabulary of the statuses (`waiting`, `running`, `succeeded`,
931
+ `cancelled`, `dead`), to publish in a contract. Work done for a tenant finds and cancels the
932
+ jobs of that tenant only (an elevation, those of the tenants it names). Called inside a unit of
933
+ work, a job is added or cancelled with the business change: the stores of the ORM packages
934
+ write with the transaction, a store that cannot (Redis, memory) waits for the commit through
935
+ `whenCommitted(write, failed?)`, the helper any such adapter uses. A run happens inside
936
+ `ExecutionScope`, in the context `executionContextOfBackgroundJob({ jobId, enqueuedBy, deadline? })`
937
+ builds: origin `queue`, the correlation, actor and tenant of the work that enqueued it
938
+ (`EnqueuedBy`), the job as the cause. `backgroundJobPolicy(options)` fills in the defaults and
939
+ `retryDelayMs(policy, attempt)` is the backoff. The queue, its worker and its stores are
940
+ `@ddd-ts-kit/scheduling` and the ORM packages; why and how:
941
+ [Background jobs](../../docs/concepts/background-jobs.md).
942
+
943
+ ### ProcessRunner and ProcessStore
944
+
945
+ ```ts
946
+ abstract class ProcessRunner {
947
+ abstract run<State>(process: ProcessType<State>, id: string, step: (current: State | undefined) => ProcessDecision<State> | undefined): Promise<void>;
948
+ abstract timeout<State>(process: ProcessType<State>, due: ProcessTimeoutDue & { jobId: string }, step: (current: State) => ProcessDecision<State> | undefined): Promise<void>;
949
+ }
950
+
951
+ abstract class ProcessStore {
952
+ abstract load(name: string, id: string): Promise<StoredProcess | undefined>;
953
+ abstract save(process: StoredProcess): Promise<void>; // ConcurrencyError unless version is the stored one plus 1
954
+ }
955
+ ```
956
+
957
+ Process managers: a class of `application/processes/` (`ProcessManager<State>`) with a static
958
+ `processName`, an optional static `timeoutJob` and a static `isState` guard; its steps answer a
959
+ `ProcessDecision` (`state`, `commands`, `timeouts` as `{ name, at }`, `cancel`, `ended`). A state
960
+ the guard does not recognise throws `UnreadableProcessStateError`. `InMemoryProcessStore` keeps
961
+ the instances as JSON, for tests and one process. The runner is `StoredProcessRunner` of
962
+ `@ddd-ts-kit/scheduling`; why and how:
963
+ [Sagas and process managers](../../docs/concepts/sagas-and-process-managers.md).
964
+
840
965
  ### RateLimiter
841
966
 
842
967
  ```ts
@@ -0,0 +1,27 @@
1
+ import type { Actor } from '../actor/actor.js';
2
+ import type { JobProgress } from './job-progress.js';
3
+ /** What a run of a background job is told about itself, and how it reports back. */
4
+ export interface BackgroundJobExecution {
5
+ /** The id `enqueue` answered: the same for every attempt. */
6
+ readonly jobId: string;
7
+ /** 1 for the first run, counted at each claim: a retry, a crash or an expired lease adds one. */
8
+ readonly attempt: number;
9
+ readonly enqueuedAt: Date;
10
+ /** Who enqueued the job, whom its commands are sent as: a job acts for the work that asked for it. */
11
+ readonly actor: Actor;
12
+ readonly startedAt: Date;
13
+ /** What the last `saveCheckpoint` of an earlier attempt stored, to resume from. Absent on a fresh start. */
14
+ readonly checkpoint: unknown;
15
+ /**
16
+ * Aborted when the run exceeds its timeout, the job is cancelled, its lease is lost to another
17
+ * worker, or the worker stops: a long job checks it between two steps and passes it on.
18
+ */
19
+ readonly signal: AbortSignal;
20
+ /** The W3C `traceparent` of the work that enqueued the job, which the run continues when tracing is on. */
21
+ readonly traceparent?: string;
22
+ /** Stores how far the work went, plain JSON, so that the next attempt resumes there. Extends the lease. */
23
+ saveCheckpoint(checkpoint: unknown): Promise<void>;
24
+ /** Stores the progress a screen may show. Extends the lease. */
25
+ reportProgress(progress: JobProgress): Promise<void>;
26
+ }
27
+ //# sourceMappingURL=background-job-execution.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"background-job-execution.d.ts","sourceRoot":"","sources":["../../../src/application/background/background-job-execution.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErD,oFAAoF;AACpF,MAAM,WAAW,sBAAsB;IACrC,6DAA6D;IAC7D,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,iGAAiG;IACjG,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,UAAU,EAAE,IAAI,CAAC;IAC1B,sGAAsG;IACtG,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC;IACzB,4GAA4G;IAC5G,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAC7B;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,2GAA2G;IAC3G,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,2GAA2G;IAC3G,cAAc,CAAC,UAAU,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACnD,gEAAgE;IAChE,cAAc,CAAC,QAAQ,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACtD"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=background-job-execution.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"background-job-execution.js","sourceRoot":"","sources":["../../../src/application/background/background-job-execution.ts"],"names":[],"mappings":""}
@@ -0,0 +1,16 @@
1
+ import type { DurationText } from '../scheduling/duration-text.js';
2
+ import type { JobBackoff } from './job-backoff.js';
3
+ export interface BackgroundJobOptions {
4
+ /** Attempts before the job is declared dead. Default 5. */
5
+ readonly maxAttempts?: number;
6
+ readonly backoff?: JobBackoff;
7
+ /** Aborts a run that lasts longer, through its signal, and counts it as failed. Default: no limit. */
8
+ readonly timeout?: number | DurationText;
9
+ /**
10
+ * How long a claimed job stays invisible to other workers without news of its own: the worker
11
+ * extends it while the run goes on, so only a worker that died lets it expire, and the job is
12
+ * then claimed again. Default 30 seconds.
13
+ */
14
+ readonly lease?: number | DurationText;
15
+ }
16
+ //# sourceMappingURL=background-job-options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"background-job-options.d.ts","sourceRoot":"","sources":["../../../src/application/background/background-job-options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAC;AACnE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,MAAM,WAAW,oBAAoB;IACnC,2DAA2D;IAC3D,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,OAAO,CAAC,EAAE,UAAU,CAAC;IAC9B,sGAAsG;IACtG,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,YAAY,CAAC;IACzC;;;;OAIG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,YAAY,CAAC;CACxC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=background-job-options.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"background-job-options.js","sourceRoot":"","sources":["../../../src/application/background/background-job-options.ts"],"names":[],"mappings":""}
@@ -0,0 +1,12 @@
1
+ import type { BackgroundJobOptions } from './background-job-options.js';
2
+ import type { JobBackoff } from './job-backoff.js';
3
+ /** The options of a background job with their defaults filled in, durations in milliseconds. */
4
+ export interface BackgroundJobPolicy {
5
+ readonly maxAttempts: number;
6
+ readonly backoff: Required<JobBackoff>;
7
+ readonly leaseMs: number;
8
+ readonly timeoutMs?: number;
9
+ }
10
+ /** Validates the options and fills in the defaults. Throws `InvalidJobScheduleError`. */
11
+ export declare function backgroundJobPolicy(options?: BackgroundJobOptions): BackgroundJobPolicy;
12
+ //# sourceMappingURL=background-job-policy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"background-job-policy.d.ts","sourceRoot":"","sources":["../../../src/application/background/background-job-policy.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACxE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,gGAAgG;AAChG,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,UAAU,CAAC,CAAC;IACvC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B;AAMD,yFAAyF;AACzF,wBAAgB,mBAAmB,CAAC,OAAO,GAAE,oBAAyB,GAAG,mBAAmB,CAe3F"}
@@ -0,0 +1,24 @@
1
+ import { InvalidJobScheduleError } from '../scheduling/invalid-job-schedule-error.js';
2
+ import { parseDuration } from '../scheduling/parse-duration.js';
3
+ const DEFAULT_LEASE_MS = 30_000;
4
+ const DEFAULT_BACKOFF = { baseMs: 1_000, factor: 2, maxMs: 300_000 };
5
+ /** Validates the options and fills in the defaults. Throws `InvalidJobScheduleError`. */
6
+ export function backgroundJobPolicy(options = {}) {
7
+ const { maxAttempts = 5, backoff = {}, lease = DEFAULT_LEASE_MS, timeout } = options;
8
+ const filled = { ...DEFAULT_BACKOFF, ...backoff };
9
+ assertWhole('maxAttempts', maxAttempts, 1);
10
+ assertWhole('backoff.baseMs', filled.baseMs, 0);
11
+ assertWhole('backoff.maxMs', filled.maxMs, filled.baseMs);
12
+ if (!Number.isFinite(filled.factor) || filled.factor < 1) {
13
+ throw new InvalidJobScheduleError(`backoff.factor is ${filled.factor}: a number of 1 or more is expected.`);
14
+ }
15
+ const policy = { maxAttempts, backoff: filled, leaseMs: parseDuration(lease) };
16
+ return timeout === undefined ? policy : { ...policy, timeoutMs: parseDuration(timeout) };
17
+ }
18
+ function assertWhole(option, value, least) {
19
+ if (Number.isSafeInteger(value) && value >= least) {
20
+ return;
21
+ }
22
+ throw new InvalidJobScheduleError(`${option} is ${value}: a whole number of ${least} or more is expected.`);
23
+ }
24
+ //# sourceMappingURL=background-job-policy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"background-job-policy.js","sourceRoot":"","sources":["../../../src/application/background/background-job-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,6CAA6C,CAAC;AACtF,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAYhE,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAEhC,MAAM,eAAe,GAAyB,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;AAE3F,yFAAyF;AACzF,MAAM,UAAU,mBAAmB,CAAC,OAAO,GAAyB,EAAE;IACpE,MAAM,EAAE,WAAW,GAAG,CAAC,EAAE,OAAO,GAAG,EAAE,EAAE,KAAK,GAAG,gBAAgB,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IACrF,MAAM,MAAM,GAAG,EAAE,GAAG,eAAe,EAAE,GAAG,OAAO,EAAE,CAAC;IAElD,WAAW,CAAC,aAAa,EAAE,WAAW,EAAE,CAAC,CAAC,CAAC;IAC3C,WAAW,CAAC,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IAChD,WAAW,CAAC,eAAe,EAAE,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IAE1D,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACzD,MAAM,IAAI,uBAAuB,CAAC,qBAAqB,MAAM,CAAC,MAAM,sCAAsC,CAAC,CAAC;IAC9G,CAAC;IAED,MAAM,MAAM,GAAG,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC;IAE/E,OAAO,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,GAAG,MAAM,EAAE,SAAS,EAAE,aAAa,CAAC,OAAO,CAAC,EAAE,CAAC;AAC3F,CAAC;AAED,SAAS,WAAW,CAAC,MAAc,EAAE,KAAa,EAAE,KAAa;IAC/D,IAAI,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,KAAK,EAAE,CAAC;QAClD,OAAO;IACT,CAAC;IAED,MAAM,IAAI,uBAAuB,CAAC,GAAG,MAAM,OAAO,KAAK,uBAAuB,KAAK,uBAAuB,CAAC,CAAC;AAC9G,CAAC"}
@@ -0,0 +1,10 @@
1
+ import type { BackgroundJob } from './background-job.js';
2
+ /**
3
+ * The class of a background job, as `enqueue` takes it: its static `jobName` names it, and its
4
+ * `run` gives the type of the payload.
5
+ */
6
+ export interface BackgroundJobType<Payload> {
7
+ readonly jobName: string;
8
+ readonly prototype: BackgroundJob<Payload>;
9
+ }
10
+ //# sourceMappingURL=background-job-type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"background-job-type.d.ts","sourceRoot":"","sources":["../../../src/application/background/background-job-type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;;GAGG;AACH,MAAM,WAAW,iBAAiB,CAAC,OAAO;IACxC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,SAAS,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;CAC5C"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=background-job-type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"background-job-type.js","sourceRoot":"","sources":["../../../src/application/background/background-job-type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,14 @@
1
+ import type { BackgroundJobExecution } from './background-job-execution.js';
2
+ /**
3
+ * A class of `application/background/`: work a use case hands to a worker, to run later and
4
+ * outside of its request. Like a scheduled job it decides nothing: it triggers use cases. Its
5
+ * name is a static `jobName`, `<context>.<job-name>`, which is also how a use case enqueues it:
6
+ * `jobs.enqueue(CompileMonthlyReportJob, { practiceId, month })`.
7
+ *
8
+ * Delivery is at least once: a run may be repeated after a crash, an expired lease or a retry,
9
+ * so the work is idempotent, and a long one saves a checkpoint to resume from.
10
+ */
11
+ export interface BackgroundJob<Payload> {
12
+ run(payload: Payload, execution: BackgroundJobExecution): Promise<void>;
13
+ }
14
+ //# sourceMappingURL=background-job.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"background-job.d.ts","sourceRoot":"","sources":["../../../src/application/background/background-job.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AAE5E;;;;;;;;GAQG;AACH,MAAM,WAAW,aAAa,CAAC,OAAO;IACpC,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,sBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACzE"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=background-job.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"background-job.js","sourceRoot":"","sources":["../../../src/application/background/background-job.ts"],"names":[],"mappings":""}
@@ -0,0 +1,20 @@
1
+ import type { DurationText } from '../scheduling/duration-text.js';
2
+ export interface EnqueueOptions {
3
+ /** Runs no earlier than this long after the commit. Default: as soon as a worker is free. */
4
+ readonly delay?: number | DurationText;
5
+ /** Runs no earlier than this instant. Not with `delay`. */
6
+ readonly runAt?: Date;
7
+ /**
8
+ * A second job of the same name and key, enqueued for the same tenant, is not added while the
9
+ * first is kept: `enqueue` answers the id of the first. Two tenants never share a key. Derive
10
+ * it from the work (`practice-42:2026-10`), never from a random value.
11
+ */
12
+ readonly idempotencyKey?: string;
13
+ /**
14
+ * The id of what the job works on (a calendar, an import), kept with the job and answered by
15
+ * `find`, which never shows the payload: a query reading a job through the element it was
16
+ * asked about checks that the job is about that element.
17
+ */
18
+ readonly subject?: string;
19
+ }
20
+ //# sourceMappingURL=enqueue-options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enqueue-options.d.ts","sourceRoot":"","sources":["../../../src/application/background/enqueue-options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAC;AAEnE,MAAM,WAAW,cAAc;IAC7B,6FAA6F;IAC7F,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,YAAY,CAAC;IACvC,2DAA2D;IAC3D,QAAQ,CAAC,KAAK,CAAC,EAAE,IAAI,CAAC;IACtB;;;;OAIG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IACjC;;;;OAIG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;CAC3B"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=enqueue-options.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enqueue-options.js","sourceRoot":"","sources":["../../../src/application/background/enqueue-options.ts"],"names":[],"mappings":""}
@@ -0,0 +1,13 @@
1
+ import type { Actor } from '../actor/actor.js';
2
+ /**
3
+ * What travels with a background job from the work that enqueued it to its run: the run belongs
4
+ * to the same correlation and the same trace, acts as the same actor and for the same tenant.
5
+ */
6
+ export interface EnqueuedBy {
7
+ readonly correlationId: string;
8
+ readonly actor: Actor;
9
+ readonly tenantId?: string;
10
+ /** W3C `traceparent` of the span that enqueued it, when tracing is on. */
11
+ readonly traceparent?: string;
12
+ }
13
+ //# sourceMappingURL=enqueued-by.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enqueued-by.d.ts","sourceRoot":"","sources":["../../../src/application/background/enqueued-by.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAE/C;;;GAGG;AACH,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,0EAA0E;IAC1E,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;CAC/B"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=enqueued-by.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enqueued-by.js","sourceRoot":"","sources":["../../../src/application/background/enqueued-by.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ /** What `enqueue` answers. */
2
+ export interface EnqueuedJob {
3
+ readonly id: string;
4
+ /** True when a job of the same name and idempotency key was already there: its id is answered and nothing is added. */
5
+ readonly duplicate: boolean;
6
+ }
7
+ //# sourceMappingURL=enqueued-job.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enqueued-job.d.ts","sourceRoot":"","sources":["../../../src/application/background/enqueued-job.ts"],"names":[],"mappings":"AAAA,8BAA8B;AAC9B,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,uHAAuH;IACvH,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;CAC7B"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=enqueued-job.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enqueued-job.js","sourceRoot":"","sources":["../../../src/application/background/enqueued-job.ts"],"names":[],"mappings":""}
@@ -0,0 +1,14 @@
1
+ import { type ExecutionContext } from '../execution/execution-context.js';
2
+ import type { EnqueuedBy } from './enqueued-by.js';
3
+ export interface BackgroundJobStart {
4
+ readonly jobId: string;
5
+ readonly enqueuedBy: EnqueuedBy;
6
+ /** The instant the timeout of the job ends the run, when it has one. */
7
+ readonly deadline?: Date;
8
+ }
9
+ /**
10
+ * The execution context of one run: origin `queue`, the correlation, the actor and the tenant of
11
+ * the work that enqueued the job, and the job as the direct cause.
12
+ */
13
+ export declare function executionContextOfBackgroundJob(start: BackgroundJobStart): ExecutionContext;
14
+ //# sourceMappingURL=execution-context-of-background-job.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"execution-context-of-background-job.d.ts","sourceRoot":"","sources":["../../../src/application/background/execution-context-of-background-job.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,gBAAgB,EAAoB,MAAM,mCAAmC,CAAC;AAC5F,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC;IAChC,wEAAwE;IACxE,QAAQ,CAAC,QAAQ,CAAC,EAAE,IAAI,CAAC;CAC1B;AAED;;;GAGG;AACH,wBAAgB,+BAA+B,CAAC,KAAK,EAAE,kBAAkB,GAAG,gBAAgB,CAY3F"}
@@ -0,0 +1,18 @@
1
+ import { executionContext } from '../execution/execution-context.js';
2
+ /**
3
+ * The execution context of one run: origin `queue`, the correlation, the actor and the tenant of
4
+ * the work that enqueued the job, and the job as the direct cause.
5
+ */
6
+ export function executionContextOfBackgroundJob(start) {
7
+ const { jobId, enqueuedBy, deadline } = start;
8
+ const { correlationId, actor, tenantId } = enqueuedBy;
9
+ return executionContext({
10
+ correlationId,
11
+ causationId: jobId,
12
+ actor,
13
+ origin: 'queue',
14
+ ...(tenantId === undefined ? {} : { tenantId }),
15
+ ...(deadline === undefined ? {} : { deadline }),
16
+ });
17
+ }
18
+ //# sourceMappingURL=execution-context-of-background-job.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"execution-context-of-background-job.js","sourceRoot":"","sources":["../../../src/application/background/execution-context-of-background-job.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AAU5F;;;GAGG;AACH,MAAM,UAAU,+BAA+B,CAAC,KAAyB;IACvE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAC;IAC9C,MAAM,EAAE,aAAa,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,UAAU,CAAC;IAEtD,OAAO,gBAAgB,CAAC;QACtB,aAAa;QACb,WAAW,EAAE,KAAK;QAClB,KAAK;QACL,MAAM,EAAE,OAAO;QACf,GAAG,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC;QAC/C,GAAG,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC;KAChD,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,15 @@
1
+ export * from './background-job-execution.js';
2
+ export * from './background-job-options.js';
3
+ export * from './background-job-policy.js';
4
+ export * from './background-job-type.js';
5
+ export * from './background-job.js';
6
+ export * from './enqueue-options.js';
7
+ export * from './enqueued-by.js';
8
+ export * from './enqueued-job.js';
9
+ export * from './execution-context-of-background-job.js';
10
+ export * from './job-backoff.js';
11
+ export * from './job-progress.js';
12
+ export * from './queued-job-status.js';
13
+ export * from './queued-job.js';
14
+ export * from './retry-delay-ms.js';
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/application/background/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,0CAA0C,CAAC;AACzD,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC"}
@@ -0,0 +1,15 @@
1
+ export * from './background-job-execution.js';
2
+ export * from './background-job-options.js';
3
+ export * from './background-job-policy.js';
4
+ export * from './background-job-type.js';
5
+ export * from './background-job.js';
6
+ export * from './enqueue-options.js';
7
+ export * from './enqueued-by.js';
8
+ export * from './enqueued-job.js';
9
+ export * from './execution-context-of-background-job.js';
10
+ export * from './job-backoff.js';
11
+ export * from './job-progress.js';
12
+ export * from './queued-job-status.js';
13
+ export * from './queued-job.js';
14
+ export * from './retry-delay-ms.js';
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/application/background/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,0CAA0C,CAAC;AACzD,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC"}
@@ -0,0 +1,10 @@
1
+ /** The delay before each retry: `baseMs`, then multiplied by `factor` at each further attempt, never above `maxMs`. */
2
+ export interface JobBackoff {
3
+ /** Default 1000. */
4
+ readonly baseMs?: number;
5
+ /** Default 2. */
6
+ readonly factor?: number;
7
+ /** Default 300000 (five minutes). */
8
+ readonly maxMs?: number;
9
+ }
10
+ //# sourceMappingURL=job-backoff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"job-backoff.d.ts","sourceRoot":"","sources":["../../../src/application/background/job-backoff.ts"],"names":[],"mappings":"AAAA,uHAAuH;AACvH,MAAM,WAAW,UAAU;IACzB,oBAAoB;IACpB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,iBAAiB;IACjB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,qCAAqC;IACrC,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACzB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=job-backoff.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"job-backoff.js","sourceRoot":"","sources":["../../../src/application/background/job-backoff.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ /** How far a background job went: `{ done: 120, total: 450 }`. */
2
+ export interface JobProgress {
3
+ readonly done: number;
4
+ /** Absent while the job does not know it yet. */
5
+ readonly total?: number;
6
+ }
7
+ //# sourceMappingURL=job-progress.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"job-progress.d.ts","sourceRoot":"","sources":["../../../src/application/background/job-progress.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,iDAAiD;IACjD,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACzB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=job-progress.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"job-progress.js","sourceRoot":"","sources":["../../../src/application/background/job-progress.ts"],"names":[],"mappings":""}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Where a background job stands, as a closed vocabulary a project can publish in its contract
3
+ * (`@IsIn(Object.values(QueuedJobStatus))`):
4
+ *
5
+ * - `waiting`: due at `runAt`, or later after a failure.
6
+ * - `running`: claimed by a worker, whose lease ends at `leaseUntil`.
7
+ * - `succeeded`, `cancelled`: over.
8
+ * - `dead`: every attempt failed; kept until an operator retries it.
9
+ */
10
+ export declare const QueuedJobStatus: {
11
+ readonly Waiting: 'waiting';
12
+ readonly Running: 'running';
13
+ readonly Succeeded: 'succeeded';
14
+ readonly Cancelled: 'cancelled';
15
+ readonly Dead: 'dead';
16
+ };
17
+ export type QueuedJobStatus = (typeof QueuedJobStatus)[keyof typeof QueuedJobStatus];
18
+ //# sourceMappingURL=queued-job-status.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queued-job-status.d.ts","sourceRoot":"","sources":["../../../src/application/background/queued-job-status.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,eAAO,MAAM,eAAe;aAC1B,OAAO,EAAE,SAAS;aAClB,OAAO,EAAE,SAAS;aAClB,SAAS,EAAE,WAAW;aACtB,SAAS,EAAE,WAAW;aACtB,IAAI,EAAE,MAAM;CACJ,CAAC;AAEX,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,eAAe,CAAC,CAAC,MAAM,OAAO,eAAe,CAAC,CAAC"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Where a background job stands, as a closed vocabulary a project can publish in its contract
3
+ * (`@IsIn(Object.values(QueuedJobStatus))`):
4
+ *
5
+ * - `waiting`: due at `runAt`, or later after a failure.
6
+ * - `running`: claimed by a worker, whose lease ends at `leaseUntil`.
7
+ * - `succeeded`, `cancelled`: over.
8
+ * - `dead`: every attempt failed; kept until an operator retries it.
9
+ */
10
+ export const QueuedJobStatus = {
11
+ Waiting: 'waiting',
12
+ Running: 'running',
13
+ Succeeded: 'succeeded',
14
+ Cancelled: 'cancelled',
15
+ Dead: 'dead',
16
+ };
17
+ //# sourceMappingURL=queued-job-status.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queued-job-status.js","sourceRoot":"","sources":["../../../src/application/background/queued-job-status.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B,OAAO,EAAE,SAAS;IAClB,OAAO,EAAE,SAAS;IAClB,SAAS,EAAE,WAAW;IACtB,SAAS,EAAE,WAAW;IACtB,IAAI,EAAE,MAAM;CACJ,CAAC"}