@@ -4,9 +4,11 @@ import { once } from 'node:events';
44import { createClient } from '@redis/client/index' ;
55import { setTimeout } from 'node:timers/promises' ;
66// import { ClusterSlotsReply } from '@redis/client/dist/lib/commands/CLUSTER_SLOTS';
7+
8+ import { execFile as execFileCallback } from 'node:child_process' ;
79import { promisify } from 'node:util' ;
8- import { exec } from 'node:child_process' ;
9- const execAsync = promisify ( exec ) ;
10+
11+ const execAsync = promisify ( execFileCallback ) ;
1012
1113interface ErrorWithCode extends Error {
1214 code : string ;
@@ -50,10 +52,25 @@ async function spawnRedisServerDocker({
5052 image,
5153 version
5254} : 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+ ] ;
5366
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 ) ;
5774
5875 if ( ! stdout ) {
5976 throw new Error ( `docker run error - ${ stderr } ` ) ;
@@ -68,7 +85,6 @@ async function spawnRedisServerDocker({
6885 dockerId : stdout . trim ( )
6986 } ;
7087}
71-
7288const RUNNING_SERVERS = new Map < Array < string > , ReturnType < typeof spawnRedisServerDocker > > ( ) ;
7389
7490export function spawnRedisServer ( dockerConfig : RedisServerDockerConfig , serverArguments : Array < string > ) : Promise < RedisServerDocker > {
@@ -83,7 +99,7 @@ export function spawnRedisServer(dockerConfig: RedisServerDockerConfig, serverAr
8399}
84100
85101async function dockerRemove ( dockerId : string ) : Promise < void > {
86- const { stderr } = await execAsync ( ` docker rm -f ${ dockerId } ` ) ;
102+ const { stderr } = await execAsync ( ' docker' , [ 'rm' , '-f' , dockerId ] ) ;
87103 if ( stderr ) {
88104 throw new Error ( `docker rm error - ${ stderr } ` ) ;
89105 }
@@ -135,10 +151,10 @@ async function spawnRedisClusterNodeDockers(
135151 '5000'
136152 ] , clientConfig ) . then ( async replica => {
137153
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 } )
142158 }
143159 await replica . client . clusterMeet ( '127.0.0.1' , master . docker . port ) ;
144160
@@ -227,7 +243,7 @@ async function spawnRedisClusterDockers(
227243 while (
228244 totalNodes ( await client . clusterSlots ( ) ) !== nodes . length ||
229245 ! ( await client . sendCommand < string > ( [ 'CLUSTER' , 'INFO' ] ) ) . startsWith ( 'cluster_state:ok' ) // TODO
230- ) {
246+ ) {
231247 await setTimeout ( 50 ) ;
232248 }
233249
@@ -260,7 +276,7 @@ export function spawnRedisCluster(
260276 return runningCluster ;
261277 }
262278
263- const dockersPromise = spawnRedisClusterDockers ( dockersConfig , serverArguments , clientConfig ) ;
279+ const dockersPromise = spawnRedisClusterDockers ( dockersConfig , serverArguments , clientConfig ) ;
264280
265281 RUNNING_CLUSTERS . set ( serverArguments , dockersPromise ) ;
266282 return dockersPromise ;
0 commit comments