@livestore/utils 0.4.0-dev.1 → 0.4.0-dev.11
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/.tsbuildinfo.json +1 -1
- package/dist/NoopTracer.d.ts.map +1 -1
- package/dist/NoopTracer.js +1 -0
- package/dist/NoopTracer.js.map +1 -1
- package/dist/effect/Effect.d.ts +9 -3
- package/dist/effect/Effect.d.ts.map +1 -1
- package/dist/effect/Effect.js +4 -2
- package/dist/effect/Effect.js.map +1 -1
- package/dist/effect/Error.d.ts +1 -1
- package/dist/effect/Error.js.map +1 -1
- package/dist/effect/Logger.d.ts +4 -1
- package/dist/effect/Logger.d.ts.map +1 -1
- package/dist/effect/Logger.js +12 -3
- package/dist/effect/Logger.js.map +1 -1
- package/dist/effect/OtelTracer.d.ts +5 -0
- package/dist/effect/OtelTracer.d.ts.map +1 -0
- package/dist/effect/OtelTracer.js +8 -0
- package/dist/effect/OtelTracer.js.map +1 -0
- package/dist/effect/RpcClient.d.ts +32 -0
- package/dist/effect/RpcClient.d.ts.map +1 -0
- package/dist/effect/RpcClient.js +149 -0
- package/dist/effect/RpcClient.js.map +1 -0
- package/dist/effect/Schema/index.d.ts +2 -2
- package/dist/effect/Schema/index.d.ts.map +1 -1
- package/dist/effect/Schema/index.js +12 -2
- package/dist/effect/Schema/index.js.map +1 -1
- package/dist/effect/Stream.d.ts +73 -2
- package/dist/effect/Stream.d.ts.map +1 -1
- package/dist/effect/Stream.js +68 -1
- package/dist/effect/Stream.js.map +1 -1
- package/dist/effect/Stream.test.d.ts +2 -0
- package/dist/effect/Stream.test.d.ts.map +1 -0
- package/dist/effect/Stream.test.js +84 -0
- package/dist/effect/Stream.test.js.map +1 -0
- package/dist/effect/SubscriptionRef.d.ts +2 -2
- package/dist/effect/SubscriptionRef.d.ts.map +1 -1
- package/dist/effect/SubscriptionRef.js +6 -1
- package/dist/effect/SubscriptionRef.js.map +1 -1
- package/dist/effect/WebChannel/common.d.ts +1 -1
- package/dist/effect/WebChannel/common.d.ts.map +1 -1
- package/dist/effect/WebSocket.js +1 -1
- package/dist/effect/WebSocket.js.map +1 -1
- package/dist/effect/index.d.ts +17 -11
- package/dist/effect/index.d.ts.map +1 -1
- package/dist/effect/index.js +20 -15
- package/dist/effect/index.js.map +1 -1
- package/dist/global.d.ts +1 -0
- package/dist/global.d.ts.map +1 -1
- package/dist/global.js.map +1 -1
- package/dist/misc.js +1 -1
- package/dist/misc.js.map +1 -1
- package/dist/mod.d.ts +2 -0
- package/dist/mod.d.ts.map +1 -1
- package/dist/mod.js +4 -0
- package/dist/mod.js.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunner.d.ts.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunner.js +66 -10
- package/dist/node/ChildProcessRunner/ChildProcessRunner.js.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/ChildProcessRunner.test.js +177 -3
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/ChildProcessRunner.test.js.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/schema.d.ts +10 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/schema.d.ts.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/schema.js +7 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/schema.js.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/serializedWorker.js +13 -3
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/serializedWorker.js.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessWorker.d.ts +16 -0
- package/dist/node/ChildProcessRunner/ChildProcessWorker.d.ts.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessWorker.js +98 -2
- package/dist/node/ChildProcessRunner/ChildProcessWorker.js.map +1 -1
- package/dist/node/mod.d.ts +7 -1
- package/dist/node/mod.d.ts.map +1 -1
- package/dist/node/mod.js +10 -2
- package/dist/node/mod.js.map +1 -1
- package/package.json +42 -41
- package/src/NoopTracer.ts +1 -0
- package/src/effect/Effect.ts +31 -4
- package/src/effect/Error.ts +1 -1
- package/src/effect/Logger.ts +14 -4
- package/src/effect/OtelTracer.ts +11 -0
- package/src/effect/RpcClient.ts +212 -0
- package/src/effect/Schema/index.ts +17 -3
- package/src/effect/Stream.test.ts +127 -0
- package/src/effect/Stream.ts +111 -2
- package/src/effect/SubscriptionRef.ts +14 -2
- package/src/effect/WebChannel/common.ts +1 -1
- package/src/effect/WebSocket.ts +1 -1
- package/src/effect/index.ts +40 -14
- package/src/global.ts +1 -0
- package/src/misc.ts +1 -1
- package/src/mod.ts +9 -0
- package/src/node/ChildProcessRunner/ChildProcessRunner.ts +71 -10
- package/src/node/ChildProcessRunner/ChildProcessRunnerTest/ChildProcessRunner.test.ts +258 -3
- package/src/node/ChildProcessRunner/ChildProcessRunnerTest/schema.ts +14 -1
- package/src/node/ChildProcessRunner/ChildProcessRunnerTest/serializedWorker.ts +16 -3
- package/src/node/ChildProcessRunner/ChildProcessWorker.ts +111 -3
- package/src/node/mod.ts +12 -5
- package/dist/effect/Schema/msgpack.d.ts +0 -3
- package/dist/effect/Schema/msgpack.d.ts.map +0 -1
- package/dist/effect/Schema/msgpack.js +0 -7
- package/dist/effect/Schema/msgpack.js.map +0 -1
- package/src/effect/Schema/msgpack.ts +0 -8
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
// import * as WorkerThreads from 'node:worker_threads'
|
|
2
1
|
import * as ChildProcess from 'node:child_process'
|
|
3
2
|
|
|
4
3
|
import * as EffectWorker from '@effect/platform/Worker'
|
|
5
4
|
import { assert, describe, it } from '@effect/vitest'
|
|
6
|
-
import { Chunk, Effect, Stream } from 'effect'
|
|
5
|
+
import { Chunk, Deferred, Effect, Exit, Fiber, Scope, Stream } from 'effect'
|
|
7
6
|
|
|
8
7
|
import * as ChildProcessWorker from '../ChildProcessWorker.ts'
|
|
9
8
|
import type { WorkerMessage } from './schema.ts'
|
|
10
|
-
import { GetPersonById, GetUserById, InitialMessage, Person, User } from './schema.ts'
|
|
9
|
+
import { GetPersonById, GetUserById, InitialMessage, Person, StartStubbornWorker, User } from './schema.ts'
|
|
11
10
|
|
|
12
11
|
const WorkerLive = ChildProcessWorker.layer(() =>
|
|
13
12
|
ChildProcess.fork(
|
|
@@ -49,4 +48,260 @@ describe('ChildProcessRunner', { timeout: 10_000 }, () => {
|
|
|
49
48
|
new Person({ id: 123, name: 'ing', data: new Uint8Array([4, 5, 6]) }),
|
|
50
49
|
])
|
|
51
50
|
}).pipe(Effect.scoped, Effect.provide(WorkerLive), Effect.runPromise))
|
|
51
|
+
|
|
52
|
+
describe('Process Cleanup', { timeout: 15_000 }, () => {
|
|
53
|
+
const isProcessRunning = (pid: number) => {
|
|
54
|
+
try {
|
|
55
|
+
process.kill(pid, 0) // Signal 0 checks if process exists
|
|
56
|
+
return true
|
|
57
|
+
} catch {
|
|
58
|
+
return false
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
it('should clean up child processes when Effect is interrupted', () =>
|
|
63
|
+
Effect.gen(function* () {
|
|
64
|
+
const workerPidDeferred = yield* Deferred.make<number>()
|
|
65
|
+
|
|
66
|
+
const testEffect = Effect.gen(function* () {
|
|
67
|
+
const pool = yield* EffectWorker.makePoolSerialized<WorkerMessage>({
|
|
68
|
+
size: 1,
|
|
69
|
+
initialMessage: () => new InitialMessage({ name: 'test', data: new Uint8Array([1, 2, 3]) }),
|
|
70
|
+
})
|
|
71
|
+
const result = yield* pool.executeEffect(new StartStubbornWorker({ blockDuration: 30_000 }))
|
|
72
|
+
yield* Deferred.succeed(workerPidDeferred, result.pid)
|
|
73
|
+
|
|
74
|
+
// Verify the worker process is running
|
|
75
|
+
assert.strictEqual(isProcessRunning(result.pid), true, 'Worker process should be running')
|
|
76
|
+
|
|
77
|
+
// Start a long-running operation that we'll interrupt
|
|
78
|
+
yield* Effect.sleep('60 seconds')
|
|
79
|
+
}).pipe(Effect.scoped, Effect.provide(WorkerLive))
|
|
80
|
+
|
|
81
|
+
// Run the test effect but interrupt it after 2 seconds
|
|
82
|
+
const fiber = yield* Effect.forkScoped(testEffect)
|
|
83
|
+
|
|
84
|
+
const workerPid = yield* Deferred.await(workerPidDeferred).pipe(
|
|
85
|
+
Effect.raceFirst(
|
|
86
|
+
Fiber.join(fiber).pipe(
|
|
87
|
+
Effect.flatMap(() => Effect.fail(new Error('testEffect completed before reporting worker PID'))),
|
|
88
|
+
),
|
|
89
|
+
),
|
|
90
|
+
Effect.timeout(10_000),
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
yield* Effect.sleep('2 seconds')
|
|
94
|
+
yield* Fiber.interrupt(fiber)
|
|
95
|
+
|
|
96
|
+
// Wait a moment for cleanup to complete
|
|
97
|
+
yield* Effect.sleep('1 second')
|
|
98
|
+
|
|
99
|
+
assert.strictEqual(
|
|
100
|
+
isProcessRunning(workerPid),
|
|
101
|
+
false,
|
|
102
|
+
`Worker process ${workerPid} should be terminated after Effect interruption`,
|
|
103
|
+
)
|
|
104
|
+
}).pipe(Effect.scoped, Effect.runPromise))
|
|
105
|
+
|
|
106
|
+
it('should clean up child processes when scope is closed abruptly', () =>
|
|
107
|
+
Effect.gen(function* () {
|
|
108
|
+
let workerPid: number | undefined
|
|
109
|
+
|
|
110
|
+
// Create a scope that we can close manually
|
|
111
|
+
const scope = yield* Scope.make()
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
const pool = yield* EffectWorker.makePoolSerialized<WorkerMessage>({
|
|
115
|
+
size: 1,
|
|
116
|
+
initialMessage: () => new InitialMessage({ name: 'test', data: new Uint8Array([1, 2, 3]) }),
|
|
117
|
+
}).pipe(Scope.extend(scope), Effect.provide(WorkerLive))
|
|
118
|
+
|
|
119
|
+
const result = yield* pool.executeEffect(new StartStubbornWorker({ blockDuration: 30_000 }))
|
|
120
|
+
workerPid = result.pid
|
|
121
|
+
|
|
122
|
+
// Verify the worker is running
|
|
123
|
+
assert.strictEqual(isProcessRunning(workerPid!), true, 'Worker process should be running')
|
|
124
|
+
} finally {
|
|
125
|
+
// Abruptly close the scope (simulating test abortion)
|
|
126
|
+
yield* Scope.close(scope, Exit.void)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Wait for cleanup
|
|
130
|
+
yield* Effect.sleep('1 second')
|
|
131
|
+
|
|
132
|
+
// This should pass but will initially fail due to zombie process issue
|
|
133
|
+
if (workerPid) {
|
|
134
|
+
assert.strictEqual(
|
|
135
|
+
isProcessRunning(workerPid),
|
|
136
|
+
false,
|
|
137
|
+
`Worker process ${workerPid} should be terminated after scope closure`,
|
|
138
|
+
)
|
|
139
|
+
} else {
|
|
140
|
+
assert.fail('Worker PID was not captured')
|
|
141
|
+
}
|
|
142
|
+
}).pipe(Effect.runPromise))
|
|
143
|
+
|
|
144
|
+
it('should clean up child processes when parent receives SIGINT', () =>
|
|
145
|
+
Effect.gen(function* () {
|
|
146
|
+
let workerPid: number | undefined
|
|
147
|
+
|
|
148
|
+
const pool = yield* EffectWorker.makePoolSerialized<WorkerMessage>({
|
|
149
|
+
size: 1,
|
|
150
|
+
initialMessage: () => new InitialMessage({ name: 'test', data: new Uint8Array([1, 2, 3]) }),
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
const result = yield* pool.executeEffect(new StartStubbornWorker({ blockDuration: 60_000 }))
|
|
154
|
+
workerPid = result.pid
|
|
155
|
+
|
|
156
|
+
// Verify the worker is running
|
|
157
|
+
assert.strictEqual(isProcessRunning(workerPid), true, 'Worker process should be running')
|
|
158
|
+
|
|
159
|
+
// Simulate SIGINT being sent to current process (like Ctrl+C in vitest)
|
|
160
|
+
// This should trigger cleanup of child processes
|
|
161
|
+
yield* Effect.async<void>((resume) => {
|
|
162
|
+
// Store current listeners before we manipulate them
|
|
163
|
+
const currentSIGINTListeners = process.listeners('SIGINT').slice()
|
|
164
|
+
|
|
165
|
+
// Set up our test handler
|
|
166
|
+
const testHandler = () => {
|
|
167
|
+
// Emit SIGINT to all current listeners to trigger cleanup
|
|
168
|
+
currentSIGINTListeners.forEach((listener) => {
|
|
169
|
+
try {
|
|
170
|
+
;(listener as Function)()
|
|
171
|
+
} catch {
|
|
172
|
+
// Ignore errors
|
|
173
|
+
}
|
|
174
|
+
})
|
|
175
|
+
resume(Effect.void)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Remove all current SIGINT listeners and add our test handler
|
|
179
|
+
process.removeAllListeners('SIGINT')
|
|
180
|
+
process.once('SIGINT', testHandler)
|
|
181
|
+
|
|
182
|
+
// Send SIGINT after a short delay
|
|
183
|
+
setTimeout(() => {
|
|
184
|
+
process.kill(process.pid, 'SIGINT')
|
|
185
|
+
}, 1000)
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
// Wait for cleanup to complete
|
|
189
|
+
yield* Effect.sleep('2 seconds')
|
|
190
|
+
|
|
191
|
+
// This test should initially fail - child process will still be running
|
|
192
|
+
if (workerPid) {
|
|
193
|
+
assert.strictEqual(
|
|
194
|
+
isProcessRunning(workerPid),
|
|
195
|
+
false,
|
|
196
|
+
`Worker process ${workerPid} should be terminated after SIGINT`,
|
|
197
|
+
)
|
|
198
|
+
} else {
|
|
199
|
+
assert.fail('Worker PID was not captured')
|
|
200
|
+
}
|
|
201
|
+
}).pipe(Effect.scoped, Effect.provide(WorkerLive), Effect.runPromise))
|
|
202
|
+
|
|
203
|
+
it('should clean up multiple concurrent child processes', () =>
|
|
204
|
+
Effect.gen(function* () {
|
|
205
|
+
let workerPids: number[] = []
|
|
206
|
+
|
|
207
|
+
const testEffect = Effect.gen(function* () {
|
|
208
|
+
const pool = yield* EffectWorker.makePoolSerialized<WorkerMessage>({
|
|
209
|
+
size: 3, // Multiple workers
|
|
210
|
+
initialMessage: () => new InitialMessage({ name: 'test', data: new Uint8Array([1, 2, 3]) }),
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
// Start multiple stubborn workers
|
|
214
|
+
const workers = yield* Effect.all(
|
|
215
|
+
[
|
|
216
|
+
pool.executeEffect(new StartStubbornWorker({ blockDuration: 30_000 })),
|
|
217
|
+
pool.executeEffect(new StartStubbornWorker({ blockDuration: 30_000 })),
|
|
218
|
+
pool.executeEffect(new StartStubbornWorker({ blockDuration: 30_000 })),
|
|
219
|
+
],
|
|
220
|
+
{ concurrency: 'unbounded' },
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
workerPids = workers.map((w) => w.pid)
|
|
224
|
+
|
|
225
|
+
// Verify all workers are running
|
|
226
|
+
for (const pid of workerPids) {
|
|
227
|
+
assert.strictEqual(isProcessRunning(pid), true, `Worker process ${pid} should be running`)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
yield* Effect.sleep('30 seconds') // Keep running until interrupted
|
|
231
|
+
}).pipe(Effect.scoped, Effect.provide(WorkerLive))
|
|
232
|
+
|
|
233
|
+
// Run with timeout to force termination
|
|
234
|
+
const fiber = yield* Effect.fork(testEffect)
|
|
235
|
+
yield* Effect.sleep('2 seconds')
|
|
236
|
+
yield* Fiber.interrupt(fiber)
|
|
237
|
+
|
|
238
|
+
// Wait for cleanup
|
|
239
|
+
yield* Effect.sleep('2 seconds')
|
|
240
|
+
|
|
241
|
+
// All worker processes should be cleaned up
|
|
242
|
+
for (const pid of workerPids) {
|
|
243
|
+
assert.strictEqual(
|
|
244
|
+
isProcessRunning(pid),
|
|
245
|
+
false,
|
|
246
|
+
`Worker process ${pid} should be terminated after pool cleanup`,
|
|
247
|
+
)
|
|
248
|
+
}
|
|
249
|
+
}).pipe(Effect.runPromise))
|
|
250
|
+
|
|
251
|
+
it('should handle direct ChildProcess.fork cleanup (node-sync pattern)', () =>
|
|
252
|
+
Effect.gen(function* () {
|
|
253
|
+
let childPid: number | undefined
|
|
254
|
+
|
|
255
|
+
// This mimics the exact pattern used in node-sync tests
|
|
256
|
+
const nodeChildProcess = ChildProcess.fork(
|
|
257
|
+
new URL(
|
|
258
|
+
'../../../../dist/node/ChildProcessRunner/ChildProcessRunnerTest/serializedWorker.js',
|
|
259
|
+
import.meta.url,
|
|
260
|
+
),
|
|
261
|
+
['test-client'],
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
childPid = nodeChildProcess.pid
|
|
265
|
+
|
|
266
|
+
const testEffect = Effect.gen(function* () {
|
|
267
|
+
const worker = yield* EffectWorker.makePoolSerialized<WorkerMessage>({
|
|
268
|
+
size: 1,
|
|
269
|
+
concurrency: 100,
|
|
270
|
+
initialMessage: () => new InitialMessage({ name: 'test', data: new Uint8Array([1, 2, 3]) }),
|
|
271
|
+
}).pipe(Effect.provide(ChildProcessWorker.layer(() => nodeChildProcess)))
|
|
272
|
+
|
|
273
|
+
// Start stubborn worker
|
|
274
|
+
yield* worker.executeEffect(new StartStubbornWorker({ blockDuration: 60_000 }))
|
|
275
|
+
|
|
276
|
+
// Verify process is running
|
|
277
|
+
if (childPid) {
|
|
278
|
+
assert.strictEqual(isProcessRunning(childPid), true, 'Child process should be running')
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Keep running until interrupted
|
|
282
|
+
yield* Effect.sleep('30 seconds')
|
|
283
|
+
}).pipe(Effect.scoped)
|
|
284
|
+
|
|
285
|
+
// Simulate the exact abortion pattern from node-sync
|
|
286
|
+
const fiber = yield* Effect.fork(testEffect)
|
|
287
|
+
yield* Effect.sleep('2 seconds')
|
|
288
|
+
|
|
289
|
+
// Force kill the fiber without proper cleanup (simulates Ctrl+C)
|
|
290
|
+
yield* Fiber.interrupt(fiber)
|
|
291
|
+
|
|
292
|
+
// Wait for cleanup
|
|
293
|
+
yield* Effect.sleep('3 seconds')
|
|
294
|
+
|
|
295
|
+
// This test should initially fail - demonstrating the zombie process issue
|
|
296
|
+
if (childPid) {
|
|
297
|
+
assert.strictEqual(
|
|
298
|
+
isProcessRunning(childPid),
|
|
299
|
+
false,
|
|
300
|
+
`Child process ${childPid} should be terminated after forced interruption`,
|
|
301
|
+
)
|
|
302
|
+
} else {
|
|
303
|
+
assert.fail('Child PID was not captured')
|
|
304
|
+
}
|
|
305
|
+
}).pipe(Effect.runPromise))
|
|
306
|
+
})
|
|
52
307
|
})
|
|
@@ -35,6 +35,12 @@ export class RunnerInterrupt extends Schema.TaggedRequest<RunnerInterrupt>()('Ru
|
|
|
35
35
|
payload: {},
|
|
36
36
|
}) {}
|
|
37
37
|
|
|
38
|
+
export class StartStubbornWorker extends Schema.TaggedRequest<StartStubbornWorker>()('StartStubbornWorker', {
|
|
39
|
+
failure: Schema.Never,
|
|
40
|
+
success: Schema.Struct({ pid: Schema.Number }),
|
|
41
|
+
payload: { blockDuration: Schema.Number },
|
|
42
|
+
}) {}
|
|
43
|
+
|
|
38
44
|
export class InitialMessage extends Schema.TaggedRequest<InitialMessage>()('InitialMessage', {
|
|
39
45
|
failure: Schema.Never,
|
|
40
46
|
success: Schema.Void,
|
|
@@ -61,5 +67,12 @@ export class GetSpan extends Schema.TaggedRequest<GetSpan>()('GetSpan', {
|
|
|
61
67
|
payload: {},
|
|
62
68
|
}) {}
|
|
63
69
|
|
|
64
|
-
export const WorkerMessage = Schema.Union(
|
|
70
|
+
export const WorkerMessage = Schema.Union(
|
|
71
|
+
GetUserById,
|
|
72
|
+
GetPersonById,
|
|
73
|
+
InitialMessage,
|
|
74
|
+
GetSpan,
|
|
75
|
+
RunnerInterrupt,
|
|
76
|
+
StartStubbornWorker,
|
|
77
|
+
)
|
|
65
78
|
export type WorkerMessage = Schema.Schema.Type<typeof WorkerMessage>
|
|
@@ -22,7 +22,7 @@ const WorkerLive = Runner.layerSerialized(WorkerMessage, {
|
|
|
22
22
|
// InitialMessage: (req) => Layer.succeed(Name, req.name),
|
|
23
23
|
InitialMessage: (req) =>
|
|
24
24
|
Effect.gen(function* () {
|
|
25
|
-
yield* Effect.addFinalizer(() => Effect.log('closing worker scope'))
|
|
25
|
+
// yield* Effect.addFinalizer(() => Effect.log('closing worker scope'))
|
|
26
26
|
return Layer.succeed(Name, req.name)
|
|
27
27
|
}).pipe(Layer.unwrapScoped),
|
|
28
28
|
// InitialMessage: (req) =>
|
|
@@ -34,8 +34,8 @@ const WorkerLive = Runner.layerSerialized(WorkerMessage, {
|
|
|
34
34
|
// }),
|
|
35
35
|
// ),
|
|
36
36
|
GetSpan: (_) =>
|
|
37
|
-
Effect.gen(function* (
|
|
38
|
-
const span = yield*
|
|
37
|
+
Effect.gen(function* () {
|
|
38
|
+
const span = yield* Effect.currentSpan.pipe(Effect.orDie)
|
|
39
39
|
return {
|
|
40
40
|
traceId: span.traceId,
|
|
41
41
|
spanId: span.spanId,
|
|
@@ -47,6 +47,19 @@ const WorkerLive = Runner.layerSerialized(WorkerMessage, {
|
|
|
47
47
|
}
|
|
48
48
|
}).pipe(Effect.withSpan('GetSpan')),
|
|
49
49
|
RunnerInterrupt: () => Effect.interrupt,
|
|
50
|
+
StartStubbornWorker: ({ blockDuration }) =>
|
|
51
|
+
Effect.gen(function* () {
|
|
52
|
+
// Start a blocking operation that won't respond to normal shutdown signals
|
|
53
|
+
const pid = process.pid
|
|
54
|
+
yield* Effect.fork(
|
|
55
|
+
Effect.gen(function* () {
|
|
56
|
+
// Block for the specified duration, ignoring shutdown attempts
|
|
57
|
+
yield* Effect.sleep(`${blockDuration} millis`)
|
|
58
|
+
yield* Effect.log('Stubborn worker finished blocking')
|
|
59
|
+
}).pipe(Effect.uninterruptible),
|
|
60
|
+
)
|
|
61
|
+
return { pid }
|
|
62
|
+
}),
|
|
50
63
|
}).pipe(Layer.provide(ChildProcessRunner.layer))
|
|
51
64
|
// }).pipe(Layer.provide(PlatformNode.NodeWorkerRunner.layer))
|
|
52
65
|
|
|
@@ -7,22 +7,114 @@ import * as Exit from 'effect/Exit'
|
|
|
7
7
|
import * as Layer from 'effect/Layer'
|
|
8
8
|
import * as Scope from 'effect/Scope'
|
|
9
9
|
|
|
10
|
+
// Track child processes for cleanup on process signals
|
|
11
|
+
const childProcesses = new Set<ChildProcess.ChildProcess>()
|
|
12
|
+
|
|
13
|
+
// Force cleanup all tracked child processes
|
|
14
|
+
const forceCleanupChildren = (signal: NodeJS.Signals = 'SIGKILL') => {
|
|
15
|
+
for (const child of childProcesses) {
|
|
16
|
+
try {
|
|
17
|
+
if (!child.killed) {
|
|
18
|
+
child.kill(signal)
|
|
19
|
+
}
|
|
20
|
+
} catch {
|
|
21
|
+
// Ignore errors during cleanup
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
childProcesses.clear()
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Install signal handlers once to clean up all child processes
|
|
28
|
+
let signalHandlersInstalled = false
|
|
29
|
+
|
|
30
|
+
const installSignalHandlers = () => {
|
|
31
|
+
if (signalHandlersInstalled) return
|
|
32
|
+
signalHandlersInstalled = true
|
|
33
|
+
|
|
34
|
+
// Use 'beforeExit' instead of signal handlers since tests may interfere with signals
|
|
35
|
+
process.on('beforeExit', () => {
|
|
36
|
+
forceCleanupChildren('SIGKILL')
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
// Also try to cleanup on uncaught exceptions
|
|
40
|
+
process.on('uncaughtException', () => {
|
|
41
|
+
forceCleanupChildren('SIGKILL')
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
// Install signal handlers but make them more robust
|
|
45
|
+
const sigintHandler = () => {
|
|
46
|
+
forceCleanupChildren('SIGINT')
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const sigtermHandler = () => {
|
|
50
|
+
forceCleanupChildren('SIGTERM')
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const exitHandler = () => {
|
|
54
|
+
forceCleanupChildren('SIGKILL')
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Add handlers that will persist even if tests remove/add other handlers
|
|
58
|
+
process.prependListener('SIGINT', sigintHandler)
|
|
59
|
+
process.prependListener('SIGTERM', sigtermHandler)
|
|
60
|
+
process.prependListener('exit', exitHandler)
|
|
61
|
+
}
|
|
62
|
+
|
|
10
63
|
const platformWorkerImpl = Worker.makePlatform<ChildProcess.ChildProcess>()({
|
|
11
64
|
setup({ scope, worker: childProcess }) {
|
|
12
65
|
return Effect.flatMap(Deferred.make<void, WorkerError>(), (exitDeferred) => {
|
|
66
|
+
// Install signal handlers for process-wide cleanup
|
|
67
|
+
installSignalHandlers()
|
|
68
|
+
|
|
69
|
+
// Track this child process for cleanup
|
|
70
|
+
childProcesses.add(childProcess)
|
|
71
|
+
|
|
13
72
|
childProcess.on('exit', () => {
|
|
73
|
+
// Remove from tracking when process exits
|
|
74
|
+
childProcesses.delete(childProcess)
|
|
14
75
|
Deferred.unsafeDone(exitDeferred, Exit.void)
|
|
15
76
|
})
|
|
77
|
+
|
|
78
|
+
childProcess.send(['setup-parent-death-detection', { parentPid: process.pid }])
|
|
79
|
+
|
|
16
80
|
return Effect.as(
|
|
17
81
|
Scope.addFinalizer(
|
|
18
82
|
scope,
|
|
19
83
|
Effect.suspend(() => {
|
|
20
|
-
|
|
84
|
+
// Try graceful shutdown first
|
|
85
|
+
try {
|
|
86
|
+
childProcess.send([1])
|
|
87
|
+
} catch {
|
|
88
|
+
// IPC channel might be closed, proceed to forceful termination
|
|
89
|
+
}
|
|
90
|
+
|
|
21
91
|
return Deferred.await(exitDeferred)
|
|
22
92
|
}).pipe(
|
|
23
|
-
Effect.timeout(
|
|
93
|
+
Effect.timeout(3000), // Reduced timeout for faster cleanup
|
|
24
94
|
Effect.interruptible,
|
|
25
|
-
Effect.catchAllCause(() =>
|
|
95
|
+
Effect.catchAllCause(() =>
|
|
96
|
+
Effect.sync(() => {
|
|
97
|
+
// Enhanced cleanup with escalating signals
|
|
98
|
+
if (!childProcess.killed) {
|
|
99
|
+
try {
|
|
100
|
+
// First try SIGTERM
|
|
101
|
+
childProcess.kill('SIGTERM')
|
|
102
|
+
|
|
103
|
+
// If still running after a short delay, use SIGKILL
|
|
104
|
+
setTimeout(() => {
|
|
105
|
+
if (!childProcess.killed) {
|
|
106
|
+
childProcess.kill('SIGKILL')
|
|
107
|
+
}
|
|
108
|
+
}, 1000)
|
|
109
|
+
} catch {
|
|
110
|
+
// Process might already be dead
|
|
111
|
+
} finally {
|
|
112
|
+
// Ensure it's removed from tracking
|
|
113
|
+
childProcesses.delete(childProcess)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}),
|
|
117
|
+
),
|
|
26
118
|
),
|
|
27
119
|
),
|
|
28
120
|
{
|
|
@@ -56,5 +148,21 @@ export const layerWorker = Layer.succeed(Worker.PlatformWorker, platformWorkerIm
|
|
|
56
148
|
|
|
57
149
|
export const layerManager = Layer.provide(Worker.layerManager, layerWorker)
|
|
58
150
|
|
|
151
|
+
/**
|
|
152
|
+
* @example
|
|
153
|
+
* ```ts
|
|
154
|
+
* import * as ChildProcess from 'node:child_process'
|
|
155
|
+
* import { Effect, Worker } from '@effect/platform/Worker'
|
|
156
|
+
* import { ChildProcessWorker } from '@livestore/utils/node'
|
|
157
|
+
*
|
|
158
|
+
* Worker.makePoolSerialized<WorkerMessage>({
|
|
159
|
+
* size: 1,
|
|
160
|
+
* initialMessage: () => new InitialMessage({ name: 'test', data: new Uint8Array([1, 2, 3]) }),
|
|
161
|
+
* }).pipe(
|
|
162
|
+
* Effect.provide(ChildProcessWorker.layer(() => ChildProcess.fork(new URL('worker.ts', import.meta.url)))),
|
|
163
|
+
* )
|
|
164
|
+
* ```
|
|
165
|
+
*
|
|
166
|
+
*/
|
|
59
167
|
export const layer = (spawn: (id: number) => ChildProcess.ChildProcess) =>
|
|
60
168
|
Layer.merge(layerManager, Worker.layerSpawner(spawn))
|
package/src/node/mod.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as http from 'node:http'
|
|
2
|
-
|
|
2
|
+
import * as PlatformNode from '@effect/platform-node'
|
|
3
|
+
import { layer as ParcelWatcherLayer } from '@effect/platform-node/NodeFileSystem/ParcelWatcher'
|
|
3
4
|
import { Effect, Layer } from 'effect'
|
|
4
|
-
|
|
5
5
|
import { OtelTracer, UnknownError } from '../effect/index.ts'
|
|
6
6
|
import { makeNoopTracer } from '../NoopTracer.ts'
|
|
7
7
|
|
|
@@ -17,7 +17,7 @@ export * as ChildProcessWorker from './ChildProcessRunner/ChildProcessWorker.ts'
|
|
|
17
17
|
|
|
18
18
|
// export const OtelLiveHttp = (args: any): Layer.Layer<never> => Layer.empty
|
|
19
19
|
|
|
20
|
-
export const getFreePort = Effect.async<number, UnknownError>((cb, signal) => {
|
|
20
|
+
export const getFreePort: Effect.Effect<number, UnknownError> = Effect.async<number, UnknownError>((cb, signal) => {
|
|
21
21
|
const server = http.createServer()
|
|
22
22
|
|
|
23
23
|
signal.addEventListener('abort', () => {
|
|
@@ -37,8 +37,8 @@ export const getFreePort = Effect.async<number, UnknownError>((cb, signal) => {
|
|
|
37
37
|
})
|
|
38
38
|
|
|
39
39
|
// Error handling in case the server encounters an error
|
|
40
|
-
server.on('error', (
|
|
41
|
-
server.close(() => cb(Effect.fail(new UnknownError({ cause:
|
|
40
|
+
server.on('error', (cause) => {
|
|
41
|
+
server.close(() => cb(Effect.fail(new UnknownError({ cause, payload: 'Failed to get a free port' }))))
|
|
42
42
|
})
|
|
43
43
|
})
|
|
44
44
|
|
|
@@ -51,3 +51,10 @@ export const OtelLiveDummy: Layer.Layer<OtelTracer.OtelTracer> = Layer.suspend((
|
|
|
51
51
|
|
|
52
52
|
return TracingLive
|
|
53
53
|
})
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Layer that enables recursive file watching by combining the Node filesystem implementation with
|
|
57
|
+
* the Parcel-based watch backend. Mirrored from Effect’s platform-node Parcel watcher layer:
|
|
58
|
+
* https://github.com/Effect-TS/effect/blob/main/packages/platform-node/src/NodeFileSystem/ParcelWatcher.ts
|
|
59
|
+
*/
|
|
60
|
+
export const NodeRecursiveWatchLayer = Layer.mergeAll(PlatformNode.NodeFileSystem.layer, ParcelWatcherLayer)
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"msgpack.d.ts","sourceRoot":"","sources":["../../../src/effect/Schema/msgpack.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAG/B,eAAO,MAAM,OAAO,GAAI,CAAC,EAAE,CAAC,EAAE,QAAQ,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,mFAIrD,CAAA"}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { Schema } from 'effect';
|
|
2
|
-
import * as msgpack from 'msgpackr';
|
|
3
|
-
export const MsgPack = (schema) => Schema.transform(Schema.Uint8ArrayFromSelf, schema, {
|
|
4
|
-
encode: (decoded) => msgpack.pack(decoded),
|
|
5
|
-
decode: (encodedBytes) => msgpack.unpack(encodedBytes),
|
|
6
|
-
});
|
|
7
|
-
//# sourceMappingURL=msgpack.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"msgpack.js","sourceRoot":"","sources":["../../../src/effect/Schema/msgpack.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC/B,OAAO,KAAK,OAAO,MAAM,UAAU,CAAA;AAEnC,MAAM,CAAC,MAAM,OAAO,GAAG,CAAO,MAA2B,EAAE,EAAE,CAC3D,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,kBAAkB,EAAE,MAAM,EAAE;IAClD,MAAM,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC;IAC1C,MAAM,EAAE,CAAC,YAAY,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC;CACvD,CAAC,CAAA"}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Schema } from 'effect'
|
|
2
|
-
import * as msgpack from 'msgpackr'
|
|
3
|
-
|
|
4
|
-
export const MsgPack = <A, I>(schema: Schema.Schema<A, I>) =>
|
|
5
|
-
Schema.transform(Schema.Uint8ArrayFromSelf, schema, {
|
|
6
|
-
encode: (decoded) => msgpack.pack(decoded),
|
|
7
|
-
decode: (encodedBytes) => msgpack.unpack(encodedBytes),
|
|
8
|
-
})
|