Created
March 26, 2015 20:07
-
-
Save runcom/d0e6a9cad2f714e93690 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run executes the job and blocks until the job completes. | |
// If the job fails it returns an error | |
func (job *Job) Run() (err error) { | |
if job.Eng.IsShutdown() && !job.GetenvBool("overrideShutdown") { | |
return fmt.Errorf("engine is shutdown") | |
} | |
// FIXME: this is a temporary workaround to avoid Engine.Shutdown | |
// waiting 5 seconds for server/api.ServeApi to complete (which it never will) | |
// everytime the daemon is cleanly restarted. | |
// The permanent fix is to implement Job.Stop and Job.OnStop so that | |
// ServeApi can cooperate and terminate cleanly. | |
if job.Name != "serveapi" { | |
job.Eng.l.Lock() | |
job.Eng.tasks.Add(1) | |
job.Eng.l.Unlock() | |
defer job.Eng.tasks.Done() | |
} | |
// FIXME: make this thread-safe | |
// FIXME: implement wait | |
if !job.end.IsZero() { | |
return fmt.Errorf("%s: job has already completed", job.Name) | |
} | |
// Log beginning and end of the job | |
if job.Eng.Logging { | |
log.Infof("+job %s", job.CallString()) | |
defer func() { | |
okerr := "OK" | |
if err != nil { | |
okerr = fmt.Sprintf("ERR: %s", err) | |
} | |
log.Infof("-job %s %s", job.CallString(), okerr) | |
}() | |
} | |
var errorMessage = bytes.NewBuffer(nil) | |
job.Stderr.Add(errorMessage) | |
if job.handler == nil { | |
err = fmt.Errorf("%s: command not found", job.Name) | |
} else { | |
err = job.handler(job) | |
job.end = time.Now() | |
} | |
if job.closeIO { | |
// Wait for all background tasks to complete | |
if err := job.Stdout.Close(); err != nil { | |
return err | |
} | |
if err := job.Stderr.Close(); err != nil { | |
return err | |
} | |
if err := job.Stdin.Close(); err != nil { | |
return err | |
} | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment