github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/worker/runtime/process.go (about)

     1  package runtime
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"code.cloudfoundry.org/garden"
     8  	"github.com/containerd/containerd"
     9  )
    10  
    11  type Process struct {
    12  	process     containerd.Process
    13  	exitStatusC <-chan containerd.ExitStatus
    14  }
    15  
    16  func NewProcess(
    17  	p containerd.Process,
    18  	ch <-chan containerd.ExitStatus,
    19  ) *Process {
    20  	return &Process{
    21  		process:     p,
    22  		exitStatusC: ch,
    23  	}
    24  }
    25  
    26  var _ garden.Process = (*Process)(nil)
    27  
    28  // Id retrieves the ID associated with this process.
    29  //
    30  func (p *Process) ID() string {
    31  	return p.process.ID()
    32  }
    33  
    34  // Wait for the process to terminate (either naturally, or from a signal), and
    35  // once done, delete it.
    36  //
    37  func (p *Process) Wait() (int, error) {
    38  	status := <-p.exitStatusC
    39  	err := status.Error()
    40  	if err != nil {
    41  		return 0, fmt.Errorf("waiting for exit status: %w", err)
    42  	}
    43  
    44  	_, err = p.process.Delete(context.Background())
    45  	if err != nil {
    46  		return 0, fmt.Errorf("delete process: %w", err)
    47  	}
    48  
    49  	p.process.IO().Wait()
    50  
    51  	return int(status.ExitCode()), nil
    52  }
    53  
    54  // SetTTY resizes the process' terminal dimensions.
    55  //
    56  func (p *Process) SetTTY(spec garden.TTYSpec) error {
    57  	if spec.WindowSize == nil {
    58  		return nil
    59  	}
    60  
    61  	err := p.process.Resize(context.Background(),
    62  		uint32(spec.WindowSize.Columns),
    63  		uint32(spec.WindowSize.Rows),
    64  	)
    65  	if err != nil {
    66  		return fmt.Errorf("resize: %w", err)
    67  	}
    68  
    69  	return nil
    70  }
    71  
    72  // Signal - Not Implemented
    73  //
    74  func (p *Process) Signal(signal garden.Signal) (err error) {
    75  	err = ErrNotImplemented
    76  	return
    77  }