effect 4.0.0-beta.64 → 4.0.0-beta.66

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 (252) hide show
  1. package/dist/Cause.d.ts +4 -4
  2. package/dist/Cause.d.ts.map +1 -1
  3. package/dist/Config.d.ts +1 -2
  4. package/dist/Config.d.ts.map +1 -1
  5. package/dist/Config.js +7 -6
  6. package/dist/Config.js.map +1 -1
  7. package/dist/Context.d.ts +3 -4
  8. package/dist/Context.d.ts.map +1 -1
  9. package/dist/Context.js +10 -8
  10. package/dist/Context.js.map +1 -1
  11. package/dist/DateTime.d.ts +1 -1
  12. package/dist/DateTime.d.ts.map +1 -1
  13. package/dist/DateTime.js +2 -2
  14. package/dist/DateTime.js.map +1 -1
  15. package/dist/Effect.d.ts +337 -412
  16. package/dist/Effect.d.ts.map +1 -1
  17. package/dist/Effect.js +5 -32
  18. package/dist/Effect.js.map +1 -1
  19. package/dist/Effectable.d.ts +1 -1
  20. package/dist/Effectable.d.ts.map +1 -1
  21. package/dist/Effectable.js +1 -1
  22. package/dist/Effectable.js.map +1 -1
  23. package/dist/ExecutionPlan.d.ts +2 -3
  24. package/dist/ExecutionPlan.d.ts.map +1 -1
  25. package/dist/ExecutionPlan.js +1 -1
  26. package/dist/ExecutionPlan.js.map +1 -1
  27. package/dist/FiberHandle.d.ts +2 -2
  28. package/dist/FiberHandle.js +2 -2
  29. package/dist/FiberMap.d.ts +2 -2
  30. package/dist/FiberMap.js +2 -2
  31. package/dist/FiberSet.d.ts +1 -1
  32. package/dist/FiberSet.js +1 -1
  33. package/dist/Layer.js +1 -1
  34. package/dist/Layer.js.map +1 -1
  35. package/dist/LayerMap.d.ts.map +1 -1
  36. package/dist/LayerMap.js +3 -3
  37. package/dist/LayerMap.js.map +1 -1
  38. package/dist/ManagedRuntime.d.ts +1 -1
  39. package/dist/ManagedRuntime.js +1 -1
  40. package/dist/Option.d.ts +11 -7
  41. package/dist/Option.d.ts.map +1 -1
  42. package/dist/Option.js +74 -0
  43. package/dist/Option.js.map +1 -1
  44. package/dist/Result.d.ts +11 -3
  45. package/dist/Result.d.ts.map +1 -1
  46. package/dist/Result.js.map +1 -1
  47. package/dist/Schedule.js +1 -1
  48. package/dist/Schedule.js.map +1 -1
  49. package/dist/Schema.d.ts +12 -12
  50. package/dist/Schema.d.ts.map +1 -1
  51. package/dist/Schema.js +6 -4
  52. package/dist/Schema.js.map +1 -1
  53. package/dist/SchemaGetter.js +2 -2
  54. package/dist/SchemaGetter.js.map +1 -1
  55. package/dist/SchemaRepresentation.d.ts +2 -2
  56. package/dist/Stream.d.ts +3 -3
  57. package/dist/Stream.js +1 -1
  58. package/dist/TxRef.js +1 -1
  59. package/dist/TxRef.js.map +1 -1
  60. package/dist/internal/concurrency.js +2 -2
  61. package/dist/internal/concurrency.js.map +1 -1
  62. package/dist/internal/core.js +8 -20
  63. package/dist/internal/core.js.map +1 -1
  64. package/dist/internal/effect.js +20 -19
  65. package/dist/internal/effect.js.map +1 -1
  66. package/dist/internal/executionPlan.js +2 -2
  67. package/dist/internal/executionPlan.js.map +1 -1
  68. package/dist/internal/option.js +5 -8
  69. package/dist/internal/option.js.map +1 -1
  70. package/dist/internal/result.js +5 -8
  71. package/dist/internal/result.js.map +1 -1
  72. package/dist/unstable/ai/LanguageModel.d.ts +5 -5
  73. package/dist/unstable/ai/LanguageModel.d.ts.map +1 -1
  74. package/dist/unstable/ai/LanguageModel.js +1 -1
  75. package/dist/unstable/ai/LanguageModel.js.map +1 -1
  76. package/dist/unstable/ai/McpServer.js +2 -2
  77. package/dist/unstable/ai/McpServer.js.map +1 -1
  78. package/dist/unstable/ai/Model.d.ts +5 -1
  79. package/dist/unstable/ai/Model.d.ts.map +1 -1
  80. package/dist/unstable/ai/Model.js +4 -4
  81. package/dist/unstable/ai/Model.js.map +1 -1
  82. package/dist/unstable/ai/Toolkit.d.ts +1 -3
  83. package/dist/unstable/ai/Toolkit.d.ts.map +1 -1
  84. package/dist/unstable/ai/Toolkit.js +28 -31
  85. package/dist/unstable/ai/Toolkit.js.map +1 -1
  86. package/dist/unstable/cli/Command.d.ts +25 -5
  87. package/dist/unstable/cli/Command.d.ts.map +1 -1
  88. package/dist/unstable/cli/Command.js.map +1 -1
  89. package/dist/unstable/cli/GlobalFlag.d.ts +1 -1
  90. package/dist/unstable/cli/GlobalFlag.d.ts.map +1 -1
  91. package/dist/unstable/cli/Param.js +1 -1
  92. package/dist/unstable/cli/Param.js.map +1 -1
  93. package/dist/unstable/cli/Prompt.d.ts +1 -2
  94. package/dist/unstable/cli/Prompt.d.ts.map +1 -1
  95. package/dist/unstable/cli/Prompt.js +8 -10
  96. package/dist/unstable/cli/Prompt.js.map +1 -1
  97. package/dist/unstable/cli/internal/command.d.ts +2 -6
  98. package/dist/unstable/cli/internal/command.d.ts.map +1 -1
  99. package/dist/unstable/cli/internal/command.js +7 -9
  100. package/dist/unstable/cli/internal/command.js.map +1 -1
  101. package/dist/unstable/cluster/ClusterWorkflowEngine.js +1 -1
  102. package/dist/unstable/cluster/ClusterWorkflowEngine.js.map +1 -1
  103. package/dist/unstable/cluster/Entity.js +3 -3
  104. package/dist/unstable/cluster/Entity.js.map +1 -1
  105. package/dist/unstable/cluster/MessageStorage.js +1 -1
  106. package/dist/unstable/cluster/MessageStorage.js.map +1 -1
  107. package/dist/unstable/cluster/ShardingConfig.js +1 -1
  108. package/dist/unstable/cluster/ShardingConfig.js.map +1 -1
  109. package/dist/unstable/eventlog/EventLogMessage.d.ts +4 -4
  110. package/dist/unstable/eventlog/SqlEventLogServerUnencrypted.js +2 -1
  111. package/dist/unstable/eventlog/SqlEventLogServerUnencrypted.js.map +1 -1
  112. package/dist/unstable/http/HttpBody.js +2 -2
  113. package/dist/unstable/http/HttpBody.js.map +1 -1
  114. package/dist/unstable/http/HttpClient.js +1 -1
  115. package/dist/unstable/http/HttpClient.js.map +1 -1
  116. package/dist/unstable/http/HttpClientRequest.js +2 -2
  117. package/dist/unstable/http/HttpClientRequest.js.map +1 -1
  118. package/dist/unstable/http/HttpMiddleware.d.ts.map +1 -1
  119. package/dist/unstable/http/HttpMiddleware.js +18 -21
  120. package/dist/unstable/http/HttpMiddleware.js.map +1 -1
  121. package/dist/unstable/http/HttpPlatform.js +1 -1
  122. package/dist/unstable/http/HttpPlatform.js.map +1 -1
  123. package/dist/unstable/http/HttpRouter.d.ts.map +1 -1
  124. package/dist/unstable/http/HttpRouter.js +4 -4
  125. package/dist/unstable/http/HttpRouter.js.map +1 -1
  126. package/dist/unstable/http/HttpServer.js +1 -1
  127. package/dist/unstable/http/HttpServer.js.map +1 -1
  128. package/dist/unstable/http/HttpServerRequest.js +11 -11
  129. package/dist/unstable/http/HttpServerRequest.js.map +1 -1
  130. package/dist/unstable/http/HttpServerResponse.d.ts.map +1 -1
  131. package/dist/unstable/http/HttpServerResponse.js +5 -5
  132. package/dist/unstable/http/HttpServerResponse.js.map +1 -1
  133. package/dist/unstable/http/Multipart.js +1 -1
  134. package/dist/unstable/http/Multipart.js.map +1 -1
  135. package/dist/unstable/httpapi/HttpApiBuilder.js +3 -3
  136. package/dist/unstable/httpapi/HttpApiBuilder.js.map +1 -1
  137. package/dist/unstable/httpapi/HttpApiClient.js +1 -1
  138. package/dist/unstable/httpapi/HttpApiClient.js.map +1 -1
  139. package/dist/unstable/httpapi/HttpApiMiddleware.d.ts +2 -0
  140. package/dist/unstable/httpapi/HttpApiMiddleware.d.ts.map +1 -1
  141. package/dist/unstable/httpapi/HttpApiMiddleware.js +2 -1
  142. package/dist/unstable/httpapi/HttpApiMiddleware.js.map +1 -1
  143. package/dist/unstable/httpapi/HttpApiTest.d.ts +3 -1
  144. package/dist/unstable/httpapi/HttpApiTest.d.ts.map +1 -1
  145. package/dist/unstable/httpapi/HttpApiTest.js +2 -2
  146. package/dist/unstable/httpapi/HttpApiTest.js.map +1 -1
  147. package/dist/unstable/persistence/RateLimiter.js +1 -1
  148. package/dist/unstable/persistence/RateLimiter.js.map +1 -1
  149. package/dist/unstable/process/ChildProcess.d.ts +2 -45
  150. package/dist/unstable/process/ChildProcess.d.ts.map +1 -1
  151. package/dist/unstable/process/ChildProcess.js +51 -7
  152. package/dist/unstable/process/ChildProcess.js.map +1 -1
  153. package/dist/unstable/reactivity/Atom.d.ts.map +1 -1
  154. package/dist/unstable/reactivity/Atom.js +4 -4
  155. package/dist/unstable/reactivity/Atom.js.map +1 -1
  156. package/dist/unstable/rpc/RpcClient.js +1 -1
  157. package/dist/unstable/rpc/RpcClient.js.map +1 -1
  158. package/dist/unstable/rpc/RpcServer.d.ts.map +1 -1
  159. package/dist/unstable/rpc/RpcServer.js +1 -1
  160. package/dist/unstable/rpc/RpcServer.js.map +1 -1
  161. package/dist/unstable/rpc/RpcWorker.js +1 -1
  162. package/dist/unstable/rpc/RpcWorker.js.map +1 -1
  163. package/dist/unstable/socket/Socket.d.ts.map +1 -1
  164. package/dist/unstable/socket/Socket.js +1 -1
  165. package/dist/unstable/socket/Socket.js.map +1 -1
  166. package/dist/unstable/sql/Migrator.d.ts.map +1 -1
  167. package/dist/unstable/sql/Migrator.js +3 -2
  168. package/dist/unstable/sql/Migrator.js.map +1 -1
  169. package/dist/unstable/sql/SqlError.d.ts +23 -3
  170. package/dist/unstable/sql/SqlError.d.ts.map +1 -1
  171. package/dist/unstable/sql/SqlError.js +62 -2
  172. package/dist/unstable/sql/SqlError.js.map +1 -1
  173. package/dist/unstable/workflow/Activity.js +1 -1
  174. package/dist/unstable/workflow/Activity.js.map +1 -1
  175. package/dist/unstable/workflow/DurableQueue.d.ts +122 -0
  176. package/dist/unstable/workflow/DurableQueue.d.ts.map +1 -0
  177. package/dist/unstable/workflow/DurableQueue.js +182 -0
  178. package/dist/unstable/workflow/DurableQueue.js.map +1 -0
  179. package/dist/unstable/workflow/Workflow.js +1 -1
  180. package/dist/unstable/workflow/Workflow.js.map +1 -1
  181. package/dist/unstable/workflow/index.d.ts +4 -0
  182. package/dist/unstable/workflow/index.d.ts.map +1 -1
  183. package/dist/unstable/workflow/index.js +4 -0
  184. package/dist/unstable/workflow/index.js.map +1 -1
  185. package/package.json +1 -1
  186. package/src/Cause.ts +4 -4
  187. package/src/Config.ts +8 -8
  188. package/src/Context.ts +13 -14
  189. package/src/DateTime.ts +2 -2
  190. package/src/Effect.ts +340 -431
  191. package/src/Effectable.ts +2 -2
  192. package/src/ExecutionPlan.ts +3 -4
  193. package/src/FiberHandle.ts +2 -2
  194. package/src/FiberMap.ts +2 -2
  195. package/src/FiberSet.ts +1 -1
  196. package/src/Layer.ts +1 -1
  197. package/src/LayerMap.ts +3 -4
  198. package/src/ManagedRuntime.ts +1 -1
  199. package/src/Option.ts +14 -7
  200. package/src/Result.ts +14 -3
  201. package/src/Schedule.ts +1 -1
  202. package/src/Schema.ts +26 -24
  203. package/src/SchemaGetter.ts +3 -3
  204. package/src/Stream.ts +4 -4
  205. package/src/TxRef.ts +1 -1
  206. package/src/internal/concurrency.ts +2 -2
  207. package/src/internal/core.ts +12 -23
  208. package/src/internal/effect.ts +25 -29
  209. package/src/internal/executionPlan.ts +2 -2
  210. package/src/internal/option.ts +5 -8
  211. package/src/internal/result.ts +5 -8
  212. package/src/unstable/ai/LanguageModel.ts +12 -19
  213. package/src/unstable/ai/McpServer.ts +2 -2
  214. package/src/unstable/ai/Model.ts +15 -12
  215. package/src/unstable/ai/Toolkit.ts +30 -38
  216. package/src/unstable/cli/Command.ts +32 -13
  217. package/src/unstable/cli/GlobalFlag.ts +1 -1
  218. package/src/unstable/cli/Param.ts +1 -1
  219. package/src/unstable/cli/Prompt.ts +9 -13
  220. package/src/unstable/cli/internal/command.ts +12 -14
  221. package/src/unstable/cluster/ClusterWorkflowEngine.ts +1 -1
  222. package/src/unstable/cluster/Entity.ts +3 -3
  223. package/src/unstable/cluster/MessageStorage.ts +1 -1
  224. package/src/unstable/cluster/ShardingConfig.ts +1 -1
  225. package/src/unstable/eventlog/SqlEventLogServerUnencrypted.ts +4 -1
  226. package/src/unstable/http/HttpBody.ts +2 -2
  227. package/src/unstable/http/HttpClient.ts +1 -1
  228. package/src/unstable/http/HttpClientRequest.ts +2 -2
  229. package/src/unstable/http/HttpMiddleware.ts +4 -5
  230. package/src/unstable/http/HttpPlatform.ts +1 -1
  231. package/src/unstable/http/HttpRouter.ts +4 -5
  232. package/src/unstable/http/HttpServer.ts +1 -1
  233. package/src/unstable/http/HttpServerRequest.ts +11 -11
  234. package/src/unstable/http/HttpServerResponse.ts +5 -5
  235. package/src/unstable/http/Multipart.ts +1 -1
  236. package/src/unstable/httpapi/HttpApiBuilder.ts +4 -4
  237. package/src/unstable/httpapi/HttpApiClient.ts +1 -1
  238. package/src/unstable/httpapi/HttpApiMiddleware.ts +2 -1
  239. package/src/unstable/httpapi/HttpApiTest.ts +5 -2
  240. package/src/unstable/persistence/RateLimiter.ts +1 -1
  241. package/src/unstable/process/ChildProcess.ts +11 -14
  242. package/src/unstable/reactivity/Atom.ts +4 -4
  243. package/src/unstable/rpc/RpcClient.ts +1 -1
  244. package/src/unstable/rpc/RpcServer.ts +1 -2
  245. package/src/unstable/rpc/RpcWorker.ts +2 -2
  246. package/src/unstable/socket/Socket.ts +1 -1
  247. package/src/unstable/sql/Migrator.ts +5 -2
  248. package/src/unstable/sql/SqlError.ts +72 -1
  249. package/src/unstable/workflow/Activity.ts +1 -1
  250. package/src/unstable/workflow/DurableQueue.ts +343 -0
  251. package/src/unstable/workflow/Workflow.ts +1 -1
  252. package/src/unstable/workflow/index.ts +5 -0
