2017-06-28 17:21:22 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2022-02-24 14:53:44 +00:00
|
|
|
"strings"
|
2019-09-14 12:21:16 +00:00
|
|
|
"time"
|
2017-06-28 17:21:22 +00:00
|
|
|
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2017-06-28 17:21:22 +00:00
|
|
|
"google.golang.org/grpc"
|
2017-06-29 21:27:06 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
2021-09-24 14:29:26 +00:00
|
|
|
"google.golang.org/grpc/status"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2021-11-26 02:34:48 +00:00
|
|
|
backend "github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/rpc/proto"
|
2017-06-28 17:21:22 +00:00
|
|
|
)
|
|
|
|
|
2017-06-29 21:27:06 +00:00
|
|
|
var backoff = time.Second
|
|
|
|
|
2017-06-28 17:21:22 +00:00
|
|
|
type client struct {
|
2021-09-29 00:10:09 +00:00
|
|
|
client proto.WoodpeckerClient
|
2017-06-28 17:21:22 +00:00
|
|
|
conn *grpc.ClientConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGrpcClient returns a new grpc Client.
|
|
|
|
func NewGrpcClient(conn *grpc.ClientConn) Peer {
|
|
|
|
client := new(client)
|
2021-09-29 00:10:09 +00:00
|
|
|
client.client = proto.NewWoodpeckerClient(conn)
|
2017-06-28 17:21:22 +00:00
|
|
|
client.conn = conn
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) Close() error {
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns the next pipeline in the queue.
|
|
|
|
func (c *client) Next(ctx context.Context, f Filter) (*Pipeline, error) {
|
|
|
|
var res *proto.NextReply
|
|
|
|
var err error
|
|
|
|
req := new(proto.NextRequest)
|
|
|
|
req.Filter = new(proto.Filter)
|
|
|
|
req.Filter.Expr = f.Expr
|
|
|
|
req.Filter.Labels = f.Labels
|
|
|
|
for {
|
|
|
|
res, err = c.client.Next(ctx, req)
|
|
|
|
if err == nil {
|
|
|
|
break
|
2018-01-10 21:31:07 +00:00
|
|
|
} else {
|
2022-02-24 14:53:44 +00:00
|
|
|
// TODO: remove after adding continuous data exchange by something like #536
|
|
|
|
if strings.Contains(err.Error(), "\"too_many_pings\"") {
|
|
|
|
// https://github.com/woodpecker-ci/woodpecker/issues/717#issuecomment-1049365104
|
|
|
|
log.Trace().Err(err).Msg("grpc: to many keepalive pings without sending data")
|
|
|
|
} else {
|
|
|
|
log.Err(err).Msgf("grpc error: done(): code: %v: %s", status.Code(err), err)
|
|
|
|
}
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2021-09-24 14:29:26 +00:00
|
|
|
switch status.Code(err) {
|
2017-09-12 16:03:32 +00:00
|
|
|
case
|
|
|
|
codes.Aborted,
|
|
|
|
codes.DataLoss,
|
|
|
|
codes.DeadlineExceeded,
|
|
|
|
codes.Internal,
|
|
|
|
codes.Unavailable:
|
|
|
|
// non-fatal errors
|
|
|
|
default:
|
2017-06-29 21:27:06 +00:00
|
|
|
return nil, err
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2017-07-20 16:21:15 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
2017-06-29 21:27:06 +00:00
|
|
|
<-time.After(backoff)
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if res.GetPipeline() == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p := new(Pipeline)
|
|
|
|
p.ID = res.GetPipeline().GetId()
|
|
|
|
p.Timeout = res.GetPipeline().GetTimeout()
|
|
|
|
p.Config = new(backend.Config)
|
2021-11-23 14:36:52 +00:00
|
|
|
if err := json.Unmarshal(res.GetPipeline().GetPayload(), p.Config); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("could not unmarshal pipeline config of '%s'", p.ID)
|
|
|
|
}
|
2017-06-28 17:21:22 +00:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait blocks until the pipeline is complete.
|
|
|
|
func (c *client) Wait(ctx context.Context, id string) (err error) {
|
|
|
|
req := new(proto.WaitRequest)
|
|
|
|
req.Id = id
|
|
|
|
for {
|
|
|
|
_, err = c.client.Wait(ctx, req)
|
|
|
|
if err == nil {
|
|
|
|
break
|
2018-01-10 21:31:07 +00:00
|
|
|
} else {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Err(err).Msgf("grpc error: wait(): code: %v: %s", status.Code(err), err)
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2021-09-24 14:29:26 +00:00
|
|
|
switch status.Code(err) {
|
2017-09-12 16:03:32 +00:00
|
|
|
case
|
|
|
|
codes.Aborted,
|
|
|
|
codes.DataLoss,
|
|
|
|
codes.DeadlineExceeded,
|
|
|
|
codes.Internal,
|
|
|
|
codes.Unavailable:
|
|
|
|
// non-fatal errors
|
|
|
|
default:
|
2017-06-29 21:27:06 +00:00
|
|
|
return err
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2017-06-29 21:27:06 +00:00
|
|
|
<-time.After(backoff)
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init signals the pipeline is initialized.
|
|
|
|
func (c *client) Init(ctx context.Context, id string, state State) (err error) {
|
|
|
|
req := new(proto.InitRequest)
|
|
|
|
req.Id = id
|
|
|
|
req.State = new(proto.State)
|
|
|
|
req.State.Error = state.Error
|
|
|
|
req.State.ExitCode = int32(state.ExitCode)
|
|
|
|
req.State.Exited = state.Exited
|
|
|
|
req.State.Finished = state.Finished
|
|
|
|
req.State.Started = state.Started
|
|
|
|
req.State.Name = state.Proc
|
|
|
|
for {
|
|
|
|
_, err = c.client.Init(ctx, req)
|
|
|
|
if err == nil {
|
|
|
|
break
|
2018-01-10 21:31:07 +00:00
|
|
|
} else {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Err(err).Msgf("grpc error: init(): code: %v: %s", status.Code(err), err)
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2021-09-24 14:29:26 +00:00
|
|
|
switch status.Code(err) {
|
2017-09-12 16:03:32 +00:00
|
|
|
case
|
|
|
|
codes.Aborted,
|
|
|
|
codes.DataLoss,
|
|
|
|
codes.DeadlineExceeded,
|
|
|
|
codes.Internal,
|
|
|
|
codes.Unavailable:
|
|
|
|
// non-fatal errors
|
|
|
|
default:
|
2017-06-29 21:27:06 +00:00
|
|
|
return err
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2017-06-29 21:27:06 +00:00
|
|
|
<-time.After(backoff)
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done signals the pipeline is complete.
|
|
|
|
func (c *client) Done(ctx context.Context, id string, state State) (err error) {
|
|
|
|
req := new(proto.DoneRequest)
|
|
|
|
req.Id = id
|
|
|
|
req.State = new(proto.State)
|
|
|
|
req.State.Error = state.Error
|
|
|
|
req.State.ExitCode = int32(state.ExitCode)
|
|
|
|
req.State.Exited = state.Exited
|
|
|
|
req.State.Finished = state.Finished
|
|
|
|
req.State.Started = state.Started
|
|
|
|
req.State.Name = state.Proc
|
|
|
|
for {
|
|
|
|
_, err = c.client.Done(ctx, req)
|
|
|
|
if err == nil {
|
|
|
|
break
|
2018-01-10 21:31:07 +00:00
|
|
|
} else {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Err(err).Msgf("grpc error: done(): code: %v: %s", status.Code(err), err)
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2021-09-24 14:29:26 +00:00
|
|
|
switch status.Code(err) {
|
2017-09-12 16:03:32 +00:00
|
|
|
case
|
|
|
|
codes.Aborted,
|
|
|
|
codes.DataLoss,
|
|
|
|
codes.DeadlineExceeded,
|
|
|
|
codes.Internal,
|
|
|
|
codes.Unavailable:
|
|
|
|
// non-fatal errors
|
|
|
|
default:
|
2017-06-29 21:27:06 +00:00
|
|
|
return err
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2017-06-29 21:27:06 +00:00
|
|
|
<-time.After(backoff)
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extend extends the pipeline deadline
|
|
|
|
func (c *client) Extend(ctx context.Context, id string) (err error) {
|
|
|
|
req := new(proto.ExtendRequest)
|
|
|
|
req.Id = id
|
|
|
|
for {
|
|
|
|
_, err = c.client.Extend(ctx, req)
|
|
|
|
if err == nil {
|
|
|
|
break
|
2018-01-10 21:31:07 +00:00
|
|
|
} else {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Err(err).Msgf("grpc error: extend(): code: %v: %s", status.Code(err), err)
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2021-09-24 14:29:26 +00:00
|
|
|
switch status.Code(err) {
|
2017-09-12 16:03:32 +00:00
|
|
|
case
|
|
|
|
codes.Aborted,
|
|
|
|
codes.DataLoss,
|
|
|
|
codes.DeadlineExceeded,
|
|
|
|
codes.Internal,
|
|
|
|
codes.Unavailable:
|
|
|
|
// non-fatal errors
|
|
|
|
default:
|
2017-06-29 21:27:06 +00:00
|
|
|
return err
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2017-06-29 21:27:06 +00:00
|
|
|
<-time.After(backoff)
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates the pipeline state.
|
|
|
|
func (c *client) Update(ctx context.Context, id string, state State) (err error) {
|
|
|
|
req := new(proto.UpdateRequest)
|
|
|
|
req.Id = id
|
|
|
|
req.State = new(proto.State)
|
|
|
|
req.State.Error = state.Error
|
|
|
|
req.State.ExitCode = int32(state.ExitCode)
|
|
|
|
req.State.Exited = state.Exited
|
|
|
|
req.State.Finished = state.Finished
|
|
|
|
req.State.Started = state.Started
|
|
|
|
req.State.Name = state.Proc
|
|
|
|
for {
|
|
|
|
_, err = c.client.Update(ctx, req)
|
|
|
|
if err == nil {
|
|
|
|
break
|
2018-01-10 21:31:07 +00:00
|
|
|
} else {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Err(err).Msgf("grpc error: update(): code: %v: %s", status.Code(err), err)
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2021-09-24 14:29:26 +00:00
|
|
|
switch status.Code(err) {
|
2017-09-12 16:03:32 +00:00
|
|
|
case
|
|
|
|
codes.Aborted,
|
|
|
|
codes.DataLoss,
|
|
|
|
codes.DeadlineExceeded,
|
|
|
|
codes.Internal,
|
|
|
|
codes.Unavailable:
|
|
|
|
// non-fatal errors
|
|
|
|
default:
|
2017-06-29 21:27:06 +00:00
|
|
|
return err
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2017-06-29 21:27:06 +00:00
|
|
|
<-time.After(backoff)
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upload uploads the pipeline artifact.
|
|
|
|
func (c *client) Upload(ctx context.Context, id string, file *File) (err error) {
|
|
|
|
req := new(proto.UploadRequest)
|
|
|
|
req.Id = id
|
|
|
|
req.File = new(proto.File)
|
|
|
|
req.File.Name = file.Name
|
|
|
|
req.File.Mime = file.Mime
|
|
|
|
req.File.Proc = file.Proc
|
|
|
|
req.File.Size = int32(file.Size)
|
|
|
|
req.File.Time = file.Time
|
|
|
|
req.File.Data = file.Data
|
2017-08-02 20:04:00 +00:00
|
|
|
req.File.Meta = file.Meta
|
2017-06-28 17:21:22 +00:00
|
|
|
for {
|
|
|
|
_, err = c.client.Upload(ctx, req)
|
|
|
|
if err == nil {
|
|
|
|
break
|
2018-01-10 21:31:07 +00:00
|
|
|
} else {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Err(err).Msgf("grpc error: upload(): code: %v: %s", status.Code(err), err)
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2021-09-24 14:29:26 +00:00
|
|
|
switch status.Code(err) {
|
2017-09-12 16:03:32 +00:00
|
|
|
case
|
|
|
|
codes.Aborted,
|
|
|
|
codes.DataLoss,
|
|
|
|
codes.DeadlineExceeded,
|
|
|
|
codes.Internal,
|
|
|
|
codes.Unavailable:
|
|
|
|
// non-fatal errors
|
|
|
|
default:
|
2017-06-29 21:27:06 +00:00
|
|
|
return err
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2017-06-29 21:27:06 +00:00
|
|
|
<-time.After(backoff)
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log writes the pipeline log entry.
|
|
|
|
func (c *client) Log(ctx context.Context, id string, line *Line) (err error) {
|
|
|
|
req := new(proto.LogRequest)
|
|
|
|
req.Id = id
|
|
|
|
req.Line = new(proto.Line)
|
|
|
|
req.Line.Out = line.Out
|
|
|
|
req.Line.Pos = int32(line.Pos)
|
|
|
|
req.Line.Proc = line.Proc
|
|
|
|
req.Line.Time = line.Time
|
|
|
|
for {
|
|
|
|
_, err = c.client.Log(ctx, req)
|
|
|
|
if err == nil {
|
|
|
|
break
|
2018-01-10 21:31:07 +00:00
|
|
|
} else {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Err(err).Msgf("grpc error: log(): code: %v: %s", status.Code(err), err)
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2021-09-24 14:29:26 +00:00
|
|
|
switch status.Code(err) {
|
2017-09-12 16:03:32 +00:00
|
|
|
case
|
|
|
|
codes.Aborted,
|
|
|
|
codes.DataLoss,
|
|
|
|
codes.DeadlineExceeded,
|
|
|
|
codes.Internal,
|
|
|
|
codes.Unavailable:
|
|
|
|
// non-fatal errors
|
|
|
|
default:
|
2017-06-29 21:27:06 +00:00
|
|
|
return err
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2017-06-29 21:27:06 +00:00
|
|
|
<-time.After(backoff)
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|