diff options
author | Valery Piashchynski <[email protected]> | 2020-04-15 20:16:46 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-04-15 20:16:46 +0300 |
commit | 6625950ac6f382f6a46bc52bf17908ef13bdc429 (patch) | |
tree | 486bdfd86f8f1c0a269cd9dfdf46c1f73943524e /server_config.go | |
parent | cc9c74665a9cabcfb006ab6b7a9beafb95ff7316 (diff) |
Add user to worker
Diffstat (limited to 'server_config.go')
-rw-r--r-- | server_config.go | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/server_config.go b/server_config.go index 5403ff01..7b11e309 100644 --- a/server_config.go +++ b/server_config.go @@ -21,6 +21,9 @@ type ServerConfig struct { // Command includes command strings with all the parameters, example: "php worker.php pipes". Command string + // User under which process is starting + User string + // CommandProducer overwrites CommandProducer CommandProducer @@ -38,7 +41,7 @@ type ServerConfig struct { Pool *Config // values defines set of values to be passed to the command context. - mu sync.Mutex + mu *sync.Mutex env map[string]string } @@ -51,6 +54,8 @@ func (cfg *ServerConfig) InitDefaults() error { cfg.Pool = &Config{} } + cfg.mu = &sync.Mutex{} + return cfg.Pool.InitDefaults() } @@ -96,7 +101,8 @@ func (cfg *ServerConfig) GetEnv() (env []string) { return } -// makeCommands returns new command provider based on configured options. +//=================================== PRIVATE METHODS ====================================================== + func (cfg *ServerConfig) makeCommand() func() *exec.Cmd { cfg.mu.Lock() defer cfg.mu.Unlock() @@ -105,11 +111,22 @@ func (cfg *ServerConfig) makeCommand() func() *exec.Cmd { return cfg.CommandProducer(cfg) } - var cmd = strings.Split(cfg.Command, " ") + var cmdArgs []string + cmdArgs = append(cmdArgs, strings.Split(cfg.Command, " ")...) + return func() *exec.Cmd { - cmd := exec.Command(cmd[0], cmd[1:]...) + cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) osutil.IsolateProcess(cmd) + // if user is not empty, and OS is linux or macos + // execute php worker from that particular user + if cfg.User != "" { + err := osutil.ExecuteFromUser(cmd, cfg.User) + if err != nil { + return nil + } + } + cmd.Env = cfg.GetEnv() return cmd |