Skip to content

Commit

Permalink
plugin: do not reopen fifos on recover task (#63)
Browse files Browse the repository at this point in the history
* plugin: do not reopen fifos on recover task

* handle: set the clock in recover handle
  • Loading branch information
shoenig authored Jul 8, 2023
1 parent 24867a7 commit ae8e167
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
18 changes: 3 additions & 15 deletions pkg/pledge/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/user"
"testing"

"github.com/shoenig/nomad-pledge/pkg/resources"
"github.com/shoenig/nomad-pledge/pkg/util"
"github.com/shoenig/test/must"
)

Expand All @@ -25,18 +25,6 @@ func lookupBin() string {
return defaultPledgeBin
}

type writeCloser struct {
io.Writer
}

func (wc *writeCloser) Close() error {
return nil
}

func noopCloser(w io.Writer) io.WriteCloser {
return &writeCloser{w}
}

func whoami() string {
u, err := user.Current()
if err != nil {
Expand All @@ -50,8 +38,8 @@ func testEnv() (*Environment, *bytes.Buffer, *bytes.Buffer) {
var err bytes.Buffer

return &Environment{
Out: noopCloser(&out),
Err: noopCloser(&err),
Out: util.NullCloser(&out),
Err: util.NullCloser(&err),
Env: map[string]string{},
Dir: ".",
User: whoami(),
Expand Down
13 changes: 3 additions & 10 deletions pkg/plugin/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func (p *PledgeDriver) RecoverTask(handle *drivers.TaskHandle) error {
return errors.New("failed to recover task, handle is nil")
}

p.logger.Warn("recovering task", "id", handle.Config.ID)
p.logger.Info("recovering task", "id", handle.Config.ID)

if _, exists := p.tasks.Get(handle.Config.ID); exists {
return nil // nothing to do
Expand All @@ -342,16 +342,11 @@ func (p *PledgeDriver) RecoverTask(handle *drivers.TaskHandle) error {
}

taskState.TaskConfig = handle.Config.Copy()
stdout, stderr, err := open(handle.Config.StdoutPath, handle.Config.StderrPath)
if err != nil {
p.logger.Error("failed to re-open log files", "error", err)
return fmt.Errorf("failed to open log file(s): %w", err)
}

// re-create the environment for pledge
env := &pledge.Environment{
Out: stdout,
Err: stderr,
Out: util.NullCloser(nil),
Err: util.NullCloser(nil),
Env: handle.Config.Env,
Dir: handle.Config.TaskDir().Dir,
User: handle.Config.User,
Expand All @@ -377,9 +372,7 @@ func (p *PledgeDriver) WaitTask(ctx context.Context, taskID string) (<-chan *dri
ch := make(chan *drivers.ExitResult)
go func() {
// todo: able to cancel ?
p.logger.Debug("WaitTask start Block")
handle.Block()
p.logger.Debug("WaitTask done Block")
result := handle.Status()
ch <- result.ExitResult
}()
Expand Down
2 changes: 2 additions & 0 deletions pkg/task/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ func NewHandle(runner pledge.Exec, config *drivers.TaskConfig) (*Handle, time.Ti
}

func RecreateHandle(runner pledge.Exec, config *drivers.TaskConfig, started time.Time) *Handle {
clock := libtime.SystemClock()
return &Handle{
pid: runner.PID(),
runner: runner,
config: config,
state: drivers.TaskStateUnknown,
clock: clock,
started: started,
result: new(drivers.ExitResult),
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/util/io.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package util

import (
"io"
)

func NullCloser(w io.Writer) io.WriteCloser {
return &writeCloser{w}
}

type writeCloser struct {
io.Writer
}

func (*writeCloser) Close() error {
return nil
}

0 comments on commit ae8e167

Please sign in to comment.