@@ -86,40 +86,37 @@ type TarantoolInstance struct {
8686 // Dialer to check that connection established.
8787 Dialer tarantool.Dialer
8888
89- done chan error
89+ is_stopping bool
9090 is_done bool
9191 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- }
11092}
11193
11294func (t * TarantoolInstance ) checkDone () {
11395 t .is_done = false
11496 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
11799 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 )
120101 }
121102}
122103
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+
123120func isReady (dialer tarantool.Dialer , opts * tarantool.Opts ) error {
124121 var err error
125122 var conn * tarantool.Connection
@@ -327,15 +324,16 @@ func StartTarantool(startOpts StartOpts) (TarantoolInstance, error) {
327324 }
328325 }
329326
330- working , err_st := inst .Status ()
331- if ! working || err_st != nil {
327+ if inst .is_done && inst .result != nil {
332328 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 )
334331 }
335332
336333 if err != nil {
337334 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 )
339337 }
340338
341339 return inst , nil
@@ -345,25 +343,9 @@ func StartTarantool(startOpts StartOpts) (TarantoolInstance, error) {
345343// with StartTarantool. Waits until any resources
346344// associated with the process is released. If something went wrong, fails.
347345func 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 )
367349 }
368350}
369351
0 commit comments