@@ -4,9 +4,11 @@ import { once } from 'node:events';
4
4
import { createClient } from '@redis/client/index' ;
5
5
import { setTimeout } from 'node:timers/promises' ;
6
6
// import { ClusterSlotsReply } from '@redis/client/dist/lib/commands/CLUSTER_SLOTS';
7
+
8
+ import { execFile as execFileCallback } from 'node:child_process' ;
7
9
import { promisify } from 'node:util' ;
8
- import { exec } from 'node:child_process' ;
9
- const execAsync = promisify ( exec ) ;
10
+
11
+ const execAsync = promisify ( execFileCallback ) ;
10
12
11
13
interface ErrorWithCode extends Error {
12
14
code : string ;
@@ -50,10 +52,25 @@ async function spawnRedisServerDocker({
50
52
image,
51
53
version
52
54
} : RedisServerDockerConfig , serverArguments : Array < string > ) : Promise < RedisServerDocker > {
55
+ const port = ( await portIterator . next ( ) ) . value ;
56
+ const portStr = port . toString ( ) ;
57
+
58
+ const dockerArgs = [
59
+ 'run' ,
60
+ '-e' , `PORT=${ portStr } ` ,
61
+ '-d' ,
62
+ '--network' , 'host' ,
63
+ `${ image } :${ version } ` ,
64
+ '--port' , portStr
65
+ ] ;
53
66
54
- const port = ( await portIterator . next ( ) ) . value
55
- const command = `docker run -e PORT="${ port . toString ( ) } " -d --network host ${ image } :${ version } --port ${ port . toString ( ) } ${ serverArguments . join ( ' ' ) } `
56
- const { stdout, stderr } = await execAsync ( command ) ;
67
+ if ( serverArguments . length > 0 ) {
68
+ dockerArgs . push ( ...serverArguments ) ;
69
+ }
70
+
71
+ console . log ( `[Docker] Spawning Redis container - Image: ${ image } :${ version } , Port: ${ port } ` ) ;
72
+
73
+ const { stdout, stderr } = await execAsync ( 'docker' , dockerArgs ) ;
57
74
58
75
if ( ! stdout ) {
59
76
throw new Error ( `docker run error - ${ stderr } ` ) ;
@@ -68,7 +85,6 @@ async function spawnRedisServerDocker({
68
85
dockerId : stdout . trim ( )
69
86
} ;
70
87
}
71
-
72
88
const RUNNING_SERVERS = new Map < Array < string > , ReturnType < typeof spawnRedisServerDocker > > ( ) ;
73
89
74
90
export function spawnRedisServer ( dockerConfig : RedisServerDockerConfig , serverArguments : Array < string > ) : Promise < RedisServerDocker > {
@@ -83,7 +99,7 @@ export function spawnRedisServer(dockerConfig: RedisServerDockerConfig, serverAr
83
99
}
84
100
85
101
async function dockerRemove ( dockerId : string ) : Promise < void > {
86
- const { stderr } = await execAsync ( ` docker rm -f ${ dockerId } ` ) ;
102
+ const { stderr } = await execAsync ( ' docker' , [ 'rm' , '-f' , dockerId ] ) ;
87
103
if ( stderr ) {
88
104
throw new Error ( `docker rm error - ${ stderr } ` ) ;
89
105
}
@@ -135,10 +151,10 @@ async function spawnRedisClusterNodeDockers(
135
151
'5000'
136
152
] , clientConfig ) . then ( async replica => {
137
153
138
- const requirePassIndex = serverArguments . findIndex ( ( x ) => x === '--requirepass' ) ;
139
- if ( requirePassIndex !== - 1 ) {
140
- const password = serverArguments [ requirePassIndex + 1 ] ;
141
- await replica . client . configSet ( { 'masterauth' : password } )
154
+ const requirePassIndex = serverArguments . findIndex ( ( x ) => x === '--requirepass' ) ;
155
+ if ( requirePassIndex !== - 1 ) {
156
+ const password = serverArguments [ requirePassIndex + 1 ] ;
157
+ await replica . client . configSet ( { 'masterauth' : password } )
142
158
}
143
159
await replica . client . clusterMeet ( '127.0.0.1' , master . docker . port ) ;
144
160
@@ -227,7 +243,7 @@ async function spawnRedisClusterDockers(
227
243
while (
228
244
totalNodes ( await client . clusterSlots ( ) ) !== nodes . length ||
229
245
! ( await client . sendCommand < string > ( [ 'CLUSTER' , 'INFO' ] ) ) . startsWith ( 'cluster_state:ok' ) // TODO
230
- ) {
246
+ ) {
231
247
await setTimeout ( 50 ) ;
232
248
}
233
249
@@ -260,7 +276,7 @@ export function spawnRedisCluster(
260
276
return runningCluster ;
261
277
}
262
278
263
- const dockersPromise = spawnRedisClusterDockers ( dockersConfig , serverArguments , clientConfig ) ;
279
+ const dockersPromise = spawnRedisClusterDockers ( dockersConfig , serverArguments , clientConfig ) ;
264
280
265
281
RUNNING_CLUSTERS . set ( serverArguments , dockersPromise ) ;
266
282
return dockersPromise ;
0 commit comments