Merge pull request #53 from hashicorp/f-executor-factory

Changed Default executor to use a factory
This commit is contained in:
Chris Bednarski 2015-09-15 17:44:27 -07:00
commit 3953cd3b2f
2 changed files with 30 additions and 7 deletions

View file

@ -22,6 +22,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"sync"
"github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/nomad/structs"
) )
@ -115,6 +116,23 @@ func OpenPid(pid int) (Executor, error) {
return executor, nil return executor, nil
} }
// ExecutorFactory is an interface for a function that returns an Executor. This
// allows us to create Executors dynamically.
type ExecutorFactory func() Executor
var executors []ExecutorFactory
var execFactoryMutex sync.Mutex
// Register an ExecutorFactory so we can create it with Default()
func Register(executor ExecutorFactory) {
execFactoryMutex.Lock()
if executors == nil {
executors = []ExecutorFactory{}
}
executors = append(executors, executor)
execFactoryMutex.Unlock()
}
// Default uses capability testing to give you the best available // Default uses capability testing to give you the best available
// executor based on your platform and execution environment. If you need a // executor based on your platform and execution environment. If you need a
// specific executor, call it directly. // specific executor, call it directly.
@ -123,13 +141,12 @@ func OpenPid(pid int) (Executor, error) {
// using a decorator pattern instead. // using a decorator pattern instead.
func Default() Executor { func Default() Executor {
// These will be IN ORDER and the first available will be used, so preferred // These will be IN ORDER and the first available will be used, so preferred
// ones should be at the top and fallbacks at the bottom. // ones should be at the top and fallbacks at the bottom. Note that if these
// TODO refactor this to be more lightweight. // are added via init() calls then the order may be a be a bit mysterious
executors := []Executor{ // even though it should be deterministic.
&LinuxExecutor{}, // TODO Make order more explicit
} for _, factory := range executors {
executor := factory()
for _, executor := range executors {
if executor.Available() { if executor.Available() {
return executor return executor
} }

View file

@ -44,6 +44,12 @@ func SetGID(command *cmd, groupid string) error {
return nil return nil
} }
func init() {
Register(func() Executor {
return &LinuxExecutor{}
})
}
// Linux executor is designed to run on linux kernel 2.8+. It will fork/exec as // Linux executor is designed to run on linux kernel 2.8+. It will fork/exec as
// a user you specify and limit resources using rlimit. // a user you specify and limit resources using rlimit.
type LinuxExecutor struct { type LinuxExecutor struct {