github.com/drone/runner-go@v1.12.0/pipeline/reporter/remote/remote.go (about) 1 // Copyright 2019 Drone.IO Inc. All rights reserved. 2 // Use of this source code is governed by the Polyform License 3 // that can be found in the LICENSE file. 4 5 // Package remote provides a reporter and streamer that sends the 6 // pipeline status and logs to the central server. 7 package remote 8 9 import ( 10 "context" 11 "io" 12 13 "github.com/drone/runner-go/client" 14 "github.com/drone/runner-go/internal" 15 "github.com/drone/runner-go/livelog" 16 "github.com/drone/runner-go/pipeline" 17 ) 18 19 var _ pipeline.Reporter = (*Remote)(nil) 20 var _ pipeline.Streamer = (*Remote)(nil) 21 22 // Remote implements a pipeline reporter that reports state 23 // changes and results to a remote server instance. 24 type Remote struct { 25 client client.Client 26 } 27 28 // New returns a remote reporter. 29 func New(client client.Client) *Remote { 30 return &Remote{ 31 client: client, 32 } 33 } 34 35 // ReportStage reports the stage status. 36 func (s *Remote) ReportStage(ctx context.Context, state *pipeline.State) error { 37 state.Lock() 38 src := state.Stage 39 cpy := internal.CloneStage(src) 40 state.Unlock() 41 err := s.client.Update(ctx, cpy) 42 if err == nil { 43 state.Lock() 44 internal.MergeStage(cpy, src) 45 state.Unlock() 46 } 47 return err 48 } 49 50 // ReportStep reports the step status. 51 func (s *Remote) ReportStep(ctx context.Context, state *pipeline.State, name string) error { 52 src := state.Find(name) 53 state.Lock() 54 cpy := internal.CloneStep(src) 55 state.Unlock() 56 err := s.client.UpdateStep(ctx, cpy) 57 if err == nil { 58 state.Lock() 59 internal.MergeStep(cpy, src) 60 state.Unlock() 61 } 62 return err 63 } 64 65 // Stream returns an io.WriteCloser to stream the stdout 66 // and stderr of the pipeline step to the server. 67 func (s *Remote) Stream(ctx context.Context, state *pipeline.State, name string) io.WriteCloser { 68 src := state.Find(name) 69 return livelog.New(s.client, src.ID) 70 }