6
6
using System . Diagnostics ;
7
7
using System . IO ;
8
8
using System . Runtime . InteropServices ;
9
+ using System . Threading ;
9
10
using Microsoft . Extensions . Logging ;
10
11
using Microsoft . Extensions . Logging . Abstractions ;
11
12
@@ -70,6 +71,29 @@ private static string GetDockerLocation()
70
71
return null ;
71
72
}
72
73
74
+ private void StartRedis ( ILogger logger )
75
+ {
76
+ try
77
+ {
78
+ Run ( ) ;
79
+ }
80
+ catch ( Exception ex )
81
+ {
82
+ logger . LogError ( ex , "Error starting redis docker container, retrying." ) ;
83
+ Thread . Sleep ( 1000 ) ;
84
+ Run ( ) ;
85
+ }
86
+
87
+ void Run ( )
88
+ {
89
+ // create and run docker container, remove automatically when stopped, map 6379 from the container to 6379 localhost
90
+ // use static name 'redisTestContainer' so if the container doesn't get removed we don't keep adding more
91
+ // use redis base docker image
92
+ // 30 second timeout to allow redis image to be downloaded, should be a rare occurrence, only happening when a new version is released
93
+ RunProcessAndThrowIfFailed ( _path , $ "run --rm -p 6380:6379 --name { _dockerContainerName } -d redis", "redis" , logger , TimeSpan . FromSeconds ( 30 ) ) ;
94
+ }
95
+ }
96
+
73
97
public void Start ( ILogger logger )
74
98
{
75
99
logger . LogInformation ( "Starting docker container" ) ;
@@ -78,11 +102,7 @@ public void Start(ILogger logger)
78
102
RunProcessAndWait ( _path , $ "stop { _dockerMonitorContainerName } ", "docker stop" , logger , TimeSpan . FromSeconds ( 15 ) , out var _ ) ;
79
103
RunProcessAndWait ( _path , $ "stop { _dockerContainerName } ", "docker stop" , logger , TimeSpan . FromSeconds ( 15 ) , out var output ) ;
80
104
81
- // create and run docker container, remove automatically when stopped, map 6379 from the container to 6379 localhost
82
- // use static name 'redisTestContainer' so if the container doesn't get removed we don't keep adding more
83
- // use redis base docker image
84
- // 20 second timeout to allow redis image to be downloaded, should be a rare occurrence, only happening when a new version is released
85
- RunProcessAndThrowIfFailed ( _path , $ "run --rm -p 6380:6379 --name { _dockerContainerName } -d redis", "redis" , logger , TimeSpan . FromSeconds ( 20 ) ) ;
105
+ StartRedis ( logger ) ;
86
106
87
107
// inspect the redis docker image and extract the IPAddress. Necessary when running tests from inside a docker container, spinning up a new docker container for redis
88
108
// outside the current container requires linking the networks (difficult to automate) or using the IP:Port combo
0 commit comments