Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update logrus to 1.0, use Entry.Writer to catch exec stdout #424

Merged
merged 1 commit into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 16 additions & 28 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package commands
import (
"context"
"fmt"
"io"
"os/exec"
"sync"
"syscall"
Expand All @@ -15,14 +14,13 @@ import (

// Command wraps an os/exec.Cmd with a timeout, logging, and arg parsing.
type Command struct {
Name string // this gets used only in logs, defaults to Exec
Cmd *exec.Cmd
Exec string
Args []string
Timeout time.Duration
logger io.WriteCloser
logFields log.Fields
lock *sync.Mutex
Name string // this gets used only in logs, defaults to Exec
Cmd *exec.Cmd
Exec string
Args []string
Timeout time.Duration
logger log.Entry
lock *sync.Mutex
}

// NewCommand parses JSON config into a Command
Expand All @@ -31,14 +29,14 @@ func NewCommand(rawArgs interface{}, timeout time.Duration, fields log.Fields) (
if err != nil {
return nil, err
}
logger := log.WithFields(fields)
cmd := &Command{
Name: exec, // override this in caller
Exec: exec,
Args: args,
Timeout: timeout,
lock: &sync.Mutex{},
logger: log.StandardLogger().Writer(),
logFields: fields,
Name: exec, // override this in caller
Exec: exec,
Args: args,
Timeout: timeout,
lock: &sync.Mutex{},
logger: *logger,
} // exec.Cmd created at Run
return cmd, nil
}
Expand All @@ -56,8 +54,8 @@ func (c *Command) Run(pctx context.Context, bus *events.EventBus) {
c.lock.Lock()
log.Debugf("%s.Run start", c.Name)
c.setUpCmd()
c.Cmd.Stdout = c.logger
c.Cmd.Stderr = c.logger
c.Cmd.Stdout = c.logger.Writer()
c.Cmd.Stderr = c.logger.Writer()

var (
ctx context.Context
Expand Down Expand Up @@ -151,13 +149,3 @@ func (c *Command) Kill() {
syscall.Kill(-c.Cmd.Process.Pid, syscall.SIGKILL)
}
}

// CloseLogs safely closes the io.WriteCloser we're using to pipe logs
func (c *Command) CloseLogs() {
// need to nil check these because they might have been closed
// concurrently.
if c != nil && c.logger != nil {
c.logger.Close()
}
return
}
8 changes: 4 additions & 4 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ homepage: https://www.joyent.com/containerpilot
license: MPL-2.0
import:
- package: github.com/Sirupsen/logrus
version: ~0.9.0
version: 1.0.0
- package: github.com/hashicorp/consul
version: ~0.8.4
subpackages:
Expand Down
1 change: 0 additions & 1 deletion jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@ func (job *Job) cleanup(ctx context.Context, cancel context.CancelFunc) {
}
}
cancel()
job.exec.CloseLogs()
job.Deregister() // deregister from Consul
job.Unsubscribe(job.Bus) // deregister from events
job.Bus.Publish(events.Event{Code: events.Stopped, Source: job.Name})
Expand Down