Commit f954e633 authored by Kamil Trzciński's avatar Kamil Trzciński 🔴

Send build log as early as possible

parent 9ce68655
v 0.1.10
- Wait for docker service to come up (Loïc Guitaut)
- Send build log as early as possible
v 0.1.9
- Fixed problem with resetting ruby environment
......
......@@ -9,8 +9,6 @@ import (
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/ayufan/gitlab-ci-multi-runner/helpers"
)
......@@ -30,6 +28,7 @@ type Build struct {
BuildStarted time.Time `json:"build_started"`
BuildFinished time.Time `json:"build_finished"`
BuildDuration time.Duration `json:"build_duration"`
BuildMessage string `json:"build_message"`
Runner *RunnerConfig `json:"runner"`
GlobalId int `json:"global_id"`
......@@ -178,23 +177,6 @@ func (build *Build) GetEnv() []string {
}
}
func (build *Build) fail(err error) {
log.Errorln(build.Runner.ShortDescription(), build.Id, "Build failed", err)
for {
error_buffer := bytes.NewBufferString(err.Error())
result := UpdateBuild(*build.Runner, build.Id, Failed, error_buffer)
switch result {
case UpdateSucceeded:
return
case UpdateAbort:
return
case UpdateFailed:
time.Sleep(UPDATE_RETRY_INTERVAL * time.Second)
continue
}
}
}
func (build *Build) Run() error {
var err error
executor := GetExecutor(build.Runner.Executor)
......@@ -210,9 +192,7 @@ func (build *Build) Run() error {
if err == nil {
err = executor.Wait()
}
if err != nil {
go build.fail(err)
}
executor.Finish(err)
if executor != nil {
executor.Cleanup()
}
......
......@@ -8,6 +8,7 @@ type Executor interface {
Prepare(config *RunnerConfig, build *Build) error
Start() error
Wait() error
Finish(err error)
Cleanup()
}
......
......@@ -138,21 +138,12 @@ func (e *AbstractExecutor) Prepare(config *common.RunnerConfig, build *common.Bu
}
e.BuildLog = build_log
e.Debugln("Created build log:", build_log.Name())
go e.WatchTrace(*e.Config, e.BuildAbort, e.BuildLogFinish)
return nil
}
func (e *AbstractExecutor) Cleanup() {
if e.BuildLog != nil {
os.Remove(e.BuildLog.Name())
e.BuildLog.Close()
}
}
func (e *AbstractExecutor) Wait() error {
var buildState common.BuildState
var buildMessage string
go e.WatchTrace(*e.Config, e.BuildAbort, e.BuildLogFinish)
e.Build.BuildState = common.Running
buildTimeout := e.Build.Timeout
if buildTimeout <= 0 {
......@@ -164,36 +155,50 @@ func (e *AbstractExecutor) Wait() error {
select {
case <-e.BuildAbort:
log.Println(e.Config.ShortDescription(), e.Build.Id, "Build got aborted.")
buildState = common.Failed
e.Build.BuildState = common.Failed
e.Build.BuildMessage = "\nBuild got aborted"
case <-time.After(time.Duration(buildTimeout) * time.Second):
log.Println(e.Config.ShortDescription(), e.Build.Id, "Build timedout.")
buildState = common.Failed
buildMessage = fmt.Sprintf("\nCI Timeout. Execution took longer then %d seconds", buildTimeout)
e.Build.BuildState = common.Failed
e.Build.BuildMessage = fmt.Sprintf("\nCI Timeout. Execution took longer then %d seconds", buildTimeout)
case err := <-e.BuildFinish:
// command finished
if err != nil {
log.Println(e.Config.ShortDescription(), e.Build.Id, "Build failed with", err)
buildState = common.Failed
buildMessage = fmt.Sprintf("\nBuild failed with %v", err)
} else {
log.Println(e.Config.ShortDescription(), e.Build.Id, "Build succeeded.")
buildState = common.Success
return err
}
log.Println(e.Config.ShortDescription(), e.Build.Id, "Build succeeded.")
e.Build.BuildState = common.Success
e.Build.BuildMessage = "\n"
}
return nil
}
func (e *AbstractExecutor) Finish(err error) {
if err != nil {
e.Build.BuildState = common.Failed
e.Build.BuildMessage = fmt.Sprintf("\nBuild failed with %v", err)
}
e.Build.BuildState = buildState
e.Build.BuildFinished = time.Now()
e.Build.BuildDuration = e.Build.BuildFinished.Sub(e.Build.BuildStarted)
e.Debugln("Build took", e.Build.BuildDuration)
// wait for update log routine to finish
e.Debugln("Waiting for build log updater to finish")
e.BuildLogFinish <- true
e.Debugln("Build log updater finished.")
if e.BuildLog != nil {
// wait for update log routine to finish
e.Debugln("Waiting for build log updater to finish")
e.BuildLogFinish <- true
e.Debugln("Build log updater finished.")
}
// Send final build state to server
e.FinishBuild(*e.Config, buildState, buildMessage)
return nil
e.FinishBuild(*e.Config, e.Build.BuildState, e.Build.BuildMessage)
}
func (e *AbstractExecutor) Cleanup() {
if e.BuildLog != nil {
os.Remove(e.BuildLog.Name())
e.BuildLog.Close()
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment