starsz commented on a change in pull request #2054:
URL: https://github.com/apache/apisix-dashboard/pull/2054#discussion_r698544968
##########
File path: api/internal/utils/pid.go
##########
@@ -18,19 +18,62 @@
package utils
import (
+ "bytes"
"fmt"
"io/ioutil"
"os"
+ "runtime"
"strconv"
+ "syscall"
+ "time"
+
+ "github.com/pkg/errors"
)
// WritePID write pid to the given file path.
func WritePID(filepath string, forceStart bool) error {
- if pid, err := ReadPID(filepath); err == nil {
- if !forceStart {
- return fmt.Errorf("Instance of Manager API maybe
running with a pid %d. If not, please run Manager API with '-f' or '--force'
flag\n", pid)
+ pidStatAction := func() error {
+ pid, err := ReadPID(filepath)
+ if err != nil {
+ return nil
+ }
+ // In unix system this always succeeds, doesn't ensure if the
process at all exist or not.
+ proc, err := os.FindProcess(pid)
+ if err != nil {
+ return errors.Errorf("instance of Manager API maybe
running with a pid %d. If not, please run Manager API with '-f' or '--force'
flag. Error :%v\n", pid, err)
+ }
+
+ // Traditional unix way to check the existence of the
particular PID.
+ err = proc.Signal(syscall.Signal(0))
+ // No entry of the process in process control block.
+ if err != nil {
+ return nil
Review comment:
Ditto.
##########
File path: api/internal/utils/pid.go
##########
@@ -18,19 +18,62 @@
package utils
import (
+ "bytes"
"fmt"
"io/ioutil"
"os"
+ "runtime"
"strconv"
+ "syscall"
+ "time"
+
+ "github.com/pkg/errors"
)
// WritePID write pid to the given file path.
func WritePID(filepath string, forceStart bool) error {
- if pid, err := ReadPID(filepath); err == nil {
- if !forceStart {
- return fmt.Errorf("Instance of Manager API maybe
running with a pid %d. If not, please run Manager API with '-f' or '--force'
flag\n", pid)
+ pidStatAction := func() error {
+ pid, err := ReadPID(filepath)
+ if err != nil {
+ return nil
+ }
+ // In unix system this always succeeds, doesn't ensure if the
process at all exist or not.
+ proc, err := os.FindProcess(pid)
+ if err != nil {
+ return errors.Errorf("instance of Manager API maybe
running with a pid %d. If not, please run Manager API with '-f' or '--force'
flag. Error :%v\n", pid, err)
+ }
+
+ // Traditional unix way to check the existence of the
particular PID.
+ err = proc.Signal(syscall.Signal(0))
+ // No entry of the process in process control block.
+ if err != nil {
+ return nil
+ }
+
+ // Windows has a tendency to pick unused old PIDs and allocate
that to new process, so we won't kill it.
+ if runtime.GOOS != "windows" && forceStart {
+ fmt.Println("Killing existing instance of Manager API")
+ if err := proc.Signal(syscall.SIGINT); err != nil {
+ return errors.Wrapf(err, "failed to kill the
existing process of PID %d ", pid)
+ }
+ // Wait for graceful shutdown.
+ for {
+ if err := proc.Signal(syscall.Signal(0)); err
!= nil {
+ break
+ }
+ time.Sleep(time.Second)
+ }
+ fmt.Println("Existing instance of Manager API
successfully killed")
Review comment:
```suggestion
fmt.Println("Existing instance of Manager API
successfully exited")
```
##########
File path: api/internal/utils/pid.go
##########
@@ -18,19 +18,62 @@
package utils
import (
+ "bytes"
"fmt"
"io/ioutil"
"os"
+ "runtime"
"strconv"
+ "syscall"
+ "time"
+
+ "github.com/pkg/errors"
)
// WritePID write pid to the given file path.
func WritePID(filepath string, forceStart bool) error {
- if pid, err := ReadPID(filepath); err == nil {
- if !forceStart {
- return fmt.Errorf("Instance of Manager API maybe
running with a pid %d. If not, please run Manager API with '-f' or '--force'
flag\n", pid)
+ pidStatAction := func() error {
+ pid, err := ReadPID(filepath)
+ if err != nil {
+ return nil
Review comment:
LGTM.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]