@@ -86,40 +86,37 @@ type TarantoolInstance struct {
86
86
// Dialer to check that connection established.
87
87
Dialer tarantool.Dialer
88
88
89
- done chan error
89
+ is_stopping bool
90
90
is_done bool
91
91
result error
92
- is_stopping bool
93
- }
94
-
95
- // Status checks if Tarantool instance is still running.
96
- // Return true if it is running, false if it is not.
97
- // If instance was exit and error is nil - process completed success with zero status code.
98
- func (t * TarantoolInstance ) Status () (bool , error ) {
99
- if t .is_done {
100
- return false , t .result
101
- }
102
-
103
- select {
104
- case t .result = <- t .done :
105
- t .is_done = true
106
- return false , t .result
107
- default :
108
- return true , nil
109
- }
110
92
}
111
93
112
94
func (t * TarantoolInstance ) checkDone () {
113
95
t .is_done = false
114
96
t .is_stopping = false
115
- t .done = make ( chan error , 1 )
116
- t .done <- t . Cmd . Wait ()
97
+ t .result = t . Cmd . Wait ( )
98
+ t .is_done = true
117
99
if ! t .is_stopping {
118
- _ , err := t .Status ()
119
- log .Printf ("Tarantool was unexpected terminated: %s" , err )
100
+ log .Printf ("Tarantool %q was unexpected terminated: %s" , t .Opts .Listen , t .result )
120
101
}
121
102
}
122
103
104
+ func (t * TarantoolInstance ) Stop () error {
105
+ log .Printf ("Stopping Tarantool instance %q" , t .Opts .Listen )
106
+ t .is_stopping = true
107
+ if t .is_done {
108
+ return nil
109
+ }
110
+ if t .Cmd != nil && t .Cmd .Process != nil {
111
+ if err := t .Cmd .Process .Kill (); err != nil && ! t .is_done {
112
+ return fmt .Errorf ("failed to kill tarantool %q (pid %d), got %s" ,
113
+ t .Opts .Listen , t .Cmd .Process .Pid , err )
114
+ }
115
+ }
116
+ t .Cmd .Process = nil
117
+ return nil
118
+ }
119
+
123
120
func isReady (dialer tarantool.Dialer , opts * tarantool.Opts ) error {
124
121
var err error
125
122
var conn * tarantool.Connection
@@ -327,15 +324,16 @@ func StartTarantool(startOpts StartOpts) (TarantoolInstance, error) {
327
324
}
328
325
}
329
326
330
- working , err_st := inst .Status ()
331
- if ! working || err_st != nil {
327
+ if inst .is_done && inst .result != nil {
332
328
StopTarantool (inst )
333
- return TarantoolInstance {}, fmt .Errorf ("unexpected terminated Tarantool: %w" , err_st )
329
+ return TarantoolInstance {}, fmt .Errorf ("unexpected terminated Tarantool %q: %w" ,
330
+ inst .Opts .Listen , inst .result )
334
331
}
335
332
336
333
if err != nil {
337
334
StopTarantool (inst )
338
- return TarantoolInstance {}, fmt .Errorf ("failed to connect Tarantool: %w" , err )
335
+ return TarantoolInstance {}, fmt .Errorf ("failed to connect Tarantool %q: %w" ,
336
+ inst .Opts .Listen , err )
339
337
}
340
338
341
339
return inst , nil
@@ -345,25 +343,9 @@ func StartTarantool(startOpts StartOpts) (TarantoolInstance, error) {
345
343
// with StartTarantool. Waits until any resources
346
344
// associated with the process is released. If something went wrong, fails.
347
345
func StopTarantool (inst TarantoolInstance ) {
348
- log .Printf ("Stopping Tarantool instance" )
349
- inst .is_stopping = true
350
- if inst .Cmd != nil && inst .Cmd .Process != nil {
351
- if err := inst .Cmd .Process .Kill (); err != nil {
352
- is_running , _ := inst .Status ()
353
- if is_running {
354
- log .Fatalf ("Failed to kill tarantool (pid %d), got %s" , inst .Cmd .Process .Pid , err )
355
- }
356
- }
357
-
358
- // Wait releases any resources associated with the Process.
359
- if _ , err := inst .Cmd .Process .Wait (); err != nil {
360
- is_running , _ := inst .Status ()
361
- if is_running {
362
- log .Fatalf ("Failed to wait for Tarantool process to exit, got %s" , err )
363
- }
364
- }
365
-
366
- inst .Cmd .Process = nil
346
+ err := inst .Stop ()
347
+ if err != nil {
348
+ log .Fatal (err )
367
349
}
368
350
}
369
351
0 commit comments