shad-go/distbuild/pkg/worker/worker.go

146 lines
3 KiB
Go
Raw Normal View History

2020-03-10 12:08:59 +00:00
package worker
import (
2020-03-14 10:24:44 +00:00
"bytes"
2020-03-10 12:08:59 +00:00
"context"
2020-03-14 10:24:44 +00:00
"encoding/json"
"fmt"
"io/ioutil"
2020-03-11 22:46:45 +00:00
"net/http"
2020-03-14 10:24:44 +00:00
"os/exec"
2020-03-10 12:08:59 +00:00
"sync"
2020-03-11 22:46:45 +00:00
"go.uber.org/zap"
2020-03-10 12:08:59 +00:00
"gitlab.com/slon/shad-go/distbuild/pkg/artifact"
"gitlab.com/slon/shad-go/distbuild/pkg/build"
"gitlab.com/slon/shad-go/distbuild/pkg/filecache"
2020-03-14 10:24:44 +00:00
"gitlab.com/slon/shad-go/distbuild/pkg/proto"
2020-03-10 12:08:59 +00:00
)
type Worker struct {
2020-03-11 22:46:45 +00:00
coordinatorEndpoint string
log *zap.Logger
fileCache *filecache.Cache
artifacts *artifact.Cache
2020-03-10 12:08:59 +00:00
2020-03-11 22:46:45 +00:00
mux *http.ServeMux
2020-03-10 12:08:59 +00:00
mu sync.Mutex
newArtifacts []build.ID
newSources []build.ID
2020-03-14 10:24:44 +00:00
finishedJobs []proto.FinishedJob
2020-03-10 12:08:59 +00:00
}
2020-03-11 22:46:45 +00:00
func New(
coordinatorEndpoint string,
log *zap.Logger,
fileCache *filecache.Cache,
artifacts *artifact.Cache,
) *Worker {
return &Worker{
coordinatorEndpoint: coordinatorEndpoint,
log: log,
fileCache: fileCache,
artifacts: artifacts,
mux: http.NewServeMux(),
}
}
func (w *Worker) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
w.mux.ServeHTTP(rw, r)
}
2020-03-10 12:08:59 +00:00
func (w *Worker) recover() error {
2020-03-14 10:24:44 +00:00
//err := w.fileCache.Range(func(file build.ID) error {
// w.newSources = append(w.newSources, file)
// return nil
//})
//if err != nil {
// return err
//}
2020-03-10 12:08:59 +00:00
2020-03-11 22:46:45 +00:00
return w.artifacts.Range(func(file build.ID) error {
2020-03-10 12:08:59 +00:00
w.newArtifacts = append(w.newArtifacts, file)
return nil
})
}
2020-03-14 10:24:44 +00:00
func (w *Worker) sendHeartbeat(req *proto.HeartbeatRequest) (*proto.HeartbeatResponse, error) {
reqJS, err := json.Marshal(req)
if err != nil {
return nil, err
}
httpReq, err := http.NewRequest("POST", w.coordinatorEndpoint+"/heartbeat", bytes.NewBuffer(reqJS))
if err != nil {
return nil, err
}
httpRsp, err := http.DefaultClient.Do(httpReq)
if err != nil {
return nil, err
}
if httpRsp.StatusCode != http.StatusOK {
errorString, _ := ioutil.ReadAll(httpRsp.Body)
return nil, fmt.Errorf("heartbeat failed: %s", errorString)
}
var rsp proto.HeartbeatResponse
if err := json.NewDecoder(httpRsp.Body).Decode(&rsp); err != nil {
return nil, err
}
return &rsp, nil
}
2020-03-10 12:08:59 +00:00
func (w *Worker) Run(ctx context.Context) error {
if err := w.recover(); err != nil {
return err
}
for {
2020-03-14 10:24:44 +00:00
w.log.Debug("sending heartbeat request")
rsp, err := w.sendHeartbeat(w.buildHeartbeat())
if err != nil {
if ctx.Err() != nil {
return ctx.Err()
}
w.log.DPanic("heartbeat failed", zap.Error(err))
continue
}
w.log.Debug("received heartbeat response",
zap.Int("num_jobs", len(rsp.JobsToRun)))
for _, job := range rsp.JobsToRun {
var finished proto.FinishedJob
finished.ID = job.Job.ID
var stdout bytes.Buffer
var stderr bytes.Buffer
for _, jobCmd := range job.Job.Cmds {
cmd := exec.Command(jobCmd.Exec[0], jobCmd.Exec[1:]...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
errorString := err.Error()
finished.Error = &errorString
finished.ExitCode = cmd.ProcessState.ExitCode()
break
}
}
finished.Stdout = stdout.Bytes()
finished.Stderr = stderr.Bytes()
w.jobFinished(&finished)
}
2020-03-10 12:08:59 +00:00
}
}