diff --git a/commands/builds_helper.go b/commands/builds_helper.go index 87657872674f4d8abba042dc41ac020ceecafe13..e5b9cdf0c93dd7997c97046810398a61f9c74c0f 100644 --- a/commands/builds_helper.go +++ b/commands/builds_helper.go @@ -91,3 +91,7 @@ func (b *buildsHelper) removeBuild(deleteBuild *common.Build) bool { } return false } + +func (b *buildsHelper) buildsCount() int { + return len(b.builds) +} diff --git a/commands/info.go b/commands/info.go new file mode 100644 index 0000000000000000000000000000000000000000..58f45c0b5d06ba225370fb18724974fadc03dfd9 --- /dev/null +++ b/commands/info.go @@ -0,0 +1,50 @@ +package commands + +import ( + "encoding/json" + "fmt" + + log "github.com/Sirupsen/logrus" + "github.com/codegangsta/cli" + "gitlab.com/gitlab-org/gitlab-ci-multi-runner/common" + "gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers" +) + +type InfoData struct { + BuildsCount int `json:"builds_count"` + + RunnersBuildsCounts map[string]int `json:"runners_builds_counts"` + VersionInfo common.AppVersionInfo `json:"version_info"` +} + +type InfoCommand struct { + buildsHelper buildsHelper + data InfoData +} + +func (c *InfoCommand) prepare() { + c.data.BuildsCount = c.buildsHelper.buildsCount() + c.data.VersionInfo = common.AppVersion + + runnersBuildsCounts := map[string]int{} + for token, count := range c.buildsHelper.counts { + runnersBuildsCounts[helpers.ShortenToken(token)] = count + } + c.data.RunnersBuildsCounts = runnersBuildsCounts +} + +func (c *InfoCommand) Execute(context *cli.Context) { + c.prepare() + + bytes, err := json.Marshal(c.data) + if err != nil { + log.WithError(err).Errorln("Error with InfoData marshalling to JSON") + return + } + + fmt.Print(string(bytes)) +} + +func init() { + common.RegisterCommand2("info", "show statistic and debuging data", &InfoCommand{}) +} diff --git a/commands/multi.go b/commands/multi.go index e67483221e8049e0f314db0629a7dd544935aa69..d3bea8c5870da77c3e1e9fb9f6f16766cf31ae3f 100644 --- a/commands/multi.go +++ b/commands/multi.go @@ -56,7 +56,7 @@ type RunCommand struct { } func (mr *RunCommand) log() *log.Entry { - return log.WithField("builds", len(mr.buildsHelper.builds)) + return log.WithField("builds", mr.buildsHelper.buildsCount()) } func (mr *RunCommand) feedRunner(runner *common.RunnerConfig, runners chan *common.RunnerConfig) {