effect 4.0.0-beta.65 → 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.
- package/dist/Cause.d.ts +4 -4
- package/dist/Cause.d.ts.map +1 -1
- package/dist/Config.d.ts +1 -2
- package/dist/Config.d.ts.map +1 -1
- package/dist/Config.js +7 -6
- package/dist/Config.js.map +1 -1
- package/dist/Context.d.ts +3 -4
- package/dist/Context.d.ts.map +1 -1
- package/dist/Context.js +10 -8
- package/dist/Context.js.map +1 -1
- package/dist/DateTime.d.ts +1 -1
- package/dist/DateTime.d.ts.map +1 -1
- package/dist/DateTime.js +2 -2
- package/dist/DateTime.js.map +1 -1
- package/dist/Effect.d.ts +337 -412
- package/dist/Effect.d.ts.map +1 -1
- package/dist/Effect.js +5 -32
- package/dist/Effect.js.map +1 -1
- package/dist/Effectable.d.ts +1 -1
- package/dist/Effectable.d.ts.map +1 -1
- package/dist/Effectable.js +1 -1
- package/dist/Effectable.js.map +1 -1
- package/dist/ExecutionPlan.d.ts +2 -3
- package/dist/ExecutionPlan.d.ts.map +1 -1
- package/dist/ExecutionPlan.js +1 -1
- package/dist/ExecutionPlan.js.map +1 -1
- package/dist/FiberHandle.d.ts +2 -2
- package/dist/FiberHandle.js +2 -2
- package/dist/FiberMap.d.ts +2 -2
- package/dist/FiberMap.js +2 -2
- package/dist/FiberSet.d.ts +1 -1
- package/dist/FiberSet.js +1 -1
- package/dist/Layer.js +1 -1
- package/dist/Layer.js.map +1 -1
- package/dist/LayerMap.d.ts.map +1 -1
- package/dist/LayerMap.js +3 -3
- package/dist/LayerMap.js.map +1 -1
- package/dist/ManagedRuntime.d.ts +1 -1
- package/dist/ManagedRuntime.js +1 -1
- package/dist/Option.d.ts +11 -7
- package/dist/Option.d.ts.map +1 -1
- package/dist/Option.js +74 -0
- package/dist/Option.js.map +1 -1
- package/dist/Result.d.ts +11 -3
- package/dist/Result.d.ts.map +1 -1
- package/dist/Result.js.map +1 -1
- package/dist/Schedule.js +1 -1
- package/dist/Schedule.js.map +1 -1
- package/dist/Schema.d.ts +12 -12
- package/dist/Schema.d.ts.map +1 -1
- package/dist/Schema.js +6 -4
- package/dist/Schema.js.map +1 -1
- package/dist/SchemaGetter.js +2 -2
- package/dist/SchemaGetter.js.map +1 -1
- package/dist/SchemaRepresentation.d.ts +2 -2
- package/dist/Stream.d.ts +3 -3
- package/dist/Stream.js +1 -1
- package/dist/TxRef.js +1 -1
- package/dist/TxRef.js.map +1 -1
- package/dist/internal/concurrency.js +2 -2
- package/dist/internal/concurrency.js.map +1 -1
- package/dist/internal/core.js +8 -20
- package/dist/internal/core.js.map +1 -1
- package/dist/internal/effect.js +20 -19
- package/dist/internal/effect.js.map +1 -1
- package/dist/internal/executionPlan.js +2 -2
- package/dist/internal/executionPlan.js.map +1 -1
- package/dist/internal/option.js +5 -8
- package/dist/internal/option.js.map +1 -1
- package/dist/internal/result.js +5 -8
- package/dist/internal/result.js.map +1 -1
- package/dist/unstable/ai/LanguageModel.d.ts +5 -5
- package/dist/unstable/ai/LanguageModel.d.ts.map +1 -1
- package/dist/unstable/ai/LanguageModel.js +1 -1
- package/dist/unstable/ai/LanguageModel.js.map +1 -1
- package/dist/unstable/ai/McpServer.js +2 -2
- package/dist/unstable/ai/McpServer.js.map +1 -1
- package/dist/unstable/ai/Model.d.ts +5 -1
- package/dist/unstable/ai/Model.d.ts.map +1 -1
- package/dist/unstable/ai/Model.js +4 -4
- package/dist/unstable/ai/Model.js.map +1 -1
- package/dist/unstable/ai/Toolkit.d.ts +1 -3
- package/dist/unstable/ai/Toolkit.d.ts.map +1 -1
- package/dist/unstable/ai/Toolkit.js +28 -31
- package/dist/unstable/ai/Toolkit.js.map +1 -1
- package/dist/unstable/cli/Command.d.ts +25 -5
- package/dist/unstable/cli/Command.d.ts.map +1 -1
- package/dist/unstable/cli/Command.js.map +1 -1
- package/dist/unstable/cli/GlobalFlag.d.ts +1 -1
- package/dist/unstable/cli/GlobalFlag.d.ts.map +1 -1
- package/dist/unstable/cli/Param.js +1 -1
- package/dist/unstable/cli/Param.js.map +1 -1
- package/dist/unstable/cli/Prompt.d.ts +1 -2
- package/dist/unstable/cli/Prompt.d.ts.map +1 -1
- package/dist/unstable/cli/Prompt.js +8 -10
- package/dist/unstable/cli/Prompt.js.map +1 -1
- package/dist/unstable/cli/internal/command.d.ts +2 -6
- package/dist/unstable/cli/internal/command.d.ts.map +1 -1
- package/dist/unstable/cli/internal/command.js +7 -9
- package/dist/unstable/cli/internal/command.js.map +1 -1
- package/dist/unstable/cluster/ClusterWorkflowEngine.js +1 -1
- package/dist/unstable/cluster/ClusterWorkflowEngine.js.map +1 -1
- package/dist/unstable/cluster/Entity.js +3 -3
- package/dist/unstable/cluster/Entity.js.map +1 -1
- package/dist/unstable/cluster/MessageStorage.js +1 -1
- package/dist/unstable/cluster/MessageStorage.js.map +1 -1
- package/dist/unstable/cluster/ShardingConfig.js +1 -1
- package/dist/unstable/cluster/ShardingConfig.js.map +1 -1
- package/dist/unstable/eventlog/EventLogMessage.d.ts +4 -4
- package/dist/unstable/http/HttpBody.js +2 -2
- package/dist/unstable/http/HttpBody.js.map +1 -1
- package/dist/unstable/http/HttpClient.js +1 -1
- package/dist/unstable/http/HttpClient.js.map +1 -1
- package/dist/unstable/http/HttpClientRequest.js +2 -2
- package/dist/unstable/http/HttpClientRequest.js.map +1 -1
- package/dist/unstable/http/HttpMiddleware.d.ts.map +1 -1
- package/dist/unstable/http/HttpMiddleware.js +18 -21
- package/dist/unstable/http/HttpMiddleware.js.map +1 -1
- package/dist/unstable/http/HttpPlatform.js +1 -1
- package/dist/unstable/http/HttpPlatform.js.map +1 -1
- package/dist/unstable/http/HttpRouter.d.ts.map +1 -1
- package/dist/unstable/http/HttpRouter.js +4 -4
- package/dist/unstable/http/HttpRouter.js.map +1 -1
- package/dist/unstable/http/HttpServer.js +1 -1
- package/dist/unstable/http/HttpServer.js.map +1 -1
- package/dist/unstable/http/HttpServerRequest.js +11 -11
- package/dist/unstable/http/HttpServerRequest.js.map +1 -1
- package/dist/unstable/http/HttpServerResponse.d.ts.map +1 -1
- package/dist/unstable/http/HttpServerResponse.js +5 -5
- package/dist/unstable/http/HttpServerResponse.js.map +1 -1
- package/dist/unstable/http/Multipart.js +1 -1
- package/dist/unstable/http/Multipart.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiBuilder.js +3 -3
- package/dist/unstable/httpapi/HttpApiBuilder.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiClient.js +1 -1
- package/dist/unstable/httpapi/HttpApiClient.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiMiddleware.d.ts +2 -0
- package/dist/unstable/httpapi/HttpApiMiddleware.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiMiddleware.js +2 -1
- package/dist/unstable/httpapi/HttpApiMiddleware.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiTest.d.ts +3 -1
- package/dist/unstable/httpapi/HttpApiTest.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiTest.js +2 -2
- package/dist/unstable/httpapi/HttpApiTest.js.map +1 -1
- package/dist/unstable/persistence/RateLimiter.js +1 -1
- package/dist/unstable/persistence/RateLimiter.js.map +1 -1
- package/dist/unstable/process/ChildProcess.d.ts +2 -45
- package/dist/unstable/process/ChildProcess.d.ts.map +1 -1
- package/dist/unstable/process/ChildProcess.js +51 -7
- package/dist/unstable/process/ChildProcess.js.map +1 -1
- package/dist/unstable/reactivity/Atom.d.ts.map +1 -1
- package/dist/unstable/reactivity/Atom.js +4 -4
- package/dist/unstable/reactivity/Atom.js.map +1 -1
- package/dist/unstable/rpc/RpcClient.js +1 -1
- package/dist/unstable/rpc/RpcClient.js.map +1 -1
- package/dist/unstable/rpc/RpcServer.d.ts.map +1 -1
- package/dist/unstable/rpc/RpcServer.js +1 -1
- package/dist/unstable/rpc/RpcServer.js.map +1 -1
- package/dist/unstable/rpc/RpcWorker.js +1 -1
- package/dist/unstable/rpc/RpcWorker.js.map +1 -1
- package/dist/unstable/socket/Socket.d.ts.map +1 -1
- package/dist/unstable/socket/Socket.js +1 -1
- package/dist/unstable/socket/Socket.js.map +1 -1
- package/dist/unstable/sql/Migrator.js +1 -1
- package/dist/unstable/sql/Migrator.js.map +1 -1
- package/dist/unstable/workflow/Activity.js +1 -1
- package/dist/unstable/workflow/Activity.js.map +1 -1
- package/dist/unstable/workflow/DurableQueue.d.ts +122 -0
- package/dist/unstable/workflow/DurableQueue.d.ts.map +1 -0
- package/dist/unstable/workflow/DurableQueue.js +182 -0
- package/dist/unstable/workflow/DurableQueue.js.map +1 -0
- package/dist/unstable/workflow/Workflow.js +1 -1
- package/dist/unstable/workflow/Workflow.js.map +1 -1
- package/dist/unstable/workflow/index.d.ts +4 -0
- package/dist/unstable/workflow/index.d.ts.map +1 -1
- package/dist/unstable/workflow/index.js +4 -0
- package/dist/unstable/workflow/index.js.map +1 -1
- package/package.json +1 -1
- package/src/Cause.ts +4 -4
- package/src/Config.ts +8 -8
- package/src/Context.ts +13 -14
- package/src/DateTime.ts +2 -2
- package/src/Effect.ts +340 -431
- package/src/Effectable.ts +2 -2
- package/src/ExecutionPlan.ts +3 -4
- package/src/FiberHandle.ts +2 -2
- package/src/FiberMap.ts +2 -2
- package/src/FiberSet.ts +1 -1
- package/src/Layer.ts +1 -1
- package/src/LayerMap.ts +3 -4
- package/src/ManagedRuntime.ts +1 -1
- package/src/Option.ts +14 -7
- package/src/Result.ts +14 -3
- package/src/Schedule.ts +1 -1
- package/src/Schema.ts +26 -24
- package/src/SchemaGetter.ts +3 -3
- package/src/Stream.ts +4 -4
- package/src/TxRef.ts +1 -1
- package/src/internal/concurrency.ts +2 -2
- package/src/internal/core.ts +12 -23
- package/src/internal/effect.ts +25 -29
- package/src/internal/executionPlan.ts +2 -2
- package/src/internal/option.ts +5 -8
- package/src/internal/result.ts +5 -8
- package/src/unstable/ai/LanguageModel.ts +12 -19
- package/src/unstable/ai/McpServer.ts +2 -2
- package/src/unstable/ai/Model.ts +15 -12
- package/src/unstable/ai/Toolkit.ts +30 -38
- package/src/unstable/cli/Command.ts +32 -13
- package/src/unstable/cli/GlobalFlag.ts +1 -1
- package/src/unstable/cli/Param.ts +1 -1
- package/src/unstable/cli/Prompt.ts +9 -13
- package/src/unstable/cli/internal/command.ts +12 -14
- package/src/unstable/cluster/ClusterWorkflowEngine.ts +1 -1
- package/src/unstable/cluster/Entity.ts +3 -3
- package/src/unstable/cluster/MessageStorage.ts +1 -1
- package/src/unstable/cluster/ShardingConfig.ts +1 -1
- package/src/unstable/http/HttpBody.ts +2 -2
- package/src/unstable/http/HttpClient.ts +1 -1
- package/src/unstable/http/HttpClientRequest.ts +2 -2
- package/src/unstable/http/HttpMiddleware.ts +4 -5
- package/src/unstable/http/HttpPlatform.ts +1 -1
- package/src/unstable/http/HttpRouter.ts +4 -5
- package/src/unstable/http/HttpServer.ts +1 -1
- package/src/unstable/http/HttpServerRequest.ts +11 -11
- package/src/unstable/http/HttpServerResponse.ts +5 -5
- package/src/unstable/http/Multipart.ts +1 -1
- package/src/unstable/httpapi/HttpApiBuilder.ts +4 -4
- package/src/unstable/httpapi/HttpApiClient.ts +1 -1
- package/src/unstable/httpapi/HttpApiMiddleware.ts +2 -1
- package/src/unstable/httpapi/HttpApiTest.ts +5 -2
- package/src/unstable/persistence/RateLimiter.ts +1 -1
- package/src/unstable/process/ChildProcess.ts +11 -14
- package/src/unstable/reactivity/Atom.ts +4 -4
- package/src/unstable/rpc/RpcClient.ts +1 -1
- package/src/unstable/rpc/RpcServer.ts +1 -2
- package/src/unstable/rpc/RpcWorker.ts +2 -2
- package/src/unstable/socket/Socket.ts +1 -1
- package/src/unstable/sql/Migrator.ts +1 -1
- package/src/unstable/workflow/Activity.ts +1 -1
- package/src/unstable/workflow/DurableQueue.ts +343 -0
- package/src/unstable/workflow/Workflow.ts +1 -1
- 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)))
|