From 623a539f23ba2a3d051a522d679780aefcf06e6f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 16 Mar 2023 15:22:54 +0800 Subject: 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 --- cmd/web.go | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'cmd') 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 -- cgit v1.2.3