From 5eb33546787848751a915c15cd4e65a66e33b24f Mon Sep 17 00:00:00 2001 From: Chris Bednarski Date: Tue, 15 Sep 2015 13:45:48 -0700 Subject: [PATCH] Update Java driver to use Executor --- client/driver/exec.go | 4 +--- client/driver/java.go | 27 +++++++++++++++------------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/client/driver/exec.go b/client/driver/exec.go index 7426f9fd4..622fa8dd7 100644 --- a/client/driver/exec.go +++ b/client/driver/exec.go @@ -81,7 +81,7 @@ func (d *ExecDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, erro // Find the process cmd, err := nexec.OpenPid(pid) - if cmd == nil || err != nil { + if err != nil { return nil, fmt.Errorf("failed to find PID %d: %v", pid, err) } @@ -110,8 +110,6 @@ func (h *execHandle) Update(task *structs.Task) error { return nil } -// Kill is used to terminate the task. We send an Interrupt -// and then provide a 5 second grace period before doing a Kill. func (h *execHandle) Kill() error { h.cmd.Shutdown() select { diff --git a/client/driver/java.go b/client/driver/java.go index b5e166205..53d4c2b23 100644 --- a/client/driver/java.go +++ b/client/driver/java.go @@ -27,7 +27,7 @@ type JavaDriver struct { // javaHandle is returned from Start/Open as a handle to the PID type javaHandle struct { - proc *os.Process + cmd nexec.Executor waitCh chan error doneCh chan struct{} } @@ -129,7 +129,11 @@ func (d *JavaDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, // Setup the command // Assumes Java is in the $PATH, but could probably be detected - cmd := exec.Command("java", args...) + cmd := nexec.Command("java", args...) + err := cmd.Limit(task.Resources) + if err != nil { + return nil, fmt.Errorf("failed to constrain resources: %s", err) + } err = cmd.Start() if err != nil { return nil, fmt.Errorf("failed to start source: %v", err) @@ -137,7 +141,7 @@ func (d *JavaDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, // Return a driver handle h := &javaHandle{ - proc: cmd.Process, + cmd: cmd, doneCh: make(chan struct{}), waitCh: make(chan error, 1), } @@ -156,13 +160,13 @@ func (d *JavaDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, erro // Find the process proc, err := os.FindProcess(pid) - if proc == nil || err != nil { + if err != nil { return nil, fmt.Errorf("failed to find PID %d: %v", pid, err) } // Return a driver handle h := &javaHandle{ - proc: proc, + cmd: cmd, doneCh: make(chan struct{}), waitCh: make(chan error, 1), } @@ -173,7 +177,8 @@ func (d *JavaDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, erro func (h *javaHandle) ID() string { // Return a handle to the PID - return fmt.Sprintf("PID:%d", h.proc.Pid) + pid, _ := h.cmd.Pid() + return fmt.Sprintf("PID:%d", pid) } func (h *javaHandle) WaitCh() chan error { @@ -185,24 +190,22 @@ func (h *javaHandle) Update(task *structs.Task) error { return nil } -// Kill is used to terminate the task. We send an Interrupt -// and then provide a 5 second grace period before doing a Kill. func (h *javaHandle) Kill() error { - h.proc.Signal(unix.SIGTERM) + h.cmd.Shutdown() select { case <-h.doneCh: return nil case <-time.After(5 * time.Second): - return h.proc.Kill() + return h.cmd.ForceStop() } } func (h *javaHandle) run() { - ps, err := h.proc.Wait() + err := h.cmd.Wait() close(h.doneCh) if err != nil { h.waitCh <- err - } else if !ps.Success() { + } else if !h.cmd.Command().ProcessState.Success() { h.waitCh <- fmt.Errorf("task exited with error") } close(h.waitCh)