@@ -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 ;
@@ -46,11 +48,29 @@ export interface RedisServerDocker {
4648 dockerId : string ;
4749}
4850
49- async function spawnRedisServerDocker ( { image, version } : RedisServerDockerConfig , serverArguments : Array < string > ) : Promise < RedisServerDocker > {
50- const port = ( await portIterator . next ( ) ) . value ,
51- { stdout, stderr } = await execAsync (
52- `docker run -e REDIS_ARGS="--port ${ port . toString ( ) } ${ serverArguments . join ( ' ' ) } " -d --network host ${ image } :${ version } `
53- ) ;
51+ async function spawnRedisServerDocker ( {
52+ image,
53+ version
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+ ] ;
66+
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 ) ;
5474
5575 if ( ! stdout ) {
5676 throw new Error ( `docker run error - ${ stderr } ` ) ;
@@ -65,7 +85,6 @@ async function spawnRedisServerDocker({ image, version }: RedisServerDockerConfi
6585 dockerId : stdout . trim ( )
6686 } ;
6787}
68-
6988const RUNNING_SERVERS = new Map < Array < string > , ReturnType < typeof spawnRedisServerDocker > > ( ) ;
7089
7190export function spawnRedisServer ( dockerConfig : RedisServerDockerConfig , serverArguments : Array < string > ) : Promise < RedisServerDocker > {
@@ -80,7 +99,7 @@ export function spawnRedisServer(dockerConfig: RedisServerDockerConfig, serverAr
8099}
81100
82101async function dockerRemove ( dockerId : string ) : Promise < void > {
83- const { stderr } = await execAsync ( ` docker rm -f ${ dockerId } ` ) ;
102+ const { stderr } = await execAsync ( ' docker' , [ 'rm' , '-f' , dockerId ] ) ;
84103 if ( stderr ) {
85104 throw new Error ( `docker rm error - ${ stderr } ` ) ;
86105 }
@@ -132,15 +151,15 @@ async function spawnRedisClusterNodeDockers(
132151 '5000'
133152 ] , clientConfig ) . then ( async replica => {
134153
135- const requirePassIndex = serverArguments . findIndex ( ( x ) => x === '--requirepass' ) ;
136- if ( requirePassIndex !== - 1 ) {
137- const password = serverArguments [ requirePassIndex + 1 ] ;
138- 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 } )
139158 }
140159 await replica . client . clusterMeet ( '127.0.0.1' , master . docker . port ) ;
141160
142161 while ( ( await replica . client . clusterSlots ( ) ) . length === 0 ) {
143- await setTimeout ( 50 ) ;
162+ await setTimeout ( 25 ) ;
144163 }
145164
146165 await replica . client . clusterReplicate (
@@ -224,7 +243,7 @@ async function spawnRedisClusterDockers(
224243 while (
225244 totalNodes ( await client . clusterSlots ( ) ) !== nodes . length ||
226245 ! ( await client . sendCommand < string > ( [ 'CLUSTER' , 'INFO' ] ) ) . startsWith ( 'cluster_state:ok' ) // TODO
227- ) {
246+ ) {
228247 await setTimeout ( 50 ) ;
229248 }
230249
@@ -257,7 +276,7 @@ export function spawnRedisCluster(
257276 return runningCluster ;
258277 }
259278
260- const dockersPromise = spawnRedisClusterDockers ( dockersConfig , serverArguments , clientConfig ) ;
279+ const dockersPromise = spawnRedisClusterDockers ( dockersConfig , serverArguments , clientConfig ) ;
261280
262281 RUNNING_CLUSTERS . set ( serverArguments , dockersPromise ) ;
263282 return dockersPromise ;
0 commit comments