github.com/getgauge/gauge@v1.6.9/runner/multithreadedRunner.go (about)

     1  /*----------------------------------------------------------------
     2   *  Copyright (c) ThoughtWorks, Inc.
     3   *  Licensed under the Apache License, Version 2.0
     4   *  See LICENSE in the project root for license information.
     5   *----------------------------------------------------------------*/
     6  
     7  package runner
     8  
     9  import (
    10  	"net"
    11  	"time"
    12  
    13  	"github.com/getgauge/gauge-proto/go/gauge_messages"
    14  	"github.com/getgauge/gauge/config"
    15  	"github.com/getgauge/gauge/conn"
    16  	"github.com/getgauge/gauge/logger"
    17  )
    18  
    19  type MultithreadedRunner struct {
    20  	r *LegacyRunner
    21  }
    22  
    23  func (r *MultithreadedRunner) Alive() bool {
    24  	if r.r.mutex != nil && r.r.Cmd != nil {
    25  		return r.r.Alive()
    26  	}
    27  	return false
    28  }
    29  
    30  func (r *MultithreadedRunner) IsMultithreaded() bool {
    31  	return false
    32  }
    33  
    34  func (r *MultithreadedRunner) SetConnection(c net.Conn) {
    35  	r.r = &LegacyRunner{connection: c}
    36  }
    37  
    38  func (r *MultithreadedRunner) Kill() error {
    39  	defer r.r.connection.Close()
    40  	err := conn.SendProcessKillMessage(r.r.connection)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	exited := make(chan bool, 1)
    45  	go func() {
    46  		for {
    47  			if r.Alive() {
    48  				time.Sleep(100 * time.Millisecond)
    49  			} else {
    50  				exited <- true
    51  				return
    52  			}
    53  		}
    54  	}()
    55  
    56  	select {
    57  	case done := <-exited:
    58  		if done {
    59  			return nil
    60  		}
    61  	case <-time.After(config.PluginKillTimeout()):
    62  		return r.killRunner()
    63  	}
    64  	return nil
    65  }
    66  
    67  func (r *MultithreadedRunner) Connection() net.Conn {
    68  	return r.r.connection
    69  }
    70  
    71  func (r *MultithreadedRunner) killRunner() error {
    72  	if r.r.Cmd != nil && r.r.Cmd.Process != nil {
    73  		logger.Warningf(true, "Killing runner with PID:%d forcefully", r.r.Cmd.Process.Pid)
    74  		return r.r.Cmd.Process.Kill()
    75  	}
    76  	return nil
    77  }
    78  
    79  // Info gives the information about runner
    80  func (r *MultithreadedRunner) Info() *RunnerInfo {
    81  	return r.r.Info()
    82  }
    83  
    84  func (r *MultithreadedRunner) Pid() int {
    85  	return -1
    86  }
    87  
    88  func (r *MultithreadedRunner) ExecuteAndGetStatus(message *gauge_messages.Message) *gauge_messages.ProtoExecutionResult {
    89  	return r.r.ExecuteAndGetStatus(message)
    90  }
    91  
    92  func (r *MultithreadedRunner) ExecuteMessageWithTimeout(message *gauge_messages.Message) (*gauge_messages.Message, error) {
    93  	r.r.EnsureConnected()
    94  	return conn.GetResponseForMessageWithTimeout(message, r.r.Connection(), config.RunnerRequestTimeout())
    95  }