Skip to content

Commit

Permalink
feat: additional flag to force start manager-api (#1898)
Browse files Browse the repository at this point in the history
  • Loading branch information
bisakhmondal committed Jun 24, 2021
1 parent 1fa139f commit 4612d02
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 3 additions & 1 deletion api/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (

var (
configFile string
forceStart bool
)

var rootCmd = &cobra.Command{
Expand All @@ -65,6 +66,7 @@ func init() {

rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "./conf/conf.yml", "config file")
rootCmd.PersistentFlags().StringVarP(&conf.WorkDir, "work-dir", "p", ".", "current work directory")
rootCmd.PersistentFlags().BoolVarP(&forceStart, "force", "f", false, "force start manager-api")

rootCmd.AddCommand(
newVersionCommand(),
Expand All @@ -86,7 +88,7 @@ func manageAPI() error {
conf.InitConf()
log.InitLogger()

if err := utils.WritePID(conf.PIDPath); err != nil {
if err := utils.WritePID(conf.PIDPath, forceStart); err != nil {
log.Errorf("failed to write pid: %s", err)
return err
}
Expand Down
9 changes: 6 additions & 3 deletions api/internal/utils/pid.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ import (
)

// WritePID write pid to the given file path.
func WritePID(filepath string) error {
if _, err := os.Stat(filepath); err == nil {
return fmt.Errorf("instance of Manager API already running: a pid file exists in %s", filepath)
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)
}
fmt.Printf("Force starting new instance. Another instance of Manager API maybe running with pid %d\n", pid)
}
pid := os.Getpid()
f, err := os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC|os.O_CREATE, 0600)
Expand Down

0 comments on commit 4612d02

Please sign in to comment.