diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-03-16 15:22:54 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-16 15:22:54 +0800 |
commit | 623a539f23ba2a3d051a522d679780aefcf06e6f (patch) | |
tree | f1590b1260101911d33230d176ce5bbc1fba4e42 /cmd | |
parent | 0f2361feddb2339d184b71864586ce1e477abee0 (diff) | |
download | gitea-623a539f23ba2a3d051a522d679780aefcf06e6f.tar.gz gitea-623a539f23ba2a3d051a522d679780aefcf06e6f.zip |
Move pidfile creation from setting to web cmd package (#23285)
Creating pid file should not belong to setting package and only web
command needs that. So this PR moves pidfile creation from setting
package to web command package to keep setting package more readable.
I marked this as `break` because the PIDFile path moved. For those who
have used the pid build argument, it has to be changed.
---------
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/web.go | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/cmd/web.go b/cmd/web.go index 8722ddb609..11620c6b00 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -9,6 +9,8 @@ import ( "net" "net/http" "os" + "path/filepath" + "strconv" "strings" _ "net/http/pprof" // Used for debugging if enabled and a web server is running @@ -25,6 +27,9 @@ import ( ini "gopkg.in/ini.v1" ) +// PIDFile could be set from build tag +var PIDFile = "/run/gitea.pid" + // CmdWeb represents the available web sub-command. var CmdWeb = cli.Command{ Name: "web", @@ -45,7 +50,7 @@ and it takes care of all the other things for you`, }, cli.StringFlag{ Name: "pid, P", - Value: setting.PIDFile, + Value: PIDFile, Usage: "Custom pid file path", }, cli.BoolFlag{ @@ -81,6 +86,22 @@ func runHTTPRedirector() { } } +func createPIDFile(pidPath string) { + currentPid := os.Getpid() + if err := os.MkdirAll(filepath.Dir(pidPath), os.ModePerm); err != nil { + log.Fatal("Failed to create PID folder: %v", err) + } + + file, err := os.Create(pidPath) + if err != nil { + log.Fatal("Failed to create PID file: %v", err) + } + defer file.Close() + if _, err := file.WriteString(strconv.FormatInt(int64(currentPid), 10)); err != nil { + log.Fatal("Failed to write PID information: %v", err) + } +} + func runWeb(ctx *cli.Context) error { if ctx.Bool("verbose") { _ = log.DelLogger("console") @@ -107,8 +128,7 @@ func runWeb(ctx *cli.Context) error { // Set pid file setting if ctx.IsSet("pid") { - setting.PIDFile = ctx.String("pid") - setting.WritePIDFile = true + createPIDFile(ctx.String("pid")) } // Perform pre-initialization |