Skip to content

Commit

Permalink
[BREAKING] tools/phpfpm: Improve error handling
Browse files Browse the repository at this point in the history
* Improve the error handling of Process
* Breaking change to phpfpm.Process.Config() to add error return.
  (It's minor. I don't expect people are really using this
  internal tool library. But it's a breaking change nonetheless)
  • Loading branch information
yookoala committed Oct 13, 2020
1 parent 28534a1 commit 4b3caef
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
48 changes: 37 additions & 11 deletions tools/phpfpm/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,51 @@ func NewProcess(phpFpm string) *Process {

// SaveConfig generates config file according to the
// process attributes
func (proc *Process) SaveConfig(path string) {
func (proc *Process) SaveConfig(path string) (err error) {
proc.ConfigFile = path
proc.Config().SaveTo(proc.ConfigFile)
c, err := proc.Config()
if err != nil {
return
}
err = c.SaveTo(proc.ConfigFile)
return
}

// 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) {
func (proc *Process) Config() (f *ini.File, err error) {
var s *ini.Section
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", "static")
f.Section("www").NewKey("pm.max_children", fmt.Sprintf("%d", proc.Worker))

// global
if s, err = f.NewSection("global"); err != nil {
return
}
if _, err = s.NewKey("pid", proc.PidFile); err != nil {
return
}
if _, err = s.NewKey("error_log", proc.ErrorLog); err != nil {
return
}

// www
if s, err = f.NewSection("www"); err != nil {
return
}
if _, err = s.NewKey("listen", proc.Listen); err != nil {
return
}
if _, err = s.NewKey("pm", "static"); err != nil {
return
}
if _, err = s.NewKey("pm.max_children", fmt.Sprintf("%d", proc.Worker)); err != nil {
return
}
if proc.User != "" {
f.Section("www").NewKey("user", proc.User)
if _, err = s.NewKey("user", proc.User); err != nil {
return
}
}
return
}
Expand Down
5 changes: 4 additions & 1 deletion tools/phpfpm/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ func TestProcess_StartStop(t *testing.T) {
process := phpfpm.NewProcess(path)
process.SetDatadir(basepath + "/var")
process.User = username
process.SaveConfig(basepath + "/etc/test.startstop.conf")
if err := process.SaveConfig(basepath + "/etc/test.startstop.conf"); err != nil {
t.Errorf("unexpected error: %s", err.Error())
return
}

if err := process.Start(); err != nil {
t.Errorf("unexpected error: %s", err.Error())
Expand Down

0 comments on commit 4b3caef

Please sign in to comment.