Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Wait for the terminal server connection instead of a session + provid…
Browse files Browse the repository at this point in the history
…e task's identification (#194)

* Wait for the terminal server connection instead of a session

* Provide task's identification when sending terminal lifecycle events

* Bump github.com/cirruslabs/terminal to fix compilation on Windows

* Bump github.com/cirruslabs/terminal to 0.11.0
  • Loading branch information
edigaryev committed Dec 6, 2021
1 parent 96a7df1 commit 73166ba
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/bmatcuk/doublestar v1.3.4
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d
github.com/cirruslabs/cirrus-ci-annotations v0.7.0
github.com/cirruslabs/terminal v0.10.1
github.com/cirruslabs/terminal v0.11.0
github.com/dustin/go-humanize v1.0.0
github.com/go-git/go-git/v5 v5.4.2
github.com/golang/protobuf v1.5.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ github.com/cirruslabs/cirrus-ci-annotations v0.3.0/go.mod h1:xrmxzL58Pf4cSSQCmQE
github.com/cirruslabs/cirrus-ci-annotations v0.7.0 h1:tEtvcCcYgKU/NOCcppMizLwmTmNMTXoi7fqMUE/XOuw=
github.com/cirruslabs/cirrus-ci-annotations v0.7.0/go.mod h1:xrmxzL58Pf4cSSQCmQEOPGQ3poeARxJdHneurUrqjZk=
github.com/cirruslabs/terminal v0.2.5/go.mod h1:ubLe9fvd4FYeQV08ob5TNp9tsAfE0efkFtPrlKAwIKw=
github.com/cirruslabs/terminal v0.10.1 h1:0FrTDHxe9f7oVVQzeiHzoevnzuinGvT4CdtNhQ32C7g=
github.com/cirruslabs/terminal v0.10.1/go.mod h1:5nVkiTg4y2ugDJjchuc9Lyz1cxu+KugFiJyuiESaYiY=
github.com/cirruslabs/terminal v0.11.0 h1:iWnyoogTA9BreM7kL0iGaZuQ6ASH0eC45Vz1QEm3FoE=
github.com/cirruslabs/terminal v0.11.0/go.mod h1:5nVkiTg4y2ugDJjchuc9Lyz1cxu+KugFiJyuiESaYiY=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
Expand Down
26 changes: 15 additions & 11 deletions internal/executor/terminalwrapper/terminalwrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import (
)

type Wrapper struct {
ctx context.Context
operationChan chan Operation
terminalHost *host.TerminalHost
expirationWindow time.Duration
ctx context.Context
taskIdentification *api.TaskIdentification
operationChan chan Operation
terminalHost *host.TerminalHost
expirationWindow time.Duration
}

func New(
Expand All @@ -29,9 +30,10 @@ func New(
shellEnv []string,
) *Wrapper {
wrapper := &Wrapper{
ctx: ctx,
operationChan: make(chan Operation, 4096),
expirationWindow: expirationWindow,
ctx: ctx,
taskIdentification: taskIdentification,
operationChan: make(chan Operation, 4096),
expirationWindow: expirationWindow,
}

// A trusted secret that grants ability to spawn shells on the terminal host we start below
Expand All @@ -54,6 +56,7 @@ func New(
}

_, err = client.CirrusClient.ReportTerminalLifecycle(wrapper.ctx, &api.ReportTerminalLifecycleRequest{
TaskIdentification: wrapper.taskIdentification,
Lifecycle: &api.ReportTerminalLifecycleRequest_Started_{
Started: &api.ReportTerminalLifecycleRequest_Started{},
},
Expand Down Expand Up @@ -113,7 +116,7 @@ func (wrapper *Wrapper) Wait() chan Operation {
}

// Wait for the terminal to connect, exit on ctx cancellation/deadline
if !wrapper.waitForSession() {
if !wrapper.waitForConnection() {
return
}

Expand All @@ -129,6 +132,7 @@ func (wrapper *Wrapper) Wait() chan Operation {

// Notify the server that the countdown has started
_, err := client.CirrusClient.ReportTerminalLifecycle(wrapper.ctx, &api.ReportTerminalLifecycleRequest{
TaskIdentification: wrapper.taskIdentification,
Lifecycle: &api.ReportTerminalLifecycleRequest_Expiring_{
Expiring: &api.ReportTerminalLifecycleRequest_Expiring{},
},
Expand Down Expand Up @@ -166,9 +170,9 @@ func (wrapper *Wrapper) Wait() chan Operation {
return wrapper.operationChan
}

func (wrapper *Wrapper) waitForSession() bool {
func (wrapper *Wrapper) waitForConnection() bool {
wrapper.operationChan <- &LogOperation{
Message: "Waiting for the terminal session to be established...",
Message: "Waiting for the terminal server connection to be established...",
}

ticker := time.NewTicker(1 * time.Second)
Expand All @@ -178,7 +182,7 @@ func (wrapper *Wrapper) waitForSession() bool {
select {
case <-ticker.C:
defaultTime := time.Time{}
if wrapper.terminalHost.LastRegistration() != defaultTime {
if wrapper.terminalHost.LastConnection() != defaultTime {
return true
}
case <-wrapper.ctx.Done():
Expand Down

0 comments on commit 73166ba

Please sign in to comment.