Use api for communicating with client.
This commit is contained in:
parent
b97b6e9a0f
commit
6e5c478920
3 changed files with 50 additions and 94 deletions
|
@ -67,11 +67,10 @@ func newEnv(t *testing.T) (e *env, cancel func()) {
|
|||
var cancelRootContext func()
|
||||
env.Ctx, cancelRootContext = context.WithCancel(context.Background())
|
||||
|
||||
env.Client = &client.Client{
|
||||
CoordinatorEndpoint: coordinatorEndpoint,
|
||||
SourceDir: filepath.Join(absCWD, "testdata/src"),
|
||||
Log: env.Logger.Named("client"),
|
||||
}
|
||||
env.Client = client.NewClient(
|
||||
env.Logger.Named("client"),
|
||||
coordinatorEndpoint,
|
||||
filepath.Join(absCWD, "testdata/src"))
|
||||
|
||||
coordinatorCache, err := filecache.New(filepath.Join(env.RootDir, "coordinator", "filecache"))
|
||||
require.NoError(t, err)
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"io"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
|
@ -15,9 +12,21 @@ import (
|
|||
)
|
||||
|
||||
type Client struct {
|
||||
CoordinatorEndpoint string
|
||||
SourceDir string
|
||||
Log *zap.Logger
|
||||
l *zap.Logger
|
||||
client *api.Client
|
||||
sourceDir string
|
||||
}
|
||||
|
||||
func NewClient(
|
||||
l *zap.Logger,
|
||||
apiEndpoint string,
|
||||
sourceDir string,
|
||||
) *Client {
|
||||
return &Client{
|
||||
l: l,
|
||||
client: &api.Client{Endpoint: apiEndpoint},
|
||||
sourceDir: sourceDir,
|
||||
}
|
||||
}
|
||||
|
||||
type BuildListener interface {
|
||||
|
@ -28,62 +37,39 @@ type BuildListener interface {
|
|||
OnJobFailed(jobID build.ID, code int, error string) error
|
||||
}
|
||||
|
||||
func (c *Client) uploadSources(ctx context.Context, src api.BuildStarted) error {
|
||||
func (c *Client) uploadSources(ctx context.Context, started *api.BuildStarted) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Build(ctx context.Context, graph build.Graph, lsn BuildListener) error {
|
||||
graphJS, err := json.Marshal(graph)
|
||||
started, r, err := c.client.StartBuild(ctx, &api.BuildRequest{Graph: graph})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", c.CoordinatorEndpoint+"/build", bytes.NewBuffer(graphJS))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
c.Log.Debug("sending build request", zap.String("url", req.URL.String()))
|
||||
|
||||
rsp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("build failed: %w", err)
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
|
||||
if rsp.StatusCode != 200 {
|
||||
errorMsg, _ := ioutil.ReadAll(rsp.Body)
|
||||
return fmt.Errorf("build failed: %s", errorMsg)
|
||||
}
|
||||
|
||||
d := json.NewDecoder(rsp.Body)
|
||||
|
||||
var missing api.BuildStarted
|
||||
if err := d.Decode(&missing); err != nil {
|
||||
return fmt.Errorf("error receiving source list: %w", err)
|
||||
}
|
||||
|
||||
if err := c.uploadSources(ctx, missing); err != nil {
|
||||
c.l.Debug("build started", zap.String("build_id", started.ID.String()))
|
||||
if err := c.uploadSources(ctx, started); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for {
|
||||
var update api.StatusUpdate
|
||||
if err := d.Decode(&update); err != nil {
|
||||
return fmt.Errorf("error receiving status update: %w", err)
|
||||
u, err := r.Next()
|
||||
if err == io.EOF {
|
||||
return fmt.Errorf("unexpected end of status stream")
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.l.Debug("received status update", zap.String("build_id", started.ID.String()), zap.Any("update", u))
|
||||
switch {
|
||||
case update.BuildFailed != nil:
|
||||
return fmt.Errorf("build failed: %s", update.BuildFailed.Error)
|
||||
case u.BuildFailed != nil:
|
||||
return fmt.Errorf("build failed: %s", u.BuildFailed.Error)
|
||||
|
||||
case update.BuildFinished != nil:
|
||||
case u.BuildFinished != nil:
|
||||
return nil
|
||||
|
||||
case update.JobFinished != nil:
|
||||
jf := update.JobFinished
|
||||
case u.JobFinished != nil:
|
||||
jf := u.JobFinished
|
||||
|
||||
if jf.Stdout != nil {
|
||||
if err := lsn.OnJobStdout(jf.ID, jf.Stdout); err != nil {
|
||||
|
|
59
distbuild/pkg/dist/coordinator.go
vendored
59
distbuild/pkg/dist/coordinator.go
vendored
|
@ -1,9 +1,9 @@
|
|||
package dist
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
@ -44,8 +44,9 @@ func NewCoordinator(
|
|||
scheduler: scheduler.NewScheduler(log, defaultConfig),
|
||||
}
|
||||
|
||||
c.mux.HandleFunc("/build", c.Build)
|
||||
c.mux.HandleFunc("/signal", c.Signal)
|
||||
apiHandler := api.NewServiceHandler(log, c)
|
||||
apiHandler.Register(c.mux)
|
||||
|
||||
c.mux.HandleFunc("/heartbeat", c.Heartbeat)
|
||||
return c
|
||||
}
|
||||
|
@ -54,66 +55,36 @@ func (c *Coordinator) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
c.mux.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func (c *Coordinator) doBuild(w http.ResponseWriter, r *http.Request) error {
|
||||
graphJS, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
func (c *Coordinator) StartBuild(ctx context.Context, req *api.BuildRequest, w api.StatusWriter) error {
|
||||
if err := w.Started(&api.BuildStarted{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var g build.Graph
|
||||
if err := json.Unmarshal(graphJS, &g); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
enc := json.NewEncoder(w)
|
||||
if err := enc.Encode(api.BuildStarted{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, job := range g.Jobs {
|
||||
for _, job := range req.Graph.Jobs {
|
||||
job := job
|
||||
|
||||
s := c.scheduler.ScheduleJob(&job)
|
||||
|
||||
select {
|
||||
case <-r.Context().Done():
|
||||
return r.Context().Err()
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-s.Finished:
|
||||
}
|
||||
|
||||
c.log.Debug("job finished", zap.String("job_id", job.ID.String()))
|
||||
|
||||
update := api.StatusUpdate{JobFinished: s.Result}
|
||||
if err := enc.Encode(update); err != nil {
|
||||
jobFinished := api.StatusUpdate{JobFinished: s.Result}
|
||||
if err := w.Updated(&jobFinished); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
update := api.StatusUpdate{BuildFinished: &api.BuildFinished{}}
|
||||
return enc.Encode(update)
|
||||
finished := api.StatusUpdate{BuildFinished: &api.BuildFinished{}}
|
||||
return w.Updated(&finished)
|
||||
}
|
||||
|
||||
func (c *Coordinator) Signal(w http.ResponseWriter, r *http.Request) {
|
||||
c.log.Debug("build signal started")
|
||||
if err := c.doHeartbeat(w, r); err != nil {
|
||||
c.log.Error("build signal failed", zap.Error(err))
|
||||
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
_, _ = w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
c.log.Debug("build signal finished")
|
||||
}
|
||||
|
||||
func (c *Coordinator) Build(w http.ResponseWriter, r *http.Request) {
|
||||
if err := c.doBuild(w, r); err != nil {
|
||||
c.log.Error("build failed", zap.Error(err))
|
||||
|
||||
errorUpdate := api.StatusUpdate{BuildFailed: &api.BuildFailed{Error: err.Error()}}
|
||||
errorJS, _ := json.Marshal(errorUpdate)
|
||||
_, _ = w.Write(errorJS)
|
||||
}
|
||||
func (c *Coordinator) SignalBuild(ctx context.Context, buildID build.ID, signal *api.SignalRequest) (*api.SignalResponse, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c *Coordinator) doHeartbeat(w http.ResponseWriter, r *http.Request) error {
|
||||
|
|
Loading…
Reference in a new issue