@@ -0,0 +1,343 @@
1
+ /**
2
+ * @since 4.0.0
3
+ */
4
+ import * as Effect from "../../Effect.ts"
5
+ import * as Layer from "../../Layer.ts"
6
+ import * as Schedule from "../../Schedule.ts"
7
+ import * as Schema from "../../Schema.ts"
8
+ import * as Tracer from "../../Tracer.ts"
9
+ import * as PersistedQueue from "../persistence/PersistedQueue.ts"
10
+ import * as Activity from "./Activity.ts"
11
+ import * as DurableDeferred from "./DurableDeferred.ts"
12
+ import type { WorkflowEngine, WorkflowInstance } from "./WorkflowEngine.ts"
13
+
14
+ /**
15
+ * @since 4.0.0
16
+ * @category Type IDs
17
+ */
18
+ export type TypeId = "~effect/workflow/DurableQueue"
19
+
20
+ /**
21
+ * @since 4.0.0
22
+ * @category Type IDs
23
+ */
24
+ export const TypeId: TypeId = "~effect/workflow/DurableQueue"
25
+
26
+ /**
27
+ * @since 4.0.0
28
+ * @category Models
29
+ */
30
+ export interface DurableQueue<
31
+ Payload extends Schema.Top,
32
+ Success extends Schema.Top = Schema.Void,
33
+ Error extends Schema.Top = Schema.Never
34
+ > {
35
+ readonly [TypeId]: TypeId
36
+ readonly name: string
37
+ readonly payloadSchema: Payload
38
+ readonly idempotencyKey: (payload: Payload["Type"]) => string
39
+ readonly deferred: DurableDeferred.DurableDeferred<Success, Error>
40
+ }
41
+
42
+ /**
43
+ * A `DurableQueue` wraps a `PersistedQueue`, providing a way to wait for items
44
+ * to finish processing using a `DurableDeferred`.
45
+ *
46
+ * ```ts
47
+ * import { DurableQueue, Workflow } from "effect/unstable/workflow"
48
+ * import { Effect, Schema } from "effect"
49
+ *
50
+ * // Define a DurableQueue that can be used to derive workers and offer items for
51
+ * // processing.
52
+ * const ApiQueue = DurableQueue.make({
53
+ * name: "ApiQueue",
54
+ * payload: {
55
+ * id: Schema.String
56
+ * },
57
+ * success: Schema.Void,
58
+ * error: Schema.Never,
59
+ * idempotencyKey(payload) {
60
+ * return payload.id
61
+ * }
62
+ * })
63
+ *
64
+ * const MyWorkflow = Workflow.make({
65
+ * name: "MyWorkflow",
66
+ * payload: {
67
+ * id: Schema.String
68
+ * },
69
+ * idempotencyKey: ({ id }) => id
70
+ * })
71
+ *
72
+ * const MyWorkflowLayer = MyWorkflow.toLayer(
73
+ * Effect.fnUntraced(function*() {
74
+ * // Add an item to the DurableQueue defined above.
75
+ * //
76
+ * // When the worker has finished processing the item, the workflow will
77
+ * // resume.
78
+ * //
79
+ * yield* DurableQueue.process(ApiQueue, { id: "api-call-1" })
80
+ *
81
+ * yield* Effect.log("Workflow succeeded!")
82
+ * })
83
+ * )
84
+ *
85
+ * // Define a worker layer that can process items from the DurableQueue.
86
+ * const ApiWorker = DurableQueue.worker(
87
+ * ApiQueue,
88
+ * Effect.fnUntraced(function*({ id }) {
89
+ * yield* Effect.log(`Worker processing API call with id: ${id}`)
90
+ * }),
91
+ * { concurrency: 5 } // Process up to 5 items concurrently
92
+ * )
93
+ * ```
94
+ *
95
+ * @since 4.0.0
96
+ * @category Constructors
97
+ */
98
+ export const make = <
99
+ Payload extends Schema.Top | Schema.Struct.Fields,
100
+ Success extends Schema.Top = Schema.Void,
101
+ Error extends Schema.Top = Schema.Never
102
+ >(options: {
103
+ readonly name: string
104
+ readonly payload: Payload
105
+ readonly idempotencyKey: (
106
+ payload: Payload extends Schema.Struct.Fields ? Schema.Struct.Type<Payload>
107
+ : Payload["Type"]
108
+ ) => string
109
+ readonly success?: Success | undefined
110
+ readonly error?: Error | undefined
111
+ }): DurableQueue<
112
+ Payload extends Schema.Struct.Fields ? Schema.Struct<Payload> : Payload,
113
+ Success,
114
+ Error
115
+ > => ({
116
+ [TypeId]: TypeId,
117
+ name: options.name,
118
+ payloadSchema: Schema.isSchema(options.payload)
119
+ ? options.payload
120
+ : Schema.Struct(options.payload as any) as any,
121
+ idempotencyKey: options.idempotencyKey as any,
122
+ deferred: DurableDeferred.make(`DurableQueue/${options.name}`, {
123
+ success: options.success,
124
+ error: options.error
125
+ })
126
+ })
127
+
128
+ const queueSchemas = new WeakMap<Schema.Top, Schema.Top>()
129
+
130
+ const getQueueSchema = <Payload extends Schema.Top>(
131
+ payload: Payload
132
+ ): Schema.Struct<{
133
+ token: typeof DurableDeferred.Token
134
+ payload: Payload
135
+ traceId: typeof Schema.String
136
+ spanId: typeof Schema.String
137
+ sampled: typeof Schema.Boolean
138
+ }> => {
139
+ let schema = queueSchemas.get(payload)
140
+ if (!schema) {
141
+ schema = Schema.Struct({
142
+ token: DurableDeferred.Token,
143
+ traceId: Schema.String,
144
+ spanId: Schema.String,
145
+ sampled: Schema.Boolean,
146
+ payload
147
+ })
148
+ queueSchemas.set(payload, schema)
149
+ }
150
+ return schema as any
151
+ }
152
+
153
+ /**
154
+ * Add an item to the queue and wait for a worker to process it.
155
+ *
156
+ * @since 4.0.0
157
+ * @category Processing
158
+ */
159
+ export const process: <
160
+ Payload extends Schema.Top,
161
+ Success extends Schema.Top,
162
+ Error extends Schema.Top
163
+ >(
164
+ self: DurableQueue<Payload, Success, Error>,
165
+ payload: Payload["~type.make.in"],
166
+ options?: {
167
+ readonly retrySchedule?: Schedule.Schedule<any, PersistedQueue.PersistedQueueError> | undefined
168
+ }
169
+ ) => Effect.Effect<
170
+ Success["Type"],
171
+ Error["Type"],
172
+ | WorkflowEngine
173
+ | WorkflowInstance
174
+ | PersistedQueue.PersistedQueueFactory
175
+ | Payload["EncodingServices"]
176
+ | Payload["DecodingServices"]
177
+ | Success["DecodingServices"]
178
+ | Error["DecodingServices"]
179
+ > = Effect.fnUntraced(function*<
180
+ Payload extends Schema.Top,
181
+ Success extends Schema.Top,
182
+ Error extends Schema.Top
183
+ >(self: DurableQueue<Payload, Success, Error>, fields: Payload["~type.make.in"], options?: {
184
+ readonly retrySchedule?: Schedule.Schedule<any, PersistedQueue.PersistedQueueError> | undefined
185
+ }) {
186
+ const payload = self.payloadSchema.make(fields)
187
+ const queueName = `DurableQueue/${self.name}`
188
+ const queue = yield* PersistedQueue.make({
189
+ name: queueName,
190
+ schema: getQueueSchema(self.payloadSchema)
191
+ })
192
+ const id = yield* Activity.idempotencyKey(`${queueName}/${self.idempotencyKey(payload)}`)
193
+
194
+ const deferred = DurableDeferred.make(`${self.deferred.name}/${id}`, {
195
+ success: self.deferred.successSchema,
196
+ error: self.deferred.errorSchema
197
+ })
198
+ const token = yield* DurableDeferred.token(deferred)
199
+
200
+ yield* Effect.useSpan(`DurableQueue/${self.name}/process`, {
201
+ attributes: { id }
202
+ }, (span) =>
203
+ queue.offer({
204
+ token,
205
+ payload,
206
+ traceId: span.traceId,
207
+ spanId: span.spanId,
208
+ sampled: span.sampled
209
+ } as any, { id }).pipe(
210
+ Effect.tapCause(Effect.logWarning),
211
+ Effect.catchTag("SchemaError", Effect.die),
212
+ Effect.retry(options?.retrySchedule ?? defaultRetrySchedule),
213
+ Effect.orDie,
214
+ Effect.annotateLogs({
215
+ package: "effect",
216
+ module: "DurableQueue",
217
+ fiber: "process",
218
+ queueName: self.name
219
+ })
220
+ ))
221
+
222
+ return yield* DurableDeferred.await(deferred)
223
+ })
224
+
225
+ const defaultRetrySchedule = Schedule.exponential(500, 1.5).pipe(
226
+ Schedule.either(Schedule.spaced("1 minute"))
227
+ )
228
+
229
+ /**
230
+ * Create a worker effect that processes items from the durable queue.
231
+ *
232
+ * @since 4.0.0
233
+ * @category Worker
234
+ */
235
+ export const makeWorker: <
236
+ Payload extends Schema.Top,
237
+ Success extends Schema.Top,
238
+ Error extends Schema.Top,
239
+ R
240
+ >(
241
+ self: DurableQueue<Payload, Success, Error>,
242
+ f: (payload: Payload["Type"]) => Effect.Effect<Success["Type"], Error["Type"], R>,
243
+ options?: { readonly concurrency?: number | undefined } | undefined
244
+ ) => Effect.Effect<
245
+ never,
246
+ never,
247
+ | WorkflowEngine
248
+ | PersistedQueue.PersistedQueueFactory
249
+ | R
250
+ | Payload["EncodingServices"]
251
+ | Payload["DecodingServices"]
252
+ | Success["EncodingServices"]
253
+ | Error["EncodingServices"]
254
+ > = Effect.fnUntraced(function*<
255
+ Payload extends Schema.Top,
256
+ Success extends Schema.Top,
257
+ Error extends Schema.Top,
258
+ R
259
+ >(
260
+ self: DurableQueue<Payload, Success, Error>,
261
+ f: (payload: Payload["Type"]) => Effect.Effect<Success["Type"], Error["Type"], R>,
262
+ options?: {
263
+ readonly concurrency?: number | undefined
264
+ }
265
+ ) {
266
+ const queue = yield* PersistedQueue.make({
267
+ name: `DurableQueue/${self.name}`,
268
+ schema: getQueueSchema(self.payloadSchema)
269
+ })
270
+ const concurrency = options?.concurrency ?? 1
271
+
272
+ const worker = queue.take((item_) => {
273
+ const item = item_ as {
274
+ readonly token: DurableDeferred.Token
275
+ readonly payload: Payload["Type"]
276
+ readonly traceId: string
277
+ readonly spanId: string
278
+ readonly sampled: boolean
279
+ }
280
+ return Effect.withSpan(
281
+ f(item.payload).pipe(
282
+ Effect.exit,
283
+ Effect.flatMap((exit) =>
284
+ DurableDeferred.done(self.deferred, {
285
+ token: item.token,
286
+ exit
287
+ })
288
+ ),
289
+ Effect.asVoid
290
+ ),
291
+ `DurableQueue/${self.name}/worker`,
292
+ {
293
+ captureStackTrace: false,
294
+ parent: Tracer.externalSpan({
295
+ traceId: item.traceId,
296
+ spanId: item.spanId,
297
+ sampled: item.sampled
298
+ })
299
+ }
300
+ )
301
+ }).pipe(
302
+ Effect.catchCause(Effect.logWarning),
303
+ Effect.forever,
304
+ Effect.annotateLogs({
305
+ package: "effect",
306
+ module: "DurableQueue",
307
+ fiber: "worker",
308
+ queueName: self.name
309
+ })
310
+ )
311
+
312
+ yield* Effect.replicateEffect(worker, concurrency, { concurrency, discard: true })
313
+ return yield* Effect.never
314
+ })
315
+
316
+ /**
317
+ * Create a layer that runs workers for the durable queue.
318
+ *
319
+ * @since 4.0.0
320
+ * @category Worker
321
+ */
322
+ export const worker: <
323
+ Payload extends Schema.Top,
324
+ Success extends Schema.Top,
325
+ Error extends Schema.Top,
326
+ R
327
+ >(
328
+ self: DurableQueue<Payload, Success, Error>,
329
+ f: (payload: Payload["Type"]) => Effect.Effect<Success["Type"], Error["Type"], R>,
330
+ options?: {
331
+ readonly concurrency?: number | undefined
332
+ } | undefined
333
+ ) => Layer.Layer<
334
+ never,
335
+ never,
336
+ | WorkflowEngine
337
+ | PersistedQueue.PersistedQueueFactory
338
+ | R
339
+ | Payload["EncodingServices"]
340
+ | Payload["DecodingServices"]
341
+ | Success["EncodingServices"]
342
+ | Error["EncodingServices"]
343
+ > = (self, f, options) => Layer.effectDiscard(Effect.forkScoped(makeWorker(self, f, options)))
@@ -669,7 +669,7 @@ export const scope: Effect.Effect<
669
669
  never,
670
670
  WorkflowInstance
671
671
  > = Effect.map(
672
- InstanceTag.asEffect(),
672
+ InstanceTag,
673
673
  (instance) => instance.scope as Scope.Scope
674
674
  )
675
675
 
@@ -19,6 +19,11 @@ export * as DurableClock from "./DurableClock.ts"
19
19
  */
20
20
  export * as DurableDeferred from "./DurableDeferred.ts"
21
21
 
22
+ /**
23
+ * @since 4.0.0
24
+ */
25
+ export * as DurableQueue from "./DurableQueue.ts"
26
+
22
27
  /**
23
28
  * @since 4.0.0
24
29
  */