Engine: improved logging and identification of jobs

This commit is contained in:
Solomon Hykes 2013-10-27 07:01:15 +00:00
parent ca6f0aa107
commit 4e7cb37dcc
2 changed files with 38 additions and 5 deletions

View file

@ -5,6 +5,7 @@ import (
"os"
"log"
"runtime"
"strings"
"github.com/dotcloud/docker/utils"
)
@ -33,6 +34,11 @@ type Engine struct {
root string
handlers map[string]Handler
hack Hack // data for temporary hackery (see hack.go)
id string
}
func (eng *Engine) Root() string {
return eng.root
}
func (eng *Engine) Register(name string, handler Handler) error {
@ -84,6 +90,10 @@ func New(root string) (*Engine, error) {
return eng, nil
}
func (eng *Engine) String() string {
return fmt.Sprintf("%s|%s", eng.Root(), eng.id[:8])
}
// Job creates a new job which can later be executed.
// This function mimics `Command` from the standard os/exec package.
func (eng *Engine) Job(name string, args ...string) *Job {
@ -102,3 +112,8 @@ func (eng *Engine) Job(name string, args ...string) *Job {
return job
}
func (eng *Engine) Logf(format string, args ...interface{}) (n int, err error) {
prefixedFormat := fmt.Sprintf("[%s] %s\n", eng, strings.TrimRight(format, "\n"))
return fmt.Printf(prefixedFormat, args...)
}

View file

@ -6,7 +6,6 @@ import (
"strings"
"fmt"
"encoding/json"
"github.com/dotcloud/docker/utils"
)
// A job is the fundamental unit of work in the docker engine.
@ -38,9 +37,10 @@ type Job struct {
// If the job returns a failure status, an error is returned
// which includes the status.
func (job *Job) Run() error {
randId := utils.RandomString()[:4]
fmt.Printf("Job #%s: %s\n", randId, job)
defer fmt.Printf("Job #%s: %s = '%s'", randId, job, job.status)
job.Logf("{")
defer func() {
job.Logf("}")
}()
if job.handler == nil {
job.status = "command not found"
} else {
@ -54,7 +54,20 @@ func (job *Job) Run() error {
// String returns a human-readable description of `job`
func (job *Job) String() string {
return strings.Join(append([]string{job.Name}, job.Args...), " ")
s := fmt.Sprintf("%s.%s(%s)", job.Eng, job.Name, strings.Join(job.Args, ", "))
// FIXME: if a job returns the empty string, it will be printed
// as not having returned.
// (this only affects String which is a convenience function).
if job.status != "" {
var okerr string
if job.status == "0" {
okerr = "OK"
} else {
okerr = "ERR"
}
s = fmt.Sprintf("%s = %s (%s)", s, okerr, job.status)
}
return s
}
func (job *Job) Getenv(key string) (value string) {
@ -169,3 +182,8 @@ func (job *Job) Environ() map[string]string {
}
return m
}
func (job *Job) Logf(format string, args ...interface{}) (n int, err error) {
prefixedFormat := fmt.Sprintf("[%s] %s\n", job, strings.TrimRight(format, "\n"))
return fmt.Fprintf(job.Stdout, prefixedFormat, args...)
}