github.com/replicatedhq/ship@v0.55.0/pkg/lifecycle/message/api.go (about)

     1  package message
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/go-kit/kit/log"
     8  	"github.com/go-kit/kit/log/level"
     9  	"github.com/mitchellh/cli"
    10  	"github.com/pkg/errors"
    11  	"github.com/replicatedhq/ship/pkg/api"
    12  	"github.com/replicatedhq/ship/pkg/lifecycle/daemon"
    13  	"github.com/replicatedhq/ship/pkg/lifecycle/daemon/daemontypes"
    14  	"github.com/replicatedhq/ship/pkg/templates"
    15  	"github.com/spf13/viper"
    16  	"go.uber.org/dig"
    17  )
    18  
    19  type DaemonMessenger struct {
    20  	dig.In
    21  	Logger         log.Logger
    22  	UI             cli.Ui
    23  	Viper          *viper.Viper
    24  	Daemon         daemontypes.Daemon
    25  	BuilderBuilder *templates.BuilderBuilder
    26  }
    27  
    28  func (m *DaemonMessenger) Execute(ctx context.Context, release *api.Release, step *api.Message) error {
    29  	debug := level.Debug(log.With(m.Logger, "struct", "daemonmessenger", "method", "execute"))
    30  
    31  	daemonExitedChan := m.Daemon.EnsureStarted(ctx, release)
    32  
    33  	builder, err := m.getBuilder(release.Metadata)
    34  	if err != nil {
    35  		return errors.Wrap(err, "get builder")
    36  	}
    37  	built, _ := builder.String(step.Contents)
    38  
    39  	m.Daemon.PushMessageStep(ctx, daemontypes.Message{
    40  		Contents: built,
    41  		Level:    step.Level,
    42  	}, daemon.MessageActions())
    43  
    44  	debug.Log("event", "step.pushed")
    45  	return m.awaitMessageConfirmed(ctx, daemonExitedChan)
    46  }
    47  
    48  func (m *DaemonMessenger) awaitMessageConfirmed(ctx context.Context, daemonExitedChan chan error) error {
    49  	debug := level.Debug(log.With(m.Logger, "struct", "daemonmessenger", "method", "message.confirm.await"))
    50  	for {
    51  		select {
    52  		case <-ctx.Done():
    53  			debug.Log("event", "ctx.done")
    54  			return ctx.Err()
    55  		case err := <-daemonExitedChan:
    56  			debug.Log("event", "daemon.exit")
    57  			if err != nil {
    58  				return err
    59  			}
    60  			return errors.New("daemon exited")
    61  		case <-m.Daemon.MessageConfirmedChan():
    62  			debug.Log("event", "message.confirmed")
    63  			return nil
    64  		case <-time.After(10 * time.Second):
    65  			debug.Log("waitingFor", "message.confirmed")
    66  		}
    67  	}
    68  }