Skip to content

Commit 623a539

Browse files
lunnyKN4CK3R
andauthored
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>
1 parent 0f2361f commit 623a539

File tree

4 files changed

+42
-41
lines changed

4 files changed

+42
-41
lines changed

cmd/web.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"net"
1010
"net/http"
1111
"os"
12+
"path/filepath"
13+
"strconv"
1214
"strings"
1315

1416
_ "net/http/pprof" // Used for debugging if enabled and a web server is running
@@ -25,6 +27,9 @@ import (
2527
ini "gopkg.in/ini.v1"
2628
)
2729

30+
// PIDFile could be set from build tag
31+
var PIDFile = "/run/gitea.pid"
32+
2833
// CmdWeb represents the available web sub-command.
2934
var CmdWeb = cli.Command{
3035
Name: "web",
@@ -45,7 +50,7 @@ and it takes care of all the other things for you`,
4550
},
4651
cli.StringFlag{
4752
Name: "pid, P",
48-
Value: setting.PIDFile,
53+
Value: PIDFile,
4954
Usage: "Custom pid file path",
5055
},
5156
cli.BoolFlag{
@@ -81,6 +86,22 @@ func runHTTPRedirector() {
8186
}
8287
}
8388

89+
func createPIDFile(pidPath string) {
90+
currentPid := os.Getpid()
91+
if err := os.MkdirAll(filepath.Dir(pidPath), os.ModePerm); err != nil {
92+
log.Fatal("Failed to create PID folder: %v", err)
93+
}
94+
95+
file, err := os.Create(pidPath)
96+
if err != nil {
97+
log.Fatal("Failed to create PID file: %v", err)
98+
}
99+
defer file.Close()
100+
if _, err := file.WriteString(strconv.FormatInt(int64(currentPid), 10)); err != nil {
101+
log.Fatal("Failed to write PID information: %v", err)
102+
}
103+
}
104+
84105
func runWeb(ctx *cli.Context) error {
85106
if ctx.Bool("verbose") {
86107
_ = log.DelLogger("console")
@@ -107,8 +128,7 @@ func runWeb(ctx *cli.Context) error {
107128

108129
// Set pid file setting
109130
if ctx.IsSet("pid") {
110-
setting.PIDFile = ctx.String("pid")
111-
setting.WritePIDFile = true
131+
createPIDFile(ctx.String("pid"))
112132
}
113133

114134
// Perform pre-initialization

docs/content/doc/installation/from-source.en-us.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ using the `LDFLAGS` environment variable for `make`. The appropriate settings ar
159159
- For _`CustomConf`_ you should use `-X \"code.gitea.io/gitea/modules/setting.CustomConf=conf.ini\"`
160160
- For _`AppWorkPath`_ you should use `-X \"code.gitea.io/gitea/modules/setting.AppWorkPath=working-path\"`
161161
- For _`StaticRootPath`_ you should use `-X \"code.gitea.io/gitea/modules/setting.StaticRootPath=static-root-path\"`
162-
- To change the default PID file location use `-X \"code.gitea.io/gitea/modules/setting.PIDFile=/run/gitea.pid\"`
162+
- To change the default PID file location use `-X \"code.gitea.io/gitea/cmd.PIDFile=/run/gitea.pid\"`
163163

164164
Add as many of the strings with their preceding `-X` to the `LDFLAGS` variable and run `make build`
165165
with the appropriate `TAGS` as above.

modules/setting/database.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ var (
5454

5555
// LoadDBSetting loads the database settings
5656
func LoadDBSetting() {
57-
sec := CfgProvider.Section("database")
57+
loadDBSetting(CfgProvider)
58+
}
59+
60+
func loadDBSetting(rootCfg ConfigProvider) {
61+
sec := rootCfg.Section("database")
5862
Database.Type = DatabaseType(sec.Key("DB_TYPE").String())
5963
defaultCharset := "utf8"
6064

modules/setting/setting.go

+13-36
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"path"
1313
"path/filepath"
1414
"runtime"
15-
"strconv"
1615
"strings"
1716
"time"
1817

@@ -42,15 +41,13 @@ var (
4241
AppWorkPath string
4342

4443
// Global setting objects
45-
CfgProvider ConfigProvider
46-
CustomPath string // Custom directory path
47-
CustomConf string
48-
PIDFile = "/run/gitea.pid"
49-
WritePIDFile bool
50-
RunMode string
51-
RunUser string
52-
IsProd bool
53-
IsWindows bool
44+
CfgProvider ConfigProvider
45+
CustomPath string // Custom directory path
46+
CustomConf string
47+
RunMode string
48+
RunUser string
49+
IsProd bool
50+
IsWindows bool
5451
)
5552

5653
func getAppPath() (string, error) {
@@ -141,22 +138,6 @@ func IsRunUserMatchCurrentUser(runUser string) (string, bool) {
141138
return currentUser, runUser == currentUser
142139
}
143140

144-
func createPIDFile(pidPath string) {
145-
currentPid := os.Getpid()
146-
if err := os.MkdirAll(filepath.Dir(pidPath), os.ModePerm); err != nil {
147-
log.Fatal("Failed to create PID folder: %v", err)
148-
}
149-
150-
file, err := os.Create(pidPath)
151-
if err != nil {
152-
log.Fatal("Failed to create PID file: %v", err)
153-
}
154-
defer file.Close()
155-
if _, err := file.WriteString(strconv.FormatInt(int64(currentPid), 10)); err != nil {
156-
log.Fatal("Failed to write PID information: %v", err)
157-
}
158-
}
159-
160141
// SetCustomPathAndConf will set CustomPath and CustomConf with reference to the
161142
// GITEA_CUSTOM environment variable and with provided overrides before stepping
162143
// back to the default
@@ -218,17 +199,17 @@ func PrepareAppDataPath() error {
218199

219200
// InitProviderFromExistingFile initializes config provider from an existing config file (app.ini)
220201
func InitProviderFromExistingFile() {
221-
CfgProvider = newFileProviderFromConf(CustomConf, WritePIDFile, false, PIDFile, "")
202+
CfgProvider = newFileProviderFromConf(CustomConf, false, "")
222203
}
223204

224205
// InitProviderAllowEmpty initializes config provider from file, it's also fine that if the config file (app.ini) doesn't exist
225206
func InitProviderAllowEmpty() {
226-
CfgProvider = newFileProviderFromConf(CustomConf, WritePIDFile, true, PIDFile, "")
207+
CfgProvider = newFileProviderFromConf(CustomConf, true, "")
227208
}
228209

229210
// InitProviderAndLoadCommonSettingsForTest initializes config provider and load common setttings for tests
230211
func InitProviderAndLoadCommonSettingsForTest(extraConfigs ...string) {
231-
CfgProvider = newFileProviderFromConf(CustomConf, WritePIDFile, true, PIDFile, strings.Join(extraConfigs, "\n"))
212+
CfgProvider = newFileProviderFromConf(CustomConf, true, strings.Join(extraConfigs, "\n"))
232213
loadCommonSettingsFrom(CfgProvider)
233214
if err := PrepareAppDataPath(); err != nil {
234215
log.Fatal("Can not prepare APP_DATA_PATH: %v", err)
@@ -241,13 +222,9 @@ func InitProviderAndLoadCommonSettingsForTest(extraConfigs ...string) {
241222

242223
// newFileProviderFromConf initializes configuration context.
243224
// NOTE: do not print any log except error.
244-
func newFileProviderFromConf(customConf string, writePIDFile, allowEmpty bool, pidFile, extraConfig string) *ini.File {
225+
func newFileProviderFromConf(customConf string, allowEmpty bool, extraConfig string) *ini.File {
245226
cfg := ini.Empty()
246227

247-
if writePIDFile && len(pidFile) > 0 {
248-
createPIDFile(pidFile)
249-
}
250-
251228
isFile, err := util.IsFile(customConf)
252229
if err != nil {
253230
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)) {
380357

381358
// LoadSettings initializes the settings for normal start up
382359
func LoadSettings() {
383-
LoadDBSetting()
360+
loadDBSetting(CfgProvider)
384361
loadServiceFrom(CfgProvider)
385362
loadOAuth2ClientFrom(CfgProvider)
386363
InitLogs(false)
@@ -401,7 +378,7 @@ func LoadSettings() {
401378

402379
// LoadSettingsForInstall initializes the settings for install
403380
func LoadSettingsForInstall() {
404-
LoadDBSetting()
381+
loadDBSetting(CfgProvider)
405382
loadServiceFrom(CfgProvider)
406383
loadMailerFrom(CfgProvider)
407384
}

0 commit comments

Comments
 (0)