Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement jsonrpc2 agent behind feature flag #1954

Merged
merged 11 commits into from
Mar 15, 2017
Prev Previous commit
Next Next commit
implement cancel function
  • Loading branch information
bradrydzewski committed Mar 5, 2017
commit bb7453262ac4b3728322972a11219511471bb9a4
11 changes: 7 additions & 4 deletions drone/agent/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,9 @@ func run(ctx context.Context, client rpc.Peer) error {

cancelled := abool.New()
go func() {
ok, _ := client.Notify(ctx, work.ID)
if ok {
if err := client.Wait(ctx, work.ID); err != nil {
cancelled.SetTo(true)
log.Printf("pipeline: cancel signal received: %s", work.ID)
log.Printf("pipeline: cancel signal received: %s: %s", work.ID, err)
cancel()
} else {
log.Printf("pipeline: cancel channel closed: %s", work.ID)
Expand All @@ -133,16 +132,19 @@ func run(ctx context.Context, client rpc.Peer) error {
log.Printf("pipeline: error updating pipeline status: %s: %s", work.ID, err)
}

var uploads sync.WaitGroup
defaultLogger := pipeline.LogFunc(func(proc *backend.Step, rc multipart.Reader) error {
part, rerr := rc.NextPart()
if rerr != nil {
return rerr
}
uploads.Add(1)
writer := rpc.NewLineWriter(client, work.ID, proc.Alias)
io.Copy(writer, part)

defer func() {
log.Printf("pipeline: finish uploading logs: %s: step %s", work.ID, proc.Alias)
uploads.Done()
}()

part, rerr = rc.NextPart()
Expand Down Expand Up @@ -174,14 +176,15 @@ func run(ctx context.Context, client rpc.Peer) error {
state.ExitCode = xerr.Code
}
if cancelled.IsSet() {
state.ExitCode = 130
state.ExitCode = 137
} else if state.ExitCode == 0 {
state.ExitCode = 1
}
}

log.Printf("pipeline: execution complete: %s", work.ID)

uploads.Wait()
err = client.Update(context.Background(), work.ID, state)
if err != nil {
log.Printf("Pipeine: error updating pipeline status: %s: %s", work.ID, err)
Expand Down
29 changes: 18 additions & 11 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package server

import (
"bufio"
"context"
"fmt"
"io"
"net/http"
"os"
"strconv"
"time"

log "github.com/Sirupsen/logrus"
"github.com/cncd/queue"
"github.com/drone/drone/remote"
"github.com/drone/drone/shared/httputil"
"github.com/drone/drone/store"
Expand Down Expand Up @@ -149,14 +153,17 @@ func DeleteBuild(c *gin.Context) {
job.ExitCode = 137
store.UpdateBuildJob(c, build, job)

client := stomp.MustFromContext(c)
client.SendJSON("/topic/cancel", model.Event{
Type: model.Cancelled,
Repo: *repo,
Build: *build,
Job: *job,
}, stomp.WithHeader("job-id", strconv.FormatInt(job.ID, 10)))

if os.Getenv("DRONE_CANARY") == "" {
client := stomp.MustFromContext(c)
client.SendJSON("/topic/cancel", model.Event{
Type: model.Cancelled,
Repo: *repo,
Build: *build,
Job: *job,
}, stomp.WithHeader("job-id", strconv.FormatInt(job.ID, 10)))
} else {
config.queue.Error(context.Background(), fmt.Sprint(job.ID), queue.ErrCancel)
}
c.String(204, "")
}

Expand Down Expand Up @@ -197,16 +204,16 @@ func PostBuild(c *gin.Context) {
}

// fetch the .drone.yml file from the database
config := ToConfig(c)
raw, err := remote_.File(user, repo, build, config.Yaml)
cfg := ToConfig(c)
raw, err := remote_.File(user, repo, build, cfg.Yaml)
if err != nil {
log.Errorf("failure to get build config for %s. %s", repo.FullName, err)
c.AbortWithError(404, err)
return
}

// Fetch secrets file but don't exit on error as it's optional
sec, err := remote_.File(user, repo, build, config.Shasum)
sec, err := remote_.File(user, repo, build, cfg.Shasum)
if err != nil {
log.Debugf("cannot find build secrets for %s. %s", repo.FullName, err)
}
Expand Down
10 changes: 6 additions & 4 deletions server/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ func (s *RPC) Next(c context.Context) (*rpc.Pipeline, error) {
return pipeline, err
}

// Notify implements the rpc.Notify function
func (s *RPC) Notify(c context.Context, id string) (bool, error) {
err := s.queue.Wait(c, id)
return (err == queue.ErrCancel), nil
// Wait implements the rpc.Wait function
func (s *RPC) Wait(c context.Context, id string) error {
return s.queue.Wait(c, id)
}

// Extend implements the rpc.Extend function
Expand Down Expand Up @@ -211,6 +210,9 @@ func (s *RPC) Update(c context.Context, id string, state rpc.State) error {
// Save implements the rpc.Save function
func (s *RPC) Save(c context.Context, id, mime string, file io.Reader) error { return nil }

// Done implements the rpc.Done function
func (s *RPC) Done(c context.Context, id string) error { return nil }

// Log implements the rpc.Log function
func (s *RPC) Log(c context.Context, id string, line *rpc.Line) error {
entry := new(logging.Entry)
Expand Down
22 changes: 15 additions & 7 deletions vendor/github.com/cncd/pipeline/pipeline/rpc/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions vendor/github.com/cncd/pipeline/pipeline/rpc/peer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 18 additions & 7 deletions vendor/github.com/cncd/pipeline/pipeline/rpc/server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 23 additions & 23 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,68 +33,68 @@
{
"checksumSHA1": "W3AuK8ocqHwlUajGmQLFvnRhTZE=",
"path": "github.com/cncd/pipeline/pipeline",
"revision": "b5457789534af0da2a05bd9fd0c0afe6fea391c6",
"revisionTime": "2017-03-04T04:47:59Z"
"revision": "d4e09fd3021a16408bc3ebdd3500efd28f51e72c",
"revisionTime": "2017-03-05T09:53:47Z"
},
{
"checksumSHA1": "PSzh0ix/rlMrS/Cl3aH6GHGrJuo=",
"path": "github.com/cncd/pipeline/pipeline/backend",
"revision": "b5457789534af0da2a05bd9fd0c0afe6fea391c6",
"revisionTime": "2017-03-04T04:47:59Z"
"revision": "d4e09fd3021a16408bc3ebdd3500efd28f51e72c",
"revisionTime": "2017-03-05T09:53:47Z"
},
{
"checksumSHA1": "0CGXRaYwZhJxGIrGhn8WGpkFqPo=",
"path": "github.com/cncd/pipeline/pipeline/backend/docker",
"revision": "b5457789534af0da2a05bd9fd0c0afe6fea391c6",
"revisionTime": "2017-03-04T04:47:59Z"
"revision": "d4e09fd3021a16408bc3ebdd3500efd28f51e72c",
"revisionTime": "2017-03-05T09:53:47Z"
},
{
"checksumSHA1": "uUagpzha5ah/a3RO6IImvzHYFlY=",
"path": "github.com/cncd/pipeline/pipeline/frontend",
"revision": "b5457789534af0da2a05bd9fd0c0afe6fea391c6",
"revisionTime": "2017-03-04T04:47:59Z"
"revision": "d4e09fd3021a16408bc3ebdd3500efd28f51e72c",
"revisionTime": "2017-03-05T09:53:47Z"
},
{
"checksumSHA1": "O0sulBQAHJeNLg3lO38Cq5uf/eg=",
"path": "github.com/cncd/pipeline/pipeline/frontend/yaml",
"revision": "b5457789534af0da2a05bd9fd0c0afe6fea391c6",
"revisionTime": "2017-03-04T04:47:59Z"
"revision": "d4e09fd3021a16408bc3ebdd3500efd28f51e72c",
"revisionTime": "2017-03-05T09:53:47Z"
},
{
"checksumSHA1": "e1lZWQdObXCKWqZOGlOeaeERQMc=",
"path": "github.com/cncd/pipeline/pipeline/frontend/yaml/compiler",
"revision": "b5457789534af0da2a05bd9fd0c0afe6fea391c6",
"revisionTime": "2017-03-04T04:47:59Z"
"revision": "d4e09fd3021a16408bc3ebdd3500efd28f51e72c",
"revisionTime": "2017-03-05T09:53:47Z"
},
{
"checksumSHA1": "Q0GkNUFamVYIA1Fd8r0A5M6Gx54=",
"path": "github.com/cncd/pipeline/pipeline/frontend/yaml/linter",
"revision": "b5457789534af0da2a05bd9fd0c0afe6fea391c6",
"revisionTime": "2017-03-04T04:47:59Z"
"revision": "d4e09fd3021a16408bc3ebdd3500efd28f51e72c",
"revisionTime": "2017-03-05T09:53:47Z"
},
{
"checksumSHA1": "kx2sPUIMozPC/g6E4w48h3FfH3k=",
"path": "github.com/cncd/pipeline/pipeline/frontend/yaml/matrix",
"revision": "b5457789534af0da2a05bd9fd0c0afe6fea391c6",
"revisionTime": "2017-03-04T04:47:59Z"
"revision": "d4e09fd3021a16408bc3ebdd3500efd28f51e72c",
"revisionTime": "2017-03-05T09:53:47Z"
},
{
"checksumSHA1": "2/3f3oNmxXy5kcrRLCFa24Oc9O4=",
"path": "github.com/cncd/pipeline/pipeline/interrupt",
"revision": "b5457789534af0da2a05bd9fd0c0afe6fea391c6",
"revisionTime": "2017-03-04T04:47:59Z"
"revision": "d4e09fd3021a16408bc3ebdd3500efd28f51e72c",
"revisionTime": "2017-03-05T09:53:47Z"
},
{
"checksumSHA1": "8eTwXZPM/Kp9uE/mnhpWDTiX7nY=",
"path": "github.com/cncd/pipeline/pipeline/multipart",
"revision": "b5457789534af0da2a05bd9fd0c0afe6fea391c6",
"revisionTime": "2017-03-04T04:47:59Z"
"revision": "d4e09fd3021a16408bc3ebdd3500efd28f51e72c",
"revisionTime": "2017-03-05T09:53:47Z"
},
{
"checksumSHA1": "5axmtZsHaQ5uE/tuNQZygquNx8U=",
"checksumSHA1": "UUmeGDBdpk+UXtexFnNmbWIHgG8=",
"path": "github.com/cncd/pipeline/pipeline/rpc",
"revision": "b5457789534af0da2a05bd9fd0c0afe6fea391c6",
"revisionTime": "2017-03-04T04:47:59Z"
"revision": "d4e09fd3021a16408bc3ebdd3500efd28f51e72c",
"revisionTime": "2017-03-05T09:53:47Z"
},
{
"checksumSHA1": "7Qj1DK0ceAXkYztW0l3+L6sn+V8=",
Expand Down