rename fifo methods for clarity
This commit is contained in:
parent
e87afe465b
commit
81f4f07ed7
|
@ -31,7 +31,7 @@ func TestFIFO(t *testing.T) {
|
|||
path = filepath.Join(dir, "fifo")
|
||||
}
|
||||
|
||||
readerOpenFn, err := New(path)
|
||||
readerOpenFn, err := CreateAndRead(path)
|
||||
require.NoError(err)
|
||||
|
||||
var reader io.ReadCloser
|
||||
|
@ -60,7 +60,7 @@ func TestFIFO(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
}()
|
||||
|
||||
writer, err := Open(path)
|
||||
writer, err := OpenWriter(path)
|
||||
require.NoError(err)
|
||||
for _, b := range toWrite {
|
||||
n, err := writer.Write(b)
|
||||
|
@ -94,7 +94,7 @@ func TestWriteClose(t *testing.T) {
|
|||
path = filepath.Join(dir, "fifo")
|
||||
}
|
||||
|
||||
readerOpenFn, err := New(path)
|
||||
readerOpenFn, err := CreateAndRead(path)
|
||||
require.NoError(err)
|
||||
var reader io.ReadCloser
|
||||
|
||||
|
@ -114,7 +114,7 @@ func TestWriteClose(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
}()
|
||||
|
||||
writer, err := Open(path)
|
||||
writer, err := OpenWriter(path)
|
||||
require.NoError(err)
|
||||
|
||||
var count int
|
||||
|
|
|
@ -10,12 +10,12 @@ import (
|
|||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// New creates a fifo at the given path, and returns an open function for reading.
|
||||
// CreateAndRead creates a fifo at the given path, and returns an open function for reading.
|
||||
// The fifo must not exist already, or that it's already a fifo file
|
||||
//
|
||||
// It returns a reader open function that may block until a writer opens
|
||||
// so it's advised to run it in a goroutine different from reader goroutine
|
||||
func New(path string) (func() (io.ReadCloser, error), error) {
|
||||
func CreateAndRead(path string) (func() (io.ReadCloser, error), error) {
|
||||
// create first
|
||||
if err := mkfifo(path, 0600); err != nil && !os.IsExist(err) {
|
||||
return nil, fmt.Errorf("error creating fifo %v: %v", path, err)
|
||||
|
@ -28,8 +28,8 @@ func New(path string) (func() (io.ReadCloser, error), error) {
|
|||
return openFn, nil
|
||||
}
|
||||
|
||||
// Open opens a fifo file for reading, assuming it already exists, returns io.WriteCloser
|
||||
func Open(path string) (io.WriteCloser, error) {
|
||||
// OpenWriter opens a fifo file for writer, assuming it already exists, returns io.WriteCloser
|
||||
func OpenWriter(path string) (io.WriteCloser, error) {
|
||||
return os.OpenFile(path, unix.O_WRONLY, os.ModeNamedPipe)
|
||||
}
|
||||
|
||||
|
|
|
@ -67,9 +67,9 @@ func (f *winFIFO) Close() error {
|
|||
return f.listener.Close()
|
||||
}
|
||||
|
||||
// New creates a fifo at the given path and returns an io.ReadCloser open for it.
|
||||
// CreateAndRead creates a fifo at the given path and returns an io.ReadCloser open for it.
|
||||
// The fifo must not already exist
|
||||
func New(path string) (func() (io.ReadCloser, error), error) {
|
||||
func CreateAndRead(path string) (func() (io.ReadCloser, error), error) {
|
||||
l, err := winio.ListenPipe(path, &winio.PipeConfig{
|
||||
InputBufferSize: PipeBufferSize,
|
||||
OutputBufferSize: PipeBufferSize,
|
||||
|
@ -88,7 +88,7 @@ func New(path string) (func() (io.ReadCloser, error), error) {
|
|||
}
|
||||
|
||||
// OpenWriter opens a fifo that already exists and returns an io.WriteCloser for it
|
||||
func Open(path string) (io.WriteCloser, error) {
|
||||
func OpenWriter(path string) (io.WriteCloser, error) {
|
||||
return winio.DialPipe(path, nil)
|
||||
}
|
||||
|
||||
|
|
|
@ -199,7 +199,7 @@ func (l *logRotatorWrapper) isRunning() bool {
|
|||
// processOutWriter to attach to the stdout or stderr of a process.
|
||||
func newLogRotatorWrapper(path string, logger hclog.Logger, rotator *logging.FileRotator) (*logRotatorWrapper, error) {
|
||||
logger.Info("opening fifo", "path", path)
|
||||
fifoOpenFn, err := fifo.New(path)
|
||||
fifoOpenFn, err := fifo.CreateAndRead(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create fifo for extracting logs: %v", err)
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ func TestLogmon_Start_rotate(t *testing.T) {
|
|||
lm := NewLogMon(testlog.HCLogger(t))
|
||||
require.NoError(lm.Start(cfg))
|
||||
|
||||
stdout, err := fifo.Open(stdoutFifoPath)
|
||||
stdout, err := fifo.OpenWriter(stdoutFifoPath)
|
||||
require.NoError(err)
|
||||
|
||||
// Write enough bytes such that the log is rotated
|
||||
|
@ -92,9 +92,9 @@ func TestLogmon_Start_restart(t *testing.T) {
|
|||
require.True(ok)
|
||||
require.NoError(lm.Start(cfg))
|
||||
|
||||
stdout, err := fifo.Open(stdoutFifoPath)
|
||||
stdout, err := fifo.OpenWriter(stdoutFifoPath)
|
||||
require.NoError(err)
|
||||
stderr, err := fifo.Open(stderrFifoPath)
|
||||
stderr, err := fifo.OpenWriter(stderrFifoPath)
|
||||
require.NoError(err)
|
||||
|
||||
// Write a string and assert it was written to the file
|
||||
|
@ -122,9 +122,9 @@ func TestLogmon_Start_restart(t *testing.T) {
|
|||
require.NoError(err)
|
||||
})
|
||||
|
||||
stdout, err = fifo.Open(stdoutFifoPath)
|
||||
stdout, err = fifo.OpenWriter(stdoutFifoPath)
|
||||
require.NoError(err)
|
||||
stderr, err = fifo.Open(stderrFifoPath)
|
||||
stderr, err = fifo.OpenWriter(stderrFifoPath)
|
||||
require.NoError(err)
|
||||
|
||||
_, err = stdout.Write([]byte("te"))
|
||||
|
@ -143,9 +143,9 @@ func TestLogmon_Start_restart(t *testing.T) {
|
|||
// Start logmon again and assert that it appended to the file
|
||||
require.NoError(lm.Start(cfg))
|
||||
|
||||
stdout, err = fifo.Open(stdoutFifoPath)
|
||||
stdout, err = fifo.OpenWriter(stdoutFifoPath)
|
||||
require.NoError(err)
|
||||
stderr, err = fifo.Open(stderrFifoPath)
|
||||
stderr, err = fifo.OpenWriter(stderrFifoPath)
|
||||
require.NoError(err)
|
||||
|
||||
_, err = stdout.Write([]byte("st\n"))
|
||||
|
|
|
@ -71,14 +71,14 @@ func (d *dockerLogger) Start(opts *StartOpts) error {
|
|||
}
|
||||
|
||||
if d.stdout == nil {
|
||||
stdout, err := fifo.Open(opts.Stdout)
|
||||
stdout, err := fifo.OpenWriter(opts.Stdout)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open fifo for path %s: %v", opts.Stdout, err)
|
||||
}
|
||||
d.stdout = stdout
|
||||
}
|
||||
if d.stderr == nil {
|
||||
stderr, err := fifo.Open(opts.Stderr)
|
||||
stderr, err := fifo.OpenWriter(opts.Stderr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open fifo for path %s: %v", opts.Stdout, err)
|
||||
}
|
||||
|
|
|
@ -125,13 +125,13 @@ func (h *taskHandle) run() {
|
|||
}
|
||||
|
||||
func (h *taskHandle) handleLogging(errCh chan<- error) {
|
||||
stdout, err := fifo.Open(h.taskConfig.StdoutPath)
|
||||
stdout, err := fifo.OpenWriter(h.taskConfig.StdoutPath)
|
||||
if err != nil {
|
||||
h.logger.Error("failed to write to stdout", "error", err)
|
||||
errCh <- err
|
||||
return
|
||||
}
|
||||
stderr, err := fifo.Open(h.taskConfig.StderrPath)
|
||||
stderr, err := fifo.OpenWriter(h.taskConfig.StderrPath)
|
||||
if err != nil {
|
||||
h.logger.Error("failed to write to stderr", "error", err)
|
||||
errCh <- err
|
||||
|
|
|
@ -148,7 +148,7 @@ func (nopCloser) Close() error { return nil }
|
|||
func (c *ExecCommand) Stdout() (io.WriteCloser, error) {
|
||||
if c.stdout == nil {
|
||||
if c.StdoutPath != "" {
|
||||
f, err := fifo.Open(c.StdoutPath)
|
||||
f, err := fifo.OpenWriter(c.StdoutPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create stdout: %v", err)
|
||||
}
|
||||
|
@ -164,7 +164,7 @@ func (c *ExecCommand) Stdout() (io.WriteCloser, error) {
|
|||
func (c *ExecCommand) Stderr() (io.WriteCloser, error) {
|
||||
if c.stderr == nil {
|
||||
if c.StderrPath != "" {
|
||||
f, err := fifo.Open(c.StderrPath)
|
||||
f, err := fifo.OpenWriter(c.StderrPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create stderr: %v", err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue