-
-
Notifications
You must be signed in to change notification settings - Fork 568
Expand file tree
/
Copy pathWorkflowProxyServer.ts
More file actions
157 lines (154 loc) · 5.02 KB
/
WorkflowProxyServer.ts
File metadata and controls
157 lines (154 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* @since 1.0.0
*/
import type * as HttpApi from "@effect/platform/HttpApi"
import * as HttpApiBuilder from "@effect/platform/HttpApiBuilder"
import type { ApiGroup, HttpApiGroup } from "@effect/platform/HttpApiGroup"
import type * as Rpc from "@effect/rpc/Rpc"
import type { NonEmptyReadonlyArray } from "effect/Array"
import * as Context from "effect/Context"
import * as Effect from "effect/Effect"
import * as Layer from "effect/Layer"
import type * as Workflow from "./Workflow.js"
import type { WorkflowEngine } from "./WorkflowEngine.js"
/**
* @since 1.0.0
* @category Layers
*/
export const layerHttpApi = <
ApiId extends string,
Groups extends HttpApiGroup.Any,
ApiE,
ApiR,
Name extends HttpApiGroup.Name<Groups>,
const Workflows extends NonEmptyReadonlyArray<Workflow.Any>
>(
api: HttpApi.HttpApi<ApiId, Groups, ApiE, ApiR>,
name: Name,
workflows: Workflows
): Layer.Layer<
ApiGroup<ApiId, Name>,
never,
WorkflowEngine | Workflow.Requirements<Workflows[number]>
> =>
HttpApiBuilder.group(
api,
name,
Effect.fnUntraced(function*(handlers_) {
let handlers = handlers_ as any
for (const workflow_ of workflows) {
const workflow = workflow_ as Workflow.Workflow<string, any, any, any>
handlers = handlers
.handle(
workflow.name as any,
({ payload }: { payload: any }) =>
workflow.execute(payload).pipe(
Effect.tapDefect(Effect.logError),
Effect.annotateLogs({
module: "WorkflowProxyServer",
method: workflow.name
})
)
)
.handle(
workflow.name + "Discard" as any,
({ payload }: { payload: any }) =>
workflow.execute(payload, { discard: true } as any).pipe(
Effect.tapDefect(Effect.logError),
Effect.annotateLogs({
module: "WorkflowProxyServer",
method: workflow.name + "Discard"
})
)
)
.handle(
workflow.name + "Resume" as any,
({ payload }: { payload: any }) =>
workflow.resume(payload.executionId).pipe(
Effect.tapDefect(Effect.logError),
Effect.annotateLogs({
module: "WorkflowProxyServer",
method: workflow.name + "Resume"
})
)
)
.handle(
workflow.name + "Interrupt" as any,
({ payload }: { payload: any }) =>
workflow.interrupt(payload.executionId).pipe(
Effect.tapDefect(Effect.logError),
Effect.annotateLogs({
module: "WorkflowProxyServer",
method: workflow.name + "Interrupt"
})
)
)
}
return handlers as HttpApiBuilder.Handlers<never, never, never>
})
)
/**
* @since 1.0.0
* @category Layers
*/
export const layerRpcHandlers = <
const Workflows extends NonEmptyReadonlyArray<Workflow.Any>,
const Prefix extends string = ""
>(workflows: Workflows, options?: {
readonly prefix?: Prefix
}): Layer.Layer<
RpcHandlers<Workflows[number], Prefix>,
never,
WorkflowEngine | Workflow.Requirements<Workflows[number]>
> =>
Layer.effectContext(Effect.gen(function*() {
const context = yield* Effect.context<never>()
const prefix = options?.prefix ?? ""
const handlers = new Map<string, Rpc.Handler<string>>()
for (const workflow_ of workflows) {
const workflow = workflow_ as Workflow.Workflow<string, any, any, any>
const tag = `${prefix}${workflow.name}`
const tagDiscard = `${tag}Discard`
const tagResume = `${tag}Resume`
const tagInterrupt = `${tag}Interrupt`
const key = `@effect/rpc/Rpc/${tag}`
const keyDiscard = `${key}Discard`
const keyResume = `${key}Resume`
const keyInterrupt = `${key}Interrupt`
handlers.set(key, {
context,
tag,
handler: (payload: any) => workflow.execute(payload) as any
} as any)
handlers.set(keyDiscard, {
context,
tag: tagDiscard,
handler: (payload: any) => workflow.execute(payload, { discard: true } as any) as any
} as any)
handlers.set(keyResume, {
context,
tag: tagResume,
handler: (payload: any) => workflow.resume(payload.executionId) as any
} as any)
handlers.set(keyInterrupt, {
context,
tag: tagInterrupt,
handler: (payload: any) => workflow.interrupt(payload.executionId) as any
} as any)
}
return Context.unsafeMake(handlers)
}))
/**
* @since 1.0.0
*/
export type RpcHandlers<Workflows extends Workflow.Any, Prefix extends string> = Workflows extends Workflow.Workflow<
infer _Name,
infer _Payload,
infer _Success,
infer _Error
> ?
| Rpc.Handler<`${Prefix}${_Name}`>
| Rpc.Handler<`${Prefix}${_Name}Discard`>
| Rpc.Handler<`${Prefix}${_Name}Resume`>
| Rpc.Handler<`${Prefix}${_Name}Interrupt`>
: never