@livestore/utils-dev 0.0.0-snapshot-1a48725ede0e1642a644d2972e548b7cead6c9a4 → 0.0.0-snapshot-2df821f8699de1c39c3ccfd1a7f790614b64edd9
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/node/DockerComposeService/DockerComposeService.d.ts +48 -0
- package/dist/node/DockerComposeService/DockerComposeService.d.ts.map +1 -0
- package/dist/node/DockerComposeService/DockerComposeService.js +106 -0
- package/dist/node/DockerComposeService/DockerComposeService.js.map +1 -0
- package/dist/node/DockerComposeService/DockerComposeService.test.d.ts +2 -0
- package/dist/node/DockerComposeService/DockerComposeService.test.d.ts.map +1 -0
- package/dist/node/DockerComposeService/DockerComposeService.test.js +64 -0
- package/dist/node/DockerComposeService/DockerComposeService.test.js.map +1 -0
- package/dist/node/WranglerDevServer/WranglerDevServer.d.ts +51 -0
- package/dist/node/WranglerDevServer/WranglerDevServer.d.ts.map +1 -0
- package/dist/node/WranglerDevServer/WranglerDevServer.js +102 -0
- package/dist/node/WranglerDevServer/WranglerDevServer.js.map +1 -0
- package/dist/node/WranglerDevServer/WranglerDevServer.test.d.ts +2 -0
- package/dist/node/WranglerDevServer/WranglerDevServer.test.d.ts.map +1 -0
- package/dist/node/WranglerDevServer/WranglerDevServer.test.js +186 -0
- package/dist/node/WranglerDevServer/WranglerDevServer.test.js.map +1 -0
- package/dist/node/WranglerDevServer/fixtures/cf-worker.d.ts +8 -0
- package/dist/node/WranglerDevServer/fixtures/cf-worker.d.ts.map +1 -0
- package/dist/node/WranglerDevServer/fixtures/cf-worker.js +11 -0
- package/dist/node/WranglerDevServer/fixtures/cf-worker.js.map +1 -0
- package/dist/node/WranglerDevServer/process-tree-manager.d.ts +55 -0
- package/dist/node/WranglerDevServer/process-tree-manager.d.ts.map +1 -0
- package/dist/node/WranglerDevServer/process-tree-manager.js +178 -0
- package/dist/node/WranglerDevServer/process-tree-manager.js.map +1 -0
- package/dist/node/mod.d.ts +3 -2
- package/dist/node/mod.d.ts.map +1 -1
- package/dist/node/mod.js +3 -2
- package/dist/node/mod.js.map +1 -1
- package/dist/node-vitest/Vitest.d.ts +15 -1
- package/dist/node-vitest/Vitest.d.ts.map +1 -1
- package/dist/node-vitest/Vitest.js +18 -1
- package/dist/node-vitest/Vitest.js.map +1 -1
- package/package.json +2 -2
- package/src/node/DockerComposeService/DockerComposeService.test.ts +91 -0
- package/src/node/DockerComposeService/DockerComposeService.ts +251 -0
- package/src/node/DockerComposeService/test-fixtures/docker-compose.yml +4 -0
- package/src/node/WranglerDevServer/WranglerDevServer.test.ts +268 -0
- package/src/node/WranglerDevServer/WranglerDevServer.ts +206 -0
- package/src/node/WranglerDevServer/fixtures/cf-worker.ts +11 -0
- package/src/node/WranglerDevServer/fixtures/wrangler.toml +11 -0
- package/src/node/WranglerDevServer/process-tree-manager.ts +263 -0
- package/src/node/mod.ts +16 -2
- package/src/node-vitest/Vitest.ts +60 -1
- package/dist/node/vitest-docker-compose-setup.d.ts +0 -32
- package/dist/node/vitest-docker-compose-setup.d.ts.map +0 -1
- package/dist/node/vitest-docker-compose-setup.js +0 -131
- package/dist/node/vitest-docker-compose-setup.js.map +0 -1
- package/dist/node/vitest-wrangler-setup.d.ts +0 -27
- package/dist/node/vitest-wrangler-setup.d.ts.map +0 -1
- package/dist/node/vitest-wrangler-setup.js +0 -96
- package/dist/node/vitest-wrangler-setup.js.map +0 -1
- package/src/node/vitest-docker-compose-setup.ts +0 -182
- package/src/node/vitest-wrangler-setup.ts +0 -132
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import * as path from 'node:path'
|
|
2
|
+
|
|
3
|
+
import { Command, Effect, Exit, type PlatformError, Schema, Stream } from '@livestore/utils/effect'
|
|
4
|
+
import { getFreePort } from '@livestore/utils/node'
|
|
5
|
+
import { cleanupOrphanedProcesses, killProcessTree } from './process-tree-manager.ts'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Error type for WranglerDevServer operations
|
|
9
|
+
*/
|
|
10
|
+
export class WranglerDevServerError extends Schema.TaggedError<WranglerDevServerError>()('WranglerDevServerError', {
|
|
11
|
+
cause: Schema.Unknown,
|
|
12
|
+
message: Schema.String,
|
|
13
|
+
port: Schema.Number,
|
|
14
|
+
}) {}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* WranglerDevServer instance interface
|
|
18
|
+
*/
|
|
19
|
+
export interface WranglerDevServer {
|
|
20
|
+
readonly port: number
|
|
21
|
+
readonly url: string
|
|
22
|
+
readonly processId: number
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Configuration for starting WranglerDevServer
|
|
27
|
+
*/
|
|
28
|
+
export interface StartWranglerDevServerArgs {
|
|
29
|
+
wranglerConfigPath?: string
|
|
30
|
+
cwd: string
|
|
31
|
+
port?: number
|
|
32
|
+
/** @default false */
|
|
33
|
+
showLogs?: boolean
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* WranglerDevServer as an Effect.Service.
|
|
38
|
+
*
|
|
39
|
+
* This service provides the WranglerDevServer properties and can be accessed
|
|
40
|
+
* directly to get port, url, and processId.
|
|
41
|
+
*
|
|
42
|
+
* TODO: Allow for config to be passed in via code instead of `wrangler.toml` file
|
|
43
|
+
* (would need to be placed in temporary file as wrangler only accepts files as config)
|
|
44
|
+
*/
|
|
45
|
+
export class WranglerDevServerService extends Effect.Service<WranglerDevServerService>()('WranglerDevServerService', {
|
|
46
|
+
scoped: (args: StartWranglerDevServerArgs) =>
|
|
47
|
+
Effect.gen(function* () {
|
|
48
|
+
const showLogs = args.showLogs ?? false
|
|
49
|
+
|
|
50
|
+
// Clean up any orphaned processes before starting (defensive cleanup)
|
|
51
|
+
yield* cleanupOrphanedProcesses(['wrangler', 'workerd']).pipe(
|
|
52
|
+
Effect.tap((result) =>
|
|
53
|
+
showLogs && (result.cleaned.length > 0 || result.failed.length > 0)
|
|
54
|
+
? Effect.logInfo(`Cleanup result: ${result.cleaned.length} cleaned, ${result.failed.length} failed`)
|
|
55
|
+
: Effect.void,
|
|
56
|
+
),
|
|
57
|
+
Effect.ignore, // Don't fail startup if cleanup fails
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
// Allocate port
|
|
61
|
+
const port =
|
|
62
|
+
args.port ??
|
|
63
|
+
(yield* getFreePort.pipe(
|
|
64
|
+
Effect.mapError(
|
|
65
|
+
(cause) => new WranglerDevServerError({ cause, message: 'Failed to get free port', port: -1 }),
|
|
66
|
+
),
|
|
67
|
+
))
|
|
68
|
+
|
|
69
|
+
yield* Effect.annotateCurrentSpan({ port })
|
|
70
|
+
|
|
71
|
+
// Resolve config path
|
|
72
|
+
const configPath = path.resolve(args.wranglerConfigPath ?? path.join(args.cwd, 'wrangler.toml'))
|
|
73
|
+
|
|
74
|
+
// Start wrangler process using Effect Command
|
|
75
|
+
const process = yield* Command.make(
|
|
76
|
+
'bunx',
|
|
77
|
+
'wrangler',
|
|
78
|
+
'dev',
|
|
79
|
+
'--port',
|
|
80
|
+
port.toString(),
|
|
81
|
+
'--config',
|
|
82
|
+
configPath,
|
|
83
|
+
).pipe(
|
|
84
|
+
Command.workingDirectory(args.cwd),
|
|
85
|
+
Command.stdout('pipe'),
|
|
86
|
+
Command.stderr('pipe'),
|
|
87
|
+
Command.start,
|
|
88
|
+
Effect.catchAllCause(
|
|
89
|
+
(error) =>
|
|
90
|
+
new WranglerDevServerError({
|
|
91
|
+
cause: error,
|
|
92
|
+
message: `Failed to start wrangler process in directory: ${args.cwd}`,
|
|
93
|
+
port,
|
|
94
|
+
}),
|
|
95
|
+
),
|
|
96
|
+
Effect.withSpan('WranglerDevServerService:startProcess'),
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
if (showLogs) {
|
|
100
|
+
yield* process.stderr.pipe(
|
|
101
|
+
Stream.decodeText('utf8'),
|
|
102
|
+
Stream.tapLogWithLabel('wrangler:stderr'),
|
|
103
|
+
Stream.runDrain,
|
|
104
|
+
Effect.forkScoped,
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const processId = process.pid
|
|
109
|
+
|
|
110
|
+
// We need to keep the `stdout` stream open, as we drain it in the waitForReady function
|
|
111
|
+
// Otherwise we'll get a EPIPE error
|
|
112
|
+
const stdout = yield* Stream.broadcastDynamic(process.stdout, 100)
|
|
113
|
+
|
|
114
|
+
// Register cleanup finalizer with intelligent timeout handling
|
|
115
|
+
yield* Effect.addFinalizer((exit) =>
|
|
116
|
+
Effect.gen(function* () {
|
|
117
|
+
const isInterrupted = Exit.isInterrupted(exit)
|
|
118
|
+
if (showLogs) {
|
|
119
|
+
yield* Effect.logDebug(`Cleaning up wrangler process ${processId}, interrupted: ${isInterrupted}`)
|
|
120
|
+
}
|
|
121
|
+
// yield* Effect.logDebug(`Cleaning up wrangler process ${processId}, interrupted: ${isInterrupted}`)
|
|
122
|
+
|
|
123
|
+
// Check if process is still running
|
|
124
|
+
const isRunning = yield* process.isRunning
|
|
125
|
+
|
|
126
|
+
if (isRunning) {
|
|
127
|
+
// Use our enhanced process tree cleanup
|
|
128
|
+
yield* killProcessTree(processId, {
|
|
129
|
+
timeout: isInterrupted ? 500 : 3000, // Fast cleanup on interruption
|
|
130
|
+
signals: ['SIGTERM', 'SIGKILL'],
|
|
131
|
+
includeRoot: true,
|
|
132
|
+
}).pipe(
|
|
133
|
+
Effect.tap((result) =>
|
|
134
|
+
showLogs
|
|
135
|
+
? Effect.logDebug(
|
|
136
|
+
`Cleaned up ${result.killedPids.length} processes, ${result.failedPids.length} failed`,
|
|
137
|
+
)
|
|
138
|
+
: Effect.void,
|
|
139
|
+
),
|
|
140
|
+
Effect.mapError(
|
|
141
|
+
(error) =>
|
|
142
|
+
new WranglerDevServerError({
|
|
143
|
+
cause: error,
|
|
144
|
+
message: `Failed to kill process tree for PID ${processId}`,
|
|
145
|
+
port: 0,
|
|
146
|
+
}),
|
|
147
|
+
),
|
|
148
|
+
Effect.ignore, // Don't fail the finalizer if cleanup has issues
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
// Also kill the command process handle
|
|
152
|
+
yield* process.kill()
|
|
153
|
+
} else if (showLogs) {
|
|
154
|
+
yield* Effect.logDebug(`Process ${processId} already terminated`)
|
|
155
|
+
}
|
|
156
|
+
}).pipe(
|
|
157
|
+
Effect.timeout('5 seconds'), // Don't let cleanup hang forever
|
|
158
|
+
Effect.ignoreLogged,
|
|
159
|
+
),
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
// Wait for server to be ready
|
|
163
|
+
yield* waitForReady({ stdout, showLogs })
|
|
164
|
+
|
|
165
|
+
if (showLogs) {
|
|
166
|
+
yield* Effect.logDebug(`Wrangler dev server ready on port ${port}`)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
port,
|
|
171
|
+
url: `http://localhost:${port}`,
|
|
172
|
+
processId,
|
|
173
|
+
} satisfies WranglerDevServer
|
|
174
|
+
}).pipe(
|
|
175
|
+
Effect.withSpan('WranglerDevServerService', {
|
|
176
|
+
attributes: { port: args.port ?? 'auto', cwd: args.cwd },
|
|
177
|
+
}),
|
|
178
|
+
),
|
|
179
|
+
}) {}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Waits for Wrangler server to be ready by monitoring stdout for "Ready on" message
|
|
183
|
+
*/
|
|
184
|
+
const waitForReady = ({
|
|
185
|
+
stdout,
|
|
186
|
+
showLogs,
|
|
187
|
+
}: {
|
|
188
|
+
stdout: Stream.Stream<Uint8Array, PlatformError.PlatformError, never>
|
|
189
|
+
showLogs: boolean
|
|
190
|
+
}): Effect.Effect<void, WranglerDevServerError, never> =>
|
|
191
|
+
stdout.pipe(
|
|
192
|
+
Stream.decodeText('utf8'),
|
|
193
|
+
Stream.splitLines,
|
|
194
|
+
Stream.tap((line) => (showLogs ? Effect.logDebug(`[wrangler] ${line}`) : Effect.void)),
|
|
195
|
+
Stream.takeUntil((line) => line.includes('Ready on')),
|
|
196
|
+
Stream.runDrain,
|
|
197
|
+
Effect.timeout('30 seconds'),
|
|
198
|
+
Effect.mapError(
|
|
199
|
+
(error) =>
|
|
200
|
+
new WranglerDevServerError({
|
|
201
|
+
cause: error,
|
|
202
|
+
message: 'Wrangler server failed to start within timeout',
|
|
203
|
+
port: 0,
|
|
204
|
+
}),
|
|
205
|
+
),
|
|
206
|
+
)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
async fetch(_request: Request): Promise<Response> {
|
|
3
|
+
return new Response('Hello from Wrangler Dev Server test worker!')
|
|
4
|
+
},
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export class TestDO {
|
|
8
|
+
async fetch(_request: Request): Promise<Response> {
|
|
9
|
+
return new Response('Hello from Test Durable Object!')
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { Command, type CommandExecutor, Effect, Schema, type Scope, Stream } from '@livestore/utils/effect'
|
|
2
|
+
|
|
3
|
+
export class ProcessTreeError extends Schema.TaggedError<ProcessTreeError>()('ProcessTreeError', {
|
|
4
|
+
cause: Schema.Unknown,
|
|
5
|
+
message: Schema.String,
|
|
6
|
+
pid: Schema.Number,
|
|
7
|
+
}) {}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Finds all child processes of a given parent PID
|
|
11
|
+
*/
|
|
12
|
+
export const findChildProcesses = (
|
|
13
|
+
parentPid: number,
|
|
14
|
+
): Effect.Effect<number[], never, CommandExecutor.CommandExecutor | Scope.Scope> =>
|
|
15
|
+
Effect.gen(function* () {
|
|
16
|
+
const result = yield* Command.make('ps', '-o', 'pid,ppid', '-ax').pipe(
|
|
17
|
+
Command.start,
|
|
18
|
+
Effect.flatMap((command) =>
|
|
19
|
+
command.stdout.pipe(
|
|
20
|
+
Stream.decodeText('utf8'),
|
|
21
|
+
Stream.runCollect,
|
|
22
|
+
Effect.map((chunks) => Array.from(chunks).join('')),
|
|
23
|
+
),
|
|
24
|
+
),
|
|
25
|
+
Effect.catchAll(() => Effect.succeed('')), // Return empty string if command fails
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
if (!result) return []
|
|
29
|
+
|
|
30
|
+
const lines = result.split('\n')
|
|
31
|
+
const pattern = new RegExp(`^\\s*([0-9]+)\\s+${parentPid}\\s*$`)
|
|
32
|
+
|
|
33
|
+
const childPids = lines
|
|
34
|
+
.map((line) => {
|
|
35
|
+
const match = line.trim().match(pattern)
|
|
36
|
+
return match ? Number.parseInt(match[1]!, 10) : null
|
|
37
|
+
})
|
|
38
|
+
.filter((pid): pid is number => pid !== null)
|
|
39
|
+
|
|
40
|
+
return childPids
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Recursively finds all descendants of a process
|
|
45
|
+
*/
|
|
46
|
+
export const findProcessTree = (
|
|
47
|
+
rootPid: number,
|
|
48
|
+
): Effect.Effect<number[], never, CommandExecutor.CommandExecutor | Scope.Scope> =>
|
|
49
|
+
Effect.gen(function* () {
|
|
50
|
+
const allPids = new Set<number>([rootPid])
|
|
51
|
+
const toProcess = [rootPid]
|
|
52
|
+
|
|
53
|
+
while (toProcess.length > 0) {
|
|
54
|
+
const currentPid = toProcess.pop()!
|
|
55
|
+
const children = yield* findChildProcesses(currentPid)
|
|
56
|
+
|
|
57
|
+
for (const childPid of children) {
|
|
58
|
+
if (!allPids.has(childPid)) {
|
|
59
|
+
allPids.add(childPid)
|
|
60
|
+
toProcess.push(childPid)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return Array.from(allPids)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Checks if a process is running
|
|
70
|
+
*/
|
|
71
|
+
export const isProcessRunning = (pid: number): Effect.Effect<boolean, never, never> =>
|
|
72
|
+
Effect.sync(() => {
|
|
73
|
+
try {
|
|
74
|
+
process.kill(pid, 0)
|
|
75
|
+
return true
|
|
76
|
+
} catch {
|
|
77
|
+
return false
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Kills a process tree with escalating signals
|
|
83
|
+
*/
|
|
84
|
+
export const killProcessTree = (
|
|
85
|
+
rootPid: number,
|
|
86
|
+
options: {
|
|
87
|
+
timeout?: number
|
|
88
|
+
signals?: NodeJS.Signals[]
|
|
89
|
+
includeRoot?: boolean
|
|
90
|
+
} = {},
|
|
91
|
+
): Effect.Effect<
|
|
92
|
+
{ killedPids: number[]; failedPids: number[] },
|
|
93
|
+
never,
|
|
94
|
+
CommandExecutor.CommandExecutor | Scope.Scope
|
|
95
|
+
> =>
|
|
96
|
+
Effect.gen(function* () {
|
|
97
|
+
const { timeout = 5000, signals = ['SIGTERM', 'SIGKILL'], includeRoot = true } = options
|
|
98
|
+
|
|
99
|
+
// Find all processes in the tree
|
|
100
|
+
const allPids = yield* findProcessTree(rootPid)
|
|
101
|
+
const pidsToKill = includeRoot ? allPids : allPids.filter((pid) => pid !== rootPid)
|
|
102
|
+
|
|
103
|
+
if (pidsToKill.length === 0) {
|
|
104
|
+
return { killedPids: [], failedPids: [] }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const killedPids: number[] = []
|
|
108
|
+
const failedPids: number[] = []
|
|
109
|
+
|
|
110
|
+
// Try each signal with timeout
|
|
111
|
+
for (const signal of signals) {
|
|
112
|
+
// Check which processes are still running and not yet killed
|
|
113
|
+
const stillRunningChecks = yield* Effect.all(
|
|
114
|
+
pidsToKill
|
|
115
|
+
.filter((pid) => !killedPids.includes(pid))
|
|
116
|
+
.map((pid) => isProcessRunning(pid).pipe(Effect.map((running) => ({ pid, running })))),
|
|
117
|
+
)
|
|
118
|
+
const remainingPids = stillRunningChecks.filter(({ running }) => running).map(({ pid }) => pid)
|
|
119
|
+
|
|
120
|
+
if (remainingPids.length === 0) break
|
|
121
|
+
|
|
122
|
+
// Send signal to all remaining processes
|
|
123
|
+
for (const pid of remainingPids) {
|
|
124
|
+
yield* Effect.sync(() => {
|
|
125
|
+
try {
|
|
126
|
+
process.kill(pid, signal)
|
|
127
|
+
} catch {
|
|
128
|
+
// Process might already be dead, continue
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Wait for processes to terminate with polling
|
|
134
|
+
const waitStart = Date.now()
|
|
135
|
+
while (Date.now() - waitStart < timeout) {
|
|
136
|
+
const runningChecks = yield* Effect.all(
|
|
137
|
+
remainingPids.map((pid) => isProcessRunning(pid).pipe(Effect.map((running) => ({ pid, running })))),
|
|
138
|
+
)
|
|
139
|
+
const stillRunning = runningChecks.filter(({ running }) => running).map(({ pid }) => pid)
|
|
140
|
+
|
|
141
|
+
if (stillRunning.length === 0) {
|
|
142
|
+
// All processes terminated
|
|
143
|
+
killedPids.push(...remainingPids)
|
|
144
|
+
break
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Short sleep before checking again
|
|
148
|
+
yield* Effect.sleep('100 millis')
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Check final status
|
|
153
|
+
const finalChecks = yield* Effect.all(
|
|
154
|
+
pidsToKill.map((pid) => isProcessRunning(pid).pipe(Effect.map((running) => ({ pid, running })))),
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
for (const { pid, running } of finalChecks) {
|
|
158
|
+
if (!killedPids.includes(pid) && running) {
|
|
159
|
+
failedPids.push(pid)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return { killedPids, failedPids }
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Finds orphaned processes by name pattern
|
|
168
|
+
*/
|
|
169
|
+
export const findOrphanedProcesses = (
|
|
170
|
+
namePattern: string,
|
|
171
|
+
): Effect.Effect<number[], never, CommandExecutor.CommandExecutor | Scope.Scope> =>
|
|
172
|
+
Effect.gen(function* () {
|
|
173
|
+
// Find processes that match the pattern and have init (PID 1) as parent
|
|
174
|
+
const result = yield* Command.make('ps', '-eo', 'pid,ppid,comm').pipe(
|
|
175
|
+
Command.start,
|
|
176
|
+
Effect.flatMap((command) =>
|
|
177
|
+
command.stdout.pipe(
|
|
178
|
+
Stream.decodeText('utf8'),
|
|
179
|
+
Stream.runCollect,
|
|
180
|
+
Effect.map((chunks) => Array.from(chunks).join('')),
|
|
181
|
+
),
|
|
182
|
+
),
|
|
183
|
+
Effect.catchAll(() => Effect.succeed('')), // Return empty string if command fails
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
if (!result) return []
|
|
187
|
+
|
|
188
|
+
const lines = result.split('\n')
|
|
189
|
+
const patternRegex = new RegExp(namePattern)
|
|
190
|
+
const parentRegex = /^\s*(\d+)\s+1\s+/
|
|
191
|
+
|
|
192
|
+
const orphanedPids = lines
|
|
193
|
+
.filter((line) => patternRegex.test(line))
|
|
194
|
+
.map((line) => {
|
|
195
|
+
const match = line.trim().match(parentRegex)
|
|
196
|
+
return match ? Number.parseInt(match[1]!, 10) : null
|
|
197
|
+
})
|
|
198
|
+
.filter((pid): pid is number => pid !== null)
|
|
199
|
+
|
|
200
|
+
return orphanedPids
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Defensive cleanup for orphaned processes matching given patterns.
|
|
205
|
+
*
|
|
206
|
+
* This function provides fallback cleanup for edge cases where normal process
|
|
207
|
+
* termination mechanisms fail (e.g., hard crashes, SIGKILL before cleanup runs,
|
|
208
|
+
* or limitations in synchronous exit handlers). While proper process tree cleanup
|
|
209
|
+
* should prevent orphans in most cases, this serves as a safety net for scenarios
|
|
210
|
+
* where child processes become orphaned despite cleanup efforts.
|
|
211
|
+
*
|
|
212
|
+
* @param processPatterns - Array of process name patterns to search for (e.g., ['wrangler', 'workerd'])
|
|
213
|
+
* @returns Object with arrays of successfully cleaned and failed PIDs
|
|
214
|
+
*/
|
|
215
|
+
export const cleanupOrphanedProcesses = (
|
|
216
|
+
processPatterns: string[],
|
|
217
|
+
): Effect.Effect<{ cleaned: number[]; failed: number[] }, never, CommandExecutor.CommandExecutor | Scope.Scope> =>
|
|
218
|
+
Effect.gen(function* () {
|
|
219
|
+
const cleaned: number[] = []
|
|
220
|
+
const failed: number[] = []
|
|
221
|
+
|
|
222
|
+
// Find all orphaned processes matching the patterns
|
|
223
|
+
const allOrphanedPids: number[] = []
|
|
224
|
+
const patternCounts: Record<string, number> = {}
|
|
225
|
+
|
|
226
|
+
for (const pattern of processPatterns) {
|
|
227
|
+
const orphaned = yield* findOrphanedProcesses(pattern)
|
|
228
|
+
allOrphanedPids.push(...orphaned)
|
|
229
|
+
patternCounts[pattern] = orphaned.length
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (allOrphanedPids.length === 0) {
|
|
233
|
+
return { cleaned, failed }
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const patternSummary = Object.entries(patternCounts)
|
|
237
|
+
.map(([pattern, count]) => `${count} ${pattern}`)
|
|
238
|
+
.join(', ')
|
|
239
|
+
|
|
240
|
+
yield* Effect.logInfo(
|
|
241
|
+
`Found ${allOrphanedPids.length} orphaned processes (${patternSummary}): ${allOrphanedPids.join(', ')}`,
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
for (const pid of allOrphanedPids) {
|
|
245
|
+
const result = yield* killProcessTree(pid, {
|
|
246
|
+
timeout: 2000,
|
|
247
|
+
signals: ['SIGTERM', 'SIGKILL'],
|
|
248
|
+
includeRoot: true,
|
|
249
|
+
}).pipe(Effect.orElse(() => Effect.succeed({ killedPids: [], failedPids: [pid] })))
|
|
250
|
+
|
|
251
|
+
if (result.failedPids.length === 0) {
|
|
252
|
+
cleaned.push(...result.killedPids)
|
|
253
|
+
yield* Effect.logInfo(
|
|
254
|
+
`Cleaned up orphaned process tree starting with ${pid} (${result.killedPids.length} processes)`,
|
|
255
|
+
)
|
|
256
|
+
} else {
|
|
257
|
+
failed.push(pid, ...result.failedPids)
|
|
258
|
+
yield* Effect.logWarning(`Failed to clean up some processes in tree ${pid}: ${result.failedPids.join(', ')}`)
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return { cleaned, failed }
|
|
263
|
+
})
|
package/src/node/mod.ts
CHANGED
|
@@ -14,9 +14,23 @@ import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base'
|
|
|
14
14
|
export { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http'
|
|
15
15
|
export { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
|
|
16
16
|
export * from './cmd.ts'
|
|
17
|
+
export {
|
|
18
|
+
type DockerComposeArgs,
|
|
19
|
+
DockerComposeError,
|
|
20
|
+
type DockerComposeOperations,
|
|
21
|
+
DockerComposeService,
|
|
22
|
+
type LogsOptions,
|
|
23
|
+
type StartOptions,
|
|
24
|
+
startDockerComposeServicesScoped,
|
|
25
|
+
} from './DockerComposeService/DockerComposeService.ts'
|
|
17
26
|
export * as FileLogger from './FileLogger.ts'
|
|
18
|
-
export * from './
|
|
19
|
-
export
|
|
27
|
+
export * from './WranglerDevServer/process-tree-manager.ts'
|
|
28
|
+
export {
|
|
29
|
+
type StartWranglerDevServerArgs,
|
|
30
|
+
type WranglerDevServer,
|
|
31
|
+
WranglerDevServerError,
|
|
32
|
+
WranglerDevServerService,
|
|
33
|
+
} from './WranglerDevServer/WranglerDevServer.ts'
|
|
20
34
|
|
|
21
35
|
export const OtelLiveHttp = ({
|
|
22
36
|
serviceName,
|
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
import * as inspector from 'node:inspector'
|
|
2
2
|
import type * as Vitest from '@effect/vitest'
|
|
3
3
|
import { IS_CI } from '@livestore/utils'
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
type Cause,
|
|
6
|
+
Duration,
|
|
7
|
+
Effect,
|
|
8
|
+
type FastCheck as FC,
|
|
9
|
+
identity,
|
|
10
|
+
Layer,
|
|
11
|
+
type OtelTracer,
|
|
12
|
+
Predicate,
|
|
13
|
+
type Schema,
|
|
14
|
+
type Scope,
|
|
15
|
+
} from '@livestore/utils/effect'
|
|
5
16
|
import { OtelLiveDummy } from '@livestore/utils/node'
|
|
6
17
|
import { OtelLiveHttp } from '../node/mod.ts'
|
|
7
18
|
|
|
@@ -62,3 +73,51 @@ export const withTestCtx =
|
|
|
62
73
|
Effect.annotateLogs({ suffix }),
|
|
63
74
|
) as any
|
|
64
75
|
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Equivalent to Vitest.prop but provides extra prop context to the test function
|
|
79
|
+
*
|
|
80
|
+
* TODO: allow for upper timelimit instead of / additional to `numRuns`
|
|
81
|
+
*
|
|
82
|
+
* TODO: Upstream to Effect
|
|
83
|
+
*/
|
|
84
|
+
export const asProp = <Arbs extends Vitest.Vitest.Arbitraries, A, E, R>(
|
|
85
|
+
api: Vitest.Vitest.Tester<R>,
|
|
86
|
+
name: string,
|
|
87
|
+
arbitraries: Arbs,
|
|
88
|
+
test: Vitest.Vitest.TestFunction<
|
|
89
|
+
A,
|
|
90
|
+
E,
|
|
91
|
+
R,
|
|
92
|
+
[
|
|
93
|
+
{ [K in keyof Arbs]: Arbs[K] extends FC.Arbitrary<infer T> ? T : Schema.Schema.Type<Arbs[K]> },
|
|
94
|
+
Vitest.TestContext,
|
|
95
|
+
{
|
|
96
|
+
numRuns: number
|
|
97
|
+
/** 0-based index */
|
|
98
|
+
runIndex: number
|
|
99
|
+
},
|
|
100
|
+
]
|
|
101
|
+
>,
|
|
102
|
+
propOptions:
|
|
103
|
+
| number
|
|
104
|
+
| (Vitest.TestOptions & {
|
|
105
|
+
fastCheck?: FC.Parameters<{
|
|
106
|
+
[K in keyof Arbs]: Arbs[K] extends FC.Arbitrary<infer T> ? T : Schema.Schema.Type<Arbs[K]>
|
|
107
|
+
}>
|
|
108
|
+
}),
|
|
109
|
+
) => {
|
|
110
|
+
const numRuns = Predicate.isObject(propOptions) ? (propOptions.fastCheck?.numRuns ?? 100) : 100
|
|
111
|
+
let runIndex = 0
|
|
112
|
+
return api.prop(
|
|
113
|
+
name,
|
|
114
|
+
arbitraries,
|
|
115
|
+
(properties, ctx) => {
|
|
116
|
+
if (ctx.signal.aborted) {
|
|
117
|
+
return ctx.skip('Test aborted')
|
|
118
|
+
}
|
|
119
|
+
return test(properties, ctx, { numRuns, runIndex: runIndex++ })
|
|
120
|
+
},
|
|
121
|
+
propOptions,
|
|
122
|
+
)
|
|
123
|
+
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { Effect, UnknownError } from '@livestore/utils/effect';
|
|
2
|
-
export declare const startDockerComposeServices: (args: StartDockerComposeServicesArgs) => Effect.Effect<void, UnknownError, never>;
|
|
3
|
-
type StartDockerComposeServicesArgs = {
|
|
4
|
-
composeFilePath?: string;
|
|
5
|
-
cwd: string;
|
|
6
|
-
serviceName?: string;
|
|
7
|
-
waitForLog?: string;
|
|
8
|
-
env?: Record<string, string>;
|
|
9
|
-
healthCheck?: {
|
|
10
|
-
url: string;
|
|
11
|
-
expectedStatus?: number;
|
|
12
|
-
maxAttempts?: number;
|
|
13
|
-
delayMs?: number;
|
|
14
|
-
};
|
|
15
|
-
/** @default false */
|
|
16
|
-
forwardLogs?: boolean;
|
|
17
|
-
};
|
|
18
|
-
export declare const pullDockerComposeImages: ({ cwd, composeFilePath, }: Pick<StartDockerComposeServicesArgs, "composeFilePath" | "cwd">) => Effect.Effect<void, import("@effect/platform/Error").PlatformError | import("./cmd.ts").CmdError, import("@effect/platform/CommandExecutor").CommandExecutor>;
|
|
19
|
-
/**
|
|
20
|
-
* Starts Docker Compose services for testing with automatic cleanup.
|
|
21
|
-
* Automatically allocates a free port and passes it as EXPOSED_PORT environment variable.
|
|
22
|
-
*
|
|
23
|
-
* @param composeFilePath - Path to docker-compose.yml file (defaults to `${cwd}/docker-compose.yml`)
|
|
24
|
-
* @param cwd - Working directory for Docker Compose commands
|
|
25
|
-
* @param env - Environment variables to pass to the Docker Compose commands (useful for passing ports)
|
|
26
|
-
* @param serviceName - Optional specific service to start (omit to start all)
|
|
27
|
-
* @param waitForLog - Log pattern to wait for before considering services ready
|
|
28
|
-
* @param healthCheck - Health check configuration for waiting until service is ready
|
|
29
|
-
*/
|
|
30
|
-
export declare const startDockerComposeServicesPromise: ({ composeFilePath, cwd, serviceName, waitForLog, healthCheck, env, forwardLogs, }: StartDockerComposeServicesArgs) => Promise<void>;
|
|
31
|
-
export {};
|
|
32
|
-
//# sourceMappingURL=vitest-docker-compose-setup.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"vitest-docker-compose-setup.d.ts","sourceRoot":"","sources":["../../src/node/vitest-docker-compose-setup.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA;AAG9D,eAAO,MAAM,0BAA0B,GAAI,MAAM,8BAA8B,6CAIvB,CAAA;AAExD,KAAK,8BAA8B,GAAG;IACpC,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,GAAG,EAAE,MAAM,CAAA;IACX,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC5B,WAAW,CAAC,EAAE;QACZ,GAAG,EAAE,MAAM,CAAA;QACX,cAAc,CAAC,EAAE,MAAM,CAAA;QACvB,WAAW,CAAC,EAAE,MAAM,CAAA;QACpB,OAAO,CAAC,EAAE,MAAM,CAAA;KACjB,CAAA;IACD,qBAAqB;IACrB,WAAW,CAAC,EAAE,OAAO,CAAA;CACtB,CAAA;AAED,eAAO,MAAM,uBAAuB,GAAI,2BAGrC,IAAI,CAAC,8BAA8B,EAAE,iBAAiB,GAAG,KAAK,CAAC,kKAIb,CAAA;AAErD;;;;;;;;;;GAUG;AAGH,eAAO,MAAM,iCAAiC,GAAU,mFAQrD,8BAA8B,kBA4HhC,CAAA"}
|