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>
This commit is contained in:
Lunny Xiao 2023-03-16 15:22:54 +08:00 committed by GitHub
parent 0f2361fedd
commit 623a539f23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 41 deletions

View File

@ -9,6 +9,8 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"path/filepath"
"strconv"
"strings" "strings"
_ "net/http/pprof" // Used for debugging if enabled and a web server is running _ "net/http/pprof" // Used for debugging if enabled and a web server is running
@ -25,6 +27,9 @@ import (
ini "gopkg.in/ini.v1" ini "gopkg.in/ini.v1"
) )
// PIDFile could be set from build tag
var PIDFile = "/run/gitea.pid"
// CmdWeb represents the available web sub-command. // CmdWeb represents the available web sub-command.
var CmdWeb = cli.Command{ var CmdWeb = cli.Command{
Name: "web", Name: "web",
@ -45,7 +50,7 @@ and it takes care of all the other things for you`,
}, },
cli.StringFlag{ cli.StringFlag{
Name: "pid, P", Name: "pid, P",
Value: setting.PIDFile, Value: PIDFile,
Usage: "Custom pid file path", Usage: "Custom pid file path",
}, },
cli.BoolFlag{ 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 { func runWeb(ctx *cli.Context) error {
if ctx.Bool("verbose") { if ctx.Bool("verbose") {
_ = log.DelLogger("console") _ = log.DelLogger("console")
@ -107,8 +128,7 @@ func runWeb(ctx *cli.Context) error {
// Set pid file setting // Set pid file setting
if ctx.IsSet("pid") { if ctx.IsSet("pid") {
setting.PIDFile = ctx.String("pid") createPIDFile(ctx.String("pid"))
setting.WritePIDFile = true
} }
// Perform pre-initialization // Perform pre-initialization

View File

@ -159,7 +159,7 @@ using the `LDFLAGS` environment variable for `make`. The appropriate settings ar
- For _`CustomConf`_ you should use `-X \"code.gitea.io/gitea/modules/setting.CustomConf=conf.ini\"` - For _`CustomConf`_ you should use `-X \"code.gitea.io/gitea/modules/setting.CustomConf=conf.ini\"`
- For _`AppWorkPath`_ you should use `-X \"code.gitea.io/gitea/modules/setting.AppWorkPath=working-path\"` - For _`AppWorkPath`_ you should use `-X \"code.gitea.io/gitea/modules/setting.AppWorkPath=working-path\"`
- For _`StaticRootPath`_ you should use `-X \"code.gitea.io/gitea/modules/setting.StaticRootPath=static-root-path\"` - For _`StaticRootPath`_ you should use `-X \"code.gitea.io/gitea/modules/setting.StaticRootPath=static-root-path\"`
- To change the default PID file location use `-X \"code.gitea.io/gitea/modules/setting.PIDFile=/run/gitea.pid\"` - To change the default PID file location use `-X \"code.gitea.io/gitea/cmd.PIDFile=/run/gitea.pid\"`
Add as many of the strings with their preceding `-X` to the `LDFLAGS` variable and run `make build` Add as many of the strings with their preceding `-X` to the `LDFLAGS` variable and run `make build`
with the appropriate `TAGS` as above. with the appropriate `TAGS` as above.

View File

@ -54,7 +54,11 @@ var (
// LoadDBSetting loads the database settings // LoadDBSetting loads the database settings
func LoadDBSetting() { func LoadDBSetting() {
sec := CfgProvider.Section("database") loadDBSetting(CfgProvider)
}
func loadDBSetting(rootCfg ConfigProvider) {
sec := rootCfg.Section("database")
Database.Type = DatabaseType(sec.Key("DB_TYPE").String()) Database.Type = DatabaseType(sec.Key("DB_TYPE").String())
defaultCharset := "utf8" defaultCharset := "utf8"

View File

@ -12,7 +12,6 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv"
"strings" "strings"
"time" "time"
@ -45,8 +44,6 @@ var (
CfgProvider ConfigProvider CfgProvider ConfigProvider
CustomPath string // Custom directory path CustomPath string // Custom directory path
CustomConf string CustomConf string
PIDFile = "/run/gitea.pid"
WritePIDFile bool
RunMode string RunMode string
RunUser string RunUser string
IsProd bool IsProd bool
@ -141,22 +138,6 @@ func IsRunUserMatchCurrentUser(runUser string) (string, bool) {
return currentUser, runUser == currentUser return currentUser, runUser == currentUser
} }
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)
}
}
// SetCustomPathAndConf will set CustomPath and CustomConf with reference to the // SetCustomPathAndConf will set CustomPath and CustomConf with reference to the
// GITEA_CUSTOM environment variable and with provided overrides before stepping // GITEA_CUSTOM environment variable and with provided overrides before stepping
// back to the default // back to the default
@ -218,17 +199,17 @@ func PrepareAppDataPath() error {
// InitProviderFromExistingFile initializes config provider from an existing config file (app.ini) // InitProviderFromExistingFile initializes config provider from an existing config file (app.ini)
func InitProviderFromExistingFile() { func InitProviderFromExistingFile() {
CfgProvider = newFileProviderFromConf(CustomConf, WritePIDFile, false, PIDFile, "") CfgProvider = newFileProviderFromConf(CustomConf, false, "")
} }
// InitProviderAllowEmpty initializes config provider from file, it's also fine that if the config file (app.ini) doesn't exist // InitProviderAllowEmpty initializes config provider from file, it's also fine that if the config file (app.ini) doesn't exist
func InitProviderAllowEmpty() { func InitProviderAllowEmpty() {
CfgProvider = newFileProviderFromConf(CustomConf, WritePIDFile, true, PIDFile, "") CfgProvider = newFileProviderFromConf(CustomConf, true, "")
} }
// InitProviderAndLoadCommonSettingsForTest initializes config provider and load common setttings for tests // InitProviderAndLoadCommonSettingsForTest initializes config provider and load common setttings for tests
func InitProviderAndLoadCommonSettingsForTest(extraConfigs ...string) { func InitProviderAndLoadCommonSettingsForTest(extraConfigs ...string) {
CfgProvider = newFileProviderFromConf(CustomConf, WritePIDFile, true, PIDFile, strings.Join(extraConfigs, "\n")) CfgProvider = newFileProviderFromConf(CustomConf, true, strings.Join(extraConfigs, "\n"))
loadCommonSettingsFrom(CfgProvider) loadCommonSettingsFrom(CfgProvider)
if err := PrepareAppDataPath(); err != nil { if err := PrepareAppDataPath(); err != nil {
log.Fatal("Can not prepare APP_DATA_PATH: %v", err) log.Fatal("Can not prepare APP_DATA_PATH: %v", err)
@ -241,13 +222,9 @@ func InitProviderAndLoadCommonSettingsForTest(extraConfigs ...string) {
// newFileProviderFromConf initializes configuration context. // newFileProviderFromConf initializes configuration context.
// NOTE: do not print any log except error. // NOTE: do not print any log except error.
func newFileProviderFromConf(customConf string, writePIDFile, allowEmpty bool, pidFile, extraConfig string) *ini.File { func newFileProviderFromConf(customConf string, allowEmpty bool, extraConfig string) *ini.File {
cfg := ini.Empty() cfg := ini.Empty()
if writePIDFile && len(pidFile) > 0 {
createPIDFile(pidFile)
}
isFile, err := util.IsFile(customConf) isFile, err := util.IsFile(customConf)
if err != nil { if err != nil {
log.Error("Unable to check if %s is a file. Error: %v", customConf, err) log.Error("Unable to check if %s is a file. Error: %v", customConf, err)
@ -380,7 +357,7 @@ func CreateOrAppendToCustomConf(purpose string, callback func(cfg *ini.File)) {
// LoadSettings initializes the settings for normal start up // LoadSettings initializes the settings for normal start up
func LoadSettings() { func LoadSettings() {
LoadDBSetting() loadDBSetting(CfgProvider)
loadServiceFrom(CfgProvider) loadServiceFrom(CfgProvider)
loadOAuth2ClientFrom(CfgProvider) loadOAuth2ClientFrom(CfgProvider)
InitLogs(false) InitLogs(false)
@ -401,7 +378,7 @@ func LoadSettings() {
// LoadSettingsForInstall initializes the settings for install // LoadSettingsForInstall initializes the settings for install
func LoadSettingsForInstall() { func LoadSettingsForInstall() {
LoadDBSetting() loadDBSetting(CfgProvider)
loadServiceFrom(CfgProvider) loadServiceFrom(CfgProvider)
loadMailerFrom(CfgProvider) loadMailerFrom(CfgProvider)
} }