github.com/dnephin/dobi@v0.15.0/tasks/compose/attach.go (about)

     1  package compose
     2  
     3  import (
     4  	"os"
     5  	"os/signal"
     6  	"syscall"
     7  
     8  	"github.com/dnephin/dobi/tasks/context"
     9  	log "github.com/sirupsen/logrus"
    10  )
    11  
    12  // RunUpAttached starts the Compose project
    13  func RunUpAttached(ctx *context.ExecuteContext, t *Task) error {
    14  	t.logger().Info("project up")
    15  
    16  	cmd := t.buildCommand("up", "-t", t.config.StopGraceString())
    17  	if err := cmd.Start(); err != nil {
    18  		return err
    19  	}
    20  
    21  	chanSig := forwardSignals(t, cmd.Process)
    22  	defer signal.Stop(chanSig)
    23  
    24  	if err := cmd.Wait(); err != nil {
    25  		return err
    26  	}
    27  	t.logger().Info("Done")
    28  	return nil
    29  }
    30  
    31  func forwardSignals(t *Task, proc *os.Process) chan<- os.Signal {
    32  	chanSig := make(chan os.Signal, 128)
    33  
    34  	// TODO: not all of these exist on windows?
    35  	signal.Notify(chanSig, syscall.SIGINT, syscall.SIGTERM)
    36  
    37  	kill := func(sig os.Signal) {
    38  		t.logger().WithFields(log.Fields{"signal": sig}).Debug("received")
    39  
    40  		if err := proc.Signal(sig); err != nil {
    41  			t.logger().WithFields(log.Fields{"pid": proc.Pid}).Warnf(
    42  				"failed to signal process: %s", err)
    43  		}
    44  	}
    45  
    46  	go func() {
    47  		for sig := range chanSig {
    48  			kill(sig)
    49  		}
    50  	}()
    51  	return chanSig
    52  }