/
process.go
230 lines (203 loc) · 5.08 KB
/
process.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package gophpfpm
import (
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"path"
"regexp"
"strconv"
"syscall"
"time"
"github.com/go-ini/ini"
)
// Process describes a minimalistic php-fpm config
// that runs only 1 pool
type Process struct {
// path to php-fpm executable
Exec string
// path to the config file
ConfigFile string
// username of the FastCGI process
User string
// The address on which to accept FastCGI requests.
// Valid syntaxes are: 'ip.add.re.ss:port', 'port',
// '/path/to/unix/socket'. This option is mandatory for each pool.
Listen string
// path of the PID file
PidFile string
// path of the error log
ErrorLog string
// cmd stores the command of the running process
cmd *exec.Cmd
}
// NewProcess creates a new process descriptor
func NewProcess(phpFpm string) *Process {
return &Process{
Exec: phpFpm,
}
}
// SaveConfig generates config file according to the
// process attributes
func (proc *Process) SaveConfig(path string) {
proc.ConfigFile = path
proc.Config().SaveTo(proc.ConfigFile)
}
// Config generates an minimalistic config ini file
// in *ini.File format. You may then use SaveTo(path)
// to save it
func (proc *Process) Config() (f *ini.File) {
f = ini.Empty()
f.NewSection("global")
f.Section("global").NewKey("pid", proc.PidFile)
f.Section("global").NewKey("error_log", proc.ErrorLog)
f.NewSection("www")
f.Section("www").NewKey("listen", proc.Listen)
f.Section("www").NewKey("pm", "dynamic")
f.Section("www").NewKey("pm.max_children", "5")
f.Section("www").NewKey("pm.start_servers", "2")
f.Section("www").NewKey("pm.min_spare_servers", "1")
f.Section("www").NewKey("pm.max_spare_servers", "3")
if proc.User != "" {
f.Section("www").NewKey("user", proc.User)
}
return
}
// SetDatadir sets default config values according
// with reference to the folder prefix
//
// Equals to running these 3 statements:
// process.PidFile = basepath + "/phpfpm.pid"
// process.ErrorLog = basepath + "/phpfpm.error_log"
// process.Listen = basepath + "/phpfpm.sock"
func (proc *Process) SetDatadir(prefix string) {
// FIXME: add error if the prefix folder doesn't exists
// or is not a folder
proc.PidFile = path.Join(prefix, "phpfpm.pid")
proc.ErrorLog = path.Join(prefix, "phpfpm.error_log")
proc.Listen = path.Join(prefix, "phpfpm.sock")
}
// Start starts the php-fpm process
// in foreground mode instead of daemonize
func (proc *Process) Start() (err error) {
proc.cmd = &exec.Cmd{
Path: proc.Exec,
Args: append([]string{proc.Exec},
"--fpm-config", proc.ConfigFile,
"-n", // no php.ini file
"-e"), // extended information
}
if cmbOut, err := proc.cmd.CombinedOutput(); err != nil {
var ok bool
var exitErr *exec.ExitError
if exitErr, ok = err.(*exec.ExitError); !ok {
// no an exit error
return err
}
if !exitErr.ProcessState.Success() {
// unsuccessful exitErr
return fmt.Errorf("unsuccessful exit. error %s\noutput:\n%s",
exitErr.ProcessState, cmbOut)
}
}
pid := <-proc.waitPid()
spawned, err := os.FindProcess(pid)
if err != nil {
return
}
proc.cmd.Process = spawned
// wait until the service is connectable
// or time out
select {
case <-proc.waitConn():
// do nothing
case <-time.After(time.Second * 10):
// wait 10 seconds or timeout
err = fmt.Errorf("time out")
}
return
}
// read pid from pid
func (proc *Process) pid() (pid int, err error) {
f, err := os.Open(proc.PidFile)
if err != nil {
return
}
b, err := ioutil.ReadAll(f)
if err != nil {
return
}
pid64, err := strconv.ParseInt(string(b), 10, 64)
pid = int(pid64)
return
}
// wait until pid file readable
func (proc *Process) waitPid() <-chan int {
cout := make(chan int)
go func() {
for {
if pid, err := proc.pid(); err != nil {
time.Sleep(time.Millisecond * 2)
} else {
cout <- pid
break
}
}
}()
return cout
}
func (proc *Process) waitConn() <-chan net.Conn {
chanConn := make(chan net.Conn)
go func() {
for {
if conn, err := net.Dial(proc.Address()); err != nil {
time.Sleep(time.Millisecond * 2)
} else {
chanConn <- conn
break
}
}
}()
return chanConn
}
// Address returns networkk and address that fits
// the use of either net.Dial or net.Listen
func (proc *Process) Address() (network, address string) {
reIP := regexp.MustCompile("^(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})\\:(\\d{2,5}$)")
rePort := regexp.MustCompile("^(\\d+)$")
switch {
case reIP.MatchString(proc.Listen):
network = "tcp"
address = proc.Listen
case rePort.MatchString(proc.Listen):
network = "tcp"
address = ":" + proc.Listen
default:
network = "unix"
address = proc.Listen
}
return
}
// Stop stops the php-fpm process with SIGINT
// instead of killing
func (proc *Process) Stop() error {
return proc.cmd.Process.Signal(os.Interrupt)
}
// Wait wait for the process to finish
func (proc *Process) Wait() (err error) {
for {
if err = proc.cmd.Process.Signal(syscall.Signal(0)); err != nil {
switch err.Error() {
case "os: process already finished":
fallthrough
case "no such process":
err = nil
}
break
} else {
time.Sleep(time.Millisecond * 2)
}
}
return
}