track cmd failures

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-06-16 11:59:40 -07:00
parent 2e37ee24f7
commit 5d4ad13b77
3 changed files with 11 additions and 138 deletions

4
run.go
View File

@ -8,7 +8,9 @@ import "io"
import "fmt" import "fmt"
import "os" import "os"
import "bufio" import "bufio"
import "github.com/svent/go-nbreader" import "github.com/svent/go-nbreader"
// import "github.com/davecgh/go-spew/spew"
import "log" import "log"
// import "git.wit.com/wit/log" // import "git.wit.com/wit/log"
@ -110,6 +112,7 @@ func (cmd *Shell) Exec(cmdline string) {
// time.Sleep(2 * time.Second) // putting this here doesn't help STDOUT flush() // time.Sleep(2 * time.Second) // putting this here doesn't help STDOUT flush()
if (err != nil) { if (err != nil) {
cmd.Fail = true
cmd.Error = err cmd.Error = err
log.Println("process.Wait() END err =", err.Error()) log.Println("process.Wait() END err =", err.Error())
} else { } else {
@ -118,6 +121,7 @@ func (cmd *Shell) Exec(cmdline string) {
return return
} }
// nonblocking read until file errors
func (cmd *Shell) Capture(f *File) { func (cmd *Shell) Capture(f *File) {
// log.Debugln("nbrREADER() START") // log.Debugln("nbrREADER() START")

110
shell.go
View File

@ -78,95 +78,6 @@ func SetStderr(newerr *os.File) {
} }
/* /*
// NOTE: this might cause problems:
// always remove the newlines at the end ?
func OldRun(cmdline string) string {
log.Println("shell.Run() START " + cmdline)
cmd := Chomp(cmdline) // this is like 'chomp' in perl
cmdArgs := strings.Fields(cmd)
if (len(cmdArgs) == 0) {
handleError(fmt.Errorf("cmdline == ''"), 0)
log.Debug("END ", cmd)
return "" // nothing to do
}
if (cmdArgs[0] == "cd") {
if (len(cmdArgs) > 1) {
log.Println("os.Chdir()", cmd)
os.Chdir(cmdArgs[1])
}
handleError(nil, 0)
log.Debug("END ", cmd)
return "" // nothing to do
}
process := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...)
pstdout, _ := process.StdoutPipe()
pstderr, _ := process.StderrPipe()
if (spewOn) {
spew.Dump(pstdout)
}
process.Start()
if (shellStdout == nil) {
shellStdout = os.Stdout
}
f := bufio.NewWriter(shellStdout)
newreader := bufio.NewReader(pstdout)
nbr := nbreader.NewNBReader(newreader, 1024)
tmp := bufio.NewReader(pstderr)
go nonBlockingReader(tmp, shellStderr, f)
totalCount := 0
var dead bool = false
for (dead == false) {
time.Sleep(time.Duration(msecDelay) * time.Millisecond) // only check the buffer 500 times a second
// log.Println("sleep done")
var empty bool = false
// tight loop that reads 1K at a time until buffer is empty
for (empty == false) {
oneByte := make([]byte, 1024)
count, err := nbr.Read(oneByte)
totalCount += count
if (err != nil) {
log.Debug("Read() count = ", count, "err = ", err)
oneByte = make([]byte, 1024)
count, err = nbr.Read(oneByte)
log.Debug("STDOUT: count = ", count)
if (quiet == false) {
f.Write(oneByte[0:count])
f.Flush()
}
empty = true
dead = true
}
// f.Write([]byte(string(oneByte)))
if (count == 0) {
empty = true
} else {
log.Debug("STDOUT: count = ", count)
io.WriteString(&bytesBuffer, string(oneByte))
if (quiet == false) {
f.Write(oneByte[0:count])
f.Flush()
}
}
}
if (totalCount != 0) {
log.Debug("STDOUT: totalCount = ", totalCount)
totalCount = 0
}
}
err := process.Wait() err := process.Wait()
if err != nil { if err != nil {
@ -179,27 +90,6 @@ func OldRun(cmdline string) string {
log.Debug("END ", cmdline) log.Debug("END ", cmdline)
handleError(err, -1) handleError(err, -1)
return "" return ""
}
// log.Println("shell.Run() END buf =", bytesBuffer)
// convert this to a byte array and then trip NULLs
// WTF this copies nulls with b.String() is fucking insanly stupid
byteSlice := bytesBuffer.Bytes()
b := bytes.Trim(byteSlice, "\x00")
log.Debug("shell.Run() END b =", b)
// reset the bytesBuffer
bytesBuffer.Reset()
// NOTE: this might cause problems:
// this removes the newlines at the end
tmp2 := string(b)
tmp2 = strings.TrimSuffix(tmp2, "\n")
handleError(nil, 0)
log.Println("shell.Run() END ", cmdline)
return Chomp(b)
}
*/ */
func Daemon(cmdline string, timeout time.Duration) int { func Daemon(cmdline string, timeout time.Duration) int {

View File

@ -10,22 +10,6 @@ var FileMap map[string]*File
var readBufferSize int var readBufferSize int
/*
type File struct {
Name string
BufferSize int
Buffer *bytes.Buffer
Fbytes []byte
TotalCount int
Empty bool
Dead bool
Fio io.ReadCloser // := process.StdoutPipe()
Fbufio *bufio.Reader // := bufio.NewReader(pOUT)
Fnbreader *nbreader.NBReader // := nbreader.NewNBReader(readOUT, 1024)
}
*/
type File struct { type File struct {
Name string Name string
// BufferSize int // BufferSize int
@ -45,6 +29,7 @@ type Shell struct {
Process *exec.Cmd Process *exec.Cmd
Done bool Done bool
Quiet bool Quiet bool
Fail bool
Error error Error error
Buffer *bytes.Buffer Buffer *bytes.Buffer
@ -57,19 +42,13 @@ type Shell struct {
Stderr *File Stderr *File
} }
// default values for Shell
func New() *Shell { func New() *Shell {
var tmp Shell var tmp Shell
tmp.Done = false
tmp.Fail = false
tmp.Quiet = quiet
return &tmp return &tmp
} }
/*
func FileCreate(f io.ReadCloser) *File {
var newfile File
newfile.Fio = f
newfile.Fbufio = bufio.NewReader(f)
newfile.Fnbreader = nbreader.NewNBReader(newfile.Fbufio, 1024)
return &newfile
}
*/