@@ -16,44 +16,57 @@ class RedisServerController: NSObject
1616
1717 func launchRedisServer( )
1818 {
19- let bundle = Bundle . main
20-
21- guard let redisConfigPath = bundle. path ( forResource: " redis " , ofType: " conf " )
22- else
23- {
24- print ( " Unable to launch Redis server: could not find terraform executable. " )
25- return
26- }
27-
28- guard let redisPath = bundle. path ( forResource: " redis-server " , ofType: nil )
29- else
19+ isRedisServerRunning
3020 {
31- print ( " Unable to launch Redis server: could not find terraform executable. " )
32- return
33- }
34-
35- guard let redisModulePath = bundle. path ( forResource: " subsequences " , ofType: " so " )
36- else
37- {
38- print ( " Unable to launch Redis server: could not find the needed module. " )
39- return
40- }
41-
42- guard let path = bundle. path ( forResource: " LaunchRedisServerScript " , ofType: " sh " )
21+ ( serverIsRunning) in
22+
23+ if serverIsRunning
24+ {
25+ return
26+ }
4327 else
44- {
45- print ( " Unable to launch Redis server. Could not find the script. " )
46- return
28+ {
29+ let bundle = Bundle . main
30+
31+ guard let redisConfigPath = bundle. path ( forResource: " redis " , ofType: " conf " )
32+ else
33+ {
34+ print ( " Unable to launch Redis server: could not find terraform executable. " )
35+ return
36+ }
37+
38+ guard let redisPath = bundle. path ( forResource: " redis-server " , ofType: nil )
39+ else
40+ {
41+ print ( " Unable to launch Redis server: could not find terraform executable. " )
42+ return
43+ }
44+
45+ guard let redisModulePath = bundle. path ( forResource: " subsequences " , ofType: " so " )
46+ else
47+ {
48+ print ( " Unable to launch Redis server: could not find the needed module. " )
49+ return
50+ }
51+
52+ guard let path = bundle. path ( forResource: " LaunchRedisServerScript " , ofType: " sh " )
53+ else
54+ {
55+ print ( " Unable to launch Redis server. Could not find the script. " )
56+ return
57+ }
58+
59+ print ( " \n 👇👇 Running Script 👇👇: \n " )
60+
61+ self . runRedisScript ( path: path, arguments: [ redisPath, redisConfigPath, redisModulePath] )
62+ {
63+ ( hasCompleted) in
64+
65+ print ( " 🚀 Launch Redis Server Script Complete 🚀 " )
66+ }
67+ }
4768 }
4869
49- print ( " \n 👇👇 Running Script 👇👇: \n " )
50-
51- runRedisScript ( path: path, arguments: [ redisPath, redisConfigPath, redisModulePath] )
52- {
53- ( hasCompleted) in
54-
55- print ( " 🚀 Launch Redis Server Script Complete 🚀 " )
56- }
5770 }
5871
5972 func shutdownRedisServer( )
@@ -90,6 +103,52 @@ class RedisServerController: NSObject
90103 }
91104 }
92105
106+ func isRedisServerRunning( completion: @escaping ( _ completion: Bool ) -> Void )
107+ {
108+ guard let redisCliPath = Bundle . main. path ( forResource: " redis-cli " , ofType: nil )
109+ else
110+ {
111+ print ( " Unable to ping Redis server. Could not find terraform executable. " )
112+ completion ( false )
113+ return
114+ }
115+ guard let path = Bundle . main. path ( forResource: " CheckRedisServerScript " , ofType: " sh " )
116+ else
117+ {
118+ print ( " Unable to ping Redis server. Could not find the script. " )
119+ completion ( false )
120+ return
121+ }
122+
123+ let process = Process ( )
124+ process. launchPath = path
125+ process. arguments = [ redisCliPath]
126+ let pipe = Pipe ( )
127+ process. standardOutput = pipe
128+ process. terminationHandler =
129+ {
130+ ( task) in
131+ // Get the data
132+ let data = pipe. fileHandleForReading. readDataToEndOfFile ( )
133+ let output = NSString ( data: data, encoding: String . Encoding. utf8. rawValue)
134+
135+ print ( output ?? " no output " )
136+
137+ if output == " PONG \n "
138+ {
139+ print ( " \n We received a pong, server is already running!! " )
140+ completion ( true )
141+ }
142+ else
143+ {
144+ print ( " \n No Pong, launch the server!! " )
145+ completion ( false )
146+ }
147+ }
148+ process. waitUntilExit ( )
149+ process. launch ( )
150+ }
151+
93152 func runRedisScript( path: String , arguments: [ String ] ? , completion: @escaping ( _ completion: Bool ) -> Void )
94153 {
95154 let processQueue = DispatchQueue . global ( qos: . background)
0 commit comments