Skip to content

Commit 9717091

Browse files
authored
Merge pull request #610 from appleboy/pid
feat: support pid file.
2 parents 19570f2 + 9f57598 commit 9717091

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

cmd/web.go

+10
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ and it takes care of all the other things for you`,
6060
Value: "custom/conf/app.ini",
6161
Usage: "Custom configuration file path",
6262
},
63+
cli.StringFlag{
64+
Name: "pid, P",
65+
Value: "custom/run/app.pid",
66+
Usage: "Custom pid file path",
67+
},
6368
},
6469
}
6570

@@ -156,6 +161,11 @@ func runWeb(ctx *cli.Context) error {
156161
if ctx.IsSet("config") {
157162
setting.CustomConf = ctx.String("config")
158163
}
164+
165+
if ctx.IsSet("pid") {
166+
setting.CustomPID = ctx.String("pid")
167+
}
168+
159169
routers.GlobalInit()
160170

161171
m := newMacaron()

modules/setting/setting.go

+23
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ var (
392392
Cfg *ini.File
393393
CustomPath string // Custom directory path
394394
CustomConf string
395+
CustomPID string
395396
ProdMode bool
396397
RunUser string
397398
IsWindows bool
@@ -471,6 +472,22 @@ func IsRunUserMatchCurrentUser(runUser string) (string, bool) {
471472
return currentUser, runUser == currentUser
472473
}
473474

475+
func createPIDFile(pidPath string) {
476+
currentPid := os.Getpid()
477+
if err := os.MkdirAll(filepath.Dir(pidPath), os.ModePerm); err != nil {
478+
log.Fatal(4, "Can't create PID folder on %s", err)
479+
}
480+
481+
file, err := os.Create(pidPath)
482+
if err != nil {
483+
log.Fatal(4, "Can't create PID file: %v", err)
484+
}
485+
defer file.Close()
486+
if _, err := file.WriteString(strconv.FormatInt(int64(currentPid), 10)); err != nil {
487+
log.Fatal(4, "Can'write PID information on %s", err)
488+
}
489+
}
490+
474491
// NewContext initializes configuration context.
475492
// NOTE: do not print any log except error.
476493
func NewContext() {
@@ -498,6 +515,12 @@ please consider changing to GITEA_CUSTOM`)
498515
}
499516
}
500517

518+
if len(CustomPID) == 0 {
519+
CustomPID = CustomPath + "/run/app.pid"
520+
}
521+
522+
createPIDFile(CustomPID)
523+
501524
if len(CustomConf) == 0 {
502525
CustomConf = CustomPath + "/conf/app.ini"
503526
}

0 commit comments

Comments
 (0)