Remove err field from Job struct, fixes #11804
Signed-off-by: Antonio Murdaca <me@runcom.ninja>
This commit is contained in:
parent
121de9e4e3
commit
60085e22ff
1 changed files with 31 additions and 34 deletions
|
@ -17,12 +17,7 @@ import (
|
||||||
// download an archive from the internet, serve the http api, etc.
|
// download an archive from the internet, serve the http api, etc.
|
||||||
//
|
//
|
||||||
// The job API is designed after unix processes: a job has a name, arguments,
|
// The job API is designed after unix processes: a job has a name, arguments,
|
||||||
// environment variables, standard streams for input, output and error, and
|
// environment variables, standard streams for input, output and error.
|
||||||
// an exit status which can indicate success (0) or error (anything else).
|
|
||||||
//
|
|
||||||
// For status, 0 indicates success, and any other integers indicates an error.
|
|
||||||
// This allows for richer error reporting.
|
|
||||||
//
|
|
||||||
type Job struct {
|
type Job struct {
|
||||||
Eng *Engine
|
Eng *Engine
|
||||||
Name string
|
Name string
|
||||||
|
@ -32,7 +27,6 @@ type Job struct {
|
||||||
Stderr *Output
|
Stderr *Output
|
||||||
Stdin *Input
|
Stdin *Input
|
||||||
handler Handler
|
handler Handler
|
||||||
err error
|
|
||||||
end time.Time
|
end time.Time
|
||||||
closeIO bool
|
closeIO bool
|
||||||
|
|
||||||
|
@ -45,7 +39,22 @@ type Job struct {
|
||||||
|
|
||||||
// Run executes the job and blocks until the job completes.
|
// Run executes the job and blocks until the job completes.
|
||||||
// If the job fails it returns an error
|
// If the job fails it returns an error
|
||||||
func (job *Job) Run() error {
|
func (job *Job) Run() (err error) {
|
||||||
|
defer func() {
|
||||||
|
// Wait for all background tasks to complete
|
||||||
|
if job.closeIO {
|
||||||
|
if err := job.Stdout.Close(); err != nil {
|
||||||
|
logrus.Error(err)
|
||||||
|
}
|
||||||
|
if err := job.Stderr.Close(); err != nil {
|
||||||
|
logrus.Error(err)
|
||||||
|
}
|
||||||
|
if err := job.Stdin.Close(); err != nil {
|
||||||
|
logrus.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
if job.Eng.IsShutdown() && !job.GetenvBool("overrideShutdown") {
|
if job.Eng.IsShutdown() && !job.GetenvBool("overrideShutdown") {
|
||||||
return fmt.Errorf("engine is shutdown")
|
return fmt.Errorf("engine is shutdown")
|
||||||
}
|
}
|
||||||
|
@ -69,32 +78,25 @@ func (job *Job) Run() error {
|
||||||
if job.Eng.Logging {
|
if job.Eng.Logging {
|
||||||
logrus.Infof("+job %s", job.CallString())
|
logrus.Infof("+job %s", job.CallString())
|
||||||
defer func() {
|
defer func() {
|
||||||
// what if err is nil?
|
okerr := "OK"
|
||||||
logrus.Infof("-job %s%s", job.CallString(), job.err)
|
if err != nil {
|
||||||
|
okerr = fmt.Sprintf("ERR: %s", err)
|
||||||
|
}
|
||||||
|
logrus.Infof("-job %s %s", job.CallString(), okerr)
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
var errorMessage = bytes.NewBuffer(nil)
|
|
||||||
job.Stderr.Add(errorMessage)
|
|
||||||
if job.handler == nil {
|
if job.handler == nil {
|
||||||
job.err = fmt.Errorf("%s: command not found", job.Name)
|
return fmt.Errorf("%s: command not found", job.Name)
|
||||||
} else {
|
|
||||||
job.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 job.err
|
var errorMessage = bytes.NewBuffer(nil)
|
||||||
|
job.Stderr.Add(errorMessage)
|
||||||
|
|
||||||
|
err = job.handler(job)
|
||||||
|
job.end = time.Now()
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (job *Job) CallString() string {
|
func (job *Job) CallString() string {
|
||||||
|
@ -195,11 +197,6 @@ func (job *Job) Environ() map[string]string {
|
||||||
return job.env.Map()
|
return job.env.Map()
|
||||||
}
|
}
|
||||||
|
|
||||||
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.Stderr, prefixedFormat, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (job *Job) Printf(format string, args ...interface{}) (n int, err error) {
|
func (job *Job) Printf(format string, args ...interface{}) (n int, err error) {
|
||||||
return fmt.Fprintf(job.Stdout, format, args...)
|
return fmt.Fprintf(job.Stdout, format, args...)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue