2019-11-22 00:02:02 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-11-22 00:02:02 +05:30
|
|
|
|
2021-08-24 22:17:09 +05:30
|
|
|
//go:build !windows
|
|
|
|
|
2019-11-22 00:02:02 +05:30
|
|
|
package graceful
|
|
|
|
|
|
|
|
import (
|
2019-11-30 20:10:22 +05:30
|
|
|
"context"
|
2019-11-22 00:02:02 +05:30
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2022-03-25 18:17:12 +05:30
|
|
|
"runtime/pprof"
|
2023-05-16 03:50:30 +05:30
|
|
|
"strconv"
|
2019-11-22 00:02:02 +05:30
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
Rewrite logger system (#24726)
## ⚠️ Breaking
The `log.<mode>.<logger>` style config has been dropped. If you used it,
please check the new config manual & app.example.ini to make your
instance output logs as expected.
Although many legacy options still work, it's encouraged to upgrade to
the new options.
The SMTP logger is deleted because SMTP is not suitable to collect logs.
If you have manually configured Gitea log options, please confirm the
logger system works as expected after upgrading.
## Description
Close #12082 and maybe more log-related issues, resolve some related
FIXMEs in old code (which seems unfixable before)
Just like rewriting queue #24505 : make code maintainable, clear legacy
bugs, and add the ability to support more writers (eg: JSON, structured
log)
There is a new document (with examples): `logging-config.en-us.md`
This PR is safer than the queue rewriting, because it's just for
logging, it won't break other logic.
## The old problems
The logging system is quite old and difficult to maintain:
* Unclear concepts: Logger, NamedLogger, MultiChannelledLogger,
SubLogger, EventLogger, WriterLogger etc
* Some code is diffuclt to konw whether it is right:
`log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs
`log.DelLogger("console")`
* The old system heavily depends on ini config system, it's difficult to
create new logger for different purpose, and it's very fragile.
* The "color" trick is difficult to use and read, many colors are
unnecessary, and in the future structured log could help
* It's difficult to add other log formats, eg: JSON format
* The log outputer doesn't have full control of its goroutine, it's
difficult to make outputer have advanced behaviors
* The logs could be lost in some cases: eg: no Fatal error when using
CLI.
* Config options are passed by JSON, which is quite fragile.
* INI package makes the KEY in `[log]` section visible in `[log.sub1]`
and `[log.sub1.subA]`, this behavior is quite fragile and would cause
more unclear problems, and there is no strong requirement to support
`log.<mode>.<logger>` syntax.
## The new design
See `logger.go` for documents.
## Screenshot
<details>
![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff)
![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9)
![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee)
</details>
## TODO
* [x] add some new tests
* [x] fix some tests
* [x] test some sub-commands (manually ....)
---------
Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Giteabot <teabot@gitea.io>
2023-05-22 04:05:11 +05:30
|
|
|
"code.gitea.io/gitea/modules/graceful/releasereopen"
|
2019-11-22 00:02:02 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2022-03-31 22:31:43 +05:30
|
|
|
"code.gitea.io/gitea/modules/process"
|
2019-11-22 00:02:02 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
2023-05-16 03:50:30 +05:30
|
|
|
func pidMsg() systemdNotifyMsg {
|
|
|
|
return systemdNotifyMsg("MAINPID=" + strconv.Itoa(os.Getpid()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify systemd of status via the notify protocol
|
|
|
|
func (g *Manager) notify(msg systemdNotifyMsg) {
|
|
|
|
conn, err := getNotifySocket()
|
|
|
|
if err != nil {
|
|
|
|
// the err is logged in getNotifySocket
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if conn == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
if _, err = conn.Write([]byte(msg)); err != nil {
|
|
|
|
log.Warn("Failed to notify NOTIFY_SOCKET: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-24 19:51:46 +05:30
|
|
|
func (g *Manager) start() {
|
2022-03-25 18:17:12 +05:30
|
|
|
// Now label this and all goroutines created by this goroutine with the graceful-lifecycle manager
|
|
|
|
pprof.SetGoroutineLabels(g.managerCtx)
|
2023-11-24 19:51:46 +05:30
|
|
|
defer pprof.SetGoroutineLabels(g.ctx)
|
|
|
|
|
|
|
|
g.isChild = len(os.Getenv(listenFDsEnv)) > 0 && os.Getppid() > 1
|
2019-12-15 15:21:28 +05:30
|
|
|
|
2023-05-16 03:50:30 +05:30
|
|
|
g.notify(statusMsg("Starting Gitea"))
|
|
|
|
g.notify(pidMsg())
|
2022-03-31 22:31:43 +05:30
|
|
|
go g.handleSignals(g.managerCtx)
|
2019-12-15 15:21:28 +05:30
|
|
|
|
|
|
|
// Handle clean up of unused provided listeners and delayed start-up
|
|
|
|
startupDone := make(chan struct{})
|
2019-11-22 00:02:02 +05:30
|
|
|
go func() {
|
2024-03-15 16:29:11 +05:30
|
|
|
defer func() {
|
|
|
|
close(startupDone)
|
|
|
|
// Close the unused listeners and ignore the error here there's not much we can do with it, they're logged in the CloseProvidedListeners function
|
|
|
|
_ = CloseProvidedListeners()
|
2024-03-06 17:20:39 +05:30
|
|
|
}()
|
2024-03-15 16:29:11 +05:30
|
|
|
// Wait for all servers to be created
|
|
|
|
g.createServerCond.L.Lock()
|
|
|
|
for {
|
|
|
|
if g.createdServer >= numberOfServersToCreate {
|
|
|
|
g.createServerCond.L.Unlock()
|
|
|
|
g.notify(readyMsg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-g.IsShutdown():
|
|
|
|
g.createServerCond.L.Unlock()
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
g.createServerCond.Wait()
|
|
|
|
}
|
2019-11-22 00:02:02 +05:30
|
|
|
}()
|
|
|
|
if setting.StartupTimeout > 0 {
|
|
|
|
go func() {
|
|
|
|
select {
|
2019-12-15 15:21:28 +05:30
|
|
|
case <-startupDone:
|
2019-11-22 00:02:02 +05:30
|
|
|
return
|
|
|
|
case <-g.IsShutdown():
|
2024-03-15 16:29:11 +05:30
|
|
|
g.createServerCond.Signal()
|
2019-11-22 00:02:02 +05:30
|
|
|
return
|
|
|
|
case <-time.After(setting.StartupTimeout):
|
|
|
|
log.Error("Startup took too long! Shutting down")
|
2023-05-16 03:50:30 +05:30
|
|
|
g.notify(statusMsg("Startup took too long! Shutting down"))
|
|
|
|
g.notify(stoppingMsg)
|
2019-11-22 00:02:02 +05:30
|
|
|
g.doShutdown()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 15:21:28 +05:30
|
|
|
func (g *Manager) handleSignals(ctx context.Context) {
|
2022-03-31 22:31:43 +05:30
|
|
|
ctx, _, finished := process.GetManager().AddTypedContext(ctx, "Graceful: HandleSignals", process.SystemProcessType, true)
|
|
|
|
defer finished()
|
|
|
|
|
2019-11-22 00:02:02 +05:30
|
|
|
signalChannel := make(chan os.Signal, 1)
|
|
|
|
|
|
|
|
signal.Notify(
|
|
|
|
signalChannel,
|
|
|
|
syscall.SIGHUP,
|
|
|
|
syscall.SIGUSR1,
|
|
|
|
syscall.SIGUSR2,
|
|
|
|
syscall.SIGINT,
|
|
|
|
syscall.SIGTERM,
|
|
|
|
syscall.SIGTSTP,
|
|
|
|
)
|
|
|
|
|
2023-05-16 03:50:30 +05:30
|
|
|
watchdogTimeout := getWatchdogTimeout()
|
|
|
|
t := &time.Ticker{}
|
|
|
|
if watchdogTimeout != 0 {
|
|
|
|
g.notify(watchdogMsg)
|
|
|
|
t = time.NewTicker(watchdogTimeout / 2)
|
|
|
|
}
|
|
|
|
|
2019-11-22 00:02:02 +05:30
|
|
|
pid := syscall.Getpid()
|
|
|
|
for {
|
2019-11-30 20:10:22 +05:30
|
|
|
select {
|
|
|
|
case sig := <-signalChannel:
|
|
|
|
switch sig {
|
|
|
|
case syscall.SIGHUP:
|
2020-02-11 10:59:45 +05:30
|
|
|
log.Info("PID: %d. Received SIGHUP. Attempting GracefulRestart...", pid)
|
|
|
|
g.DoGracefulRestart()
|
2019-11-30 20:10:22 +05:30
|
|
|
case syscall.SIGUSR1:
|
2020-07-06 05:37:07 +05:30
|
|
|
log.Warn("PID %d. Received SIGUSR1. Releasing and reopening logs", pid)
|
2023-05-16 03:50:30 +05:30
|
|
|
g.notify(statusMsg("Releasing and reopening logs"))
|
Rewrite logger system (#24726)
## ⚠️ Breaking
The `log.<mode>.<logger>` style config has been dropped. If you used it,
please check the new config manual & app.example.ini to make your
instance output logs as expected.
Although many legacy options still work, it's encouraged to upgrade to
the new options.
The SMTP logger is deleted because SMTP is not suitable to collect logs.
If you have manually configured Gitea log options, please confirm the
logger system works as expected after upgrading.
## Description
Close #12082 and maybe more log-related issues, resolve some related
FIXMEs in old code (which seems unfixable before)
Just like rewriting queue #24505 : make code maintainable, clear legacy
bugs, and add the ability to support more writers (eg: JSON, structured
log)
There is a new document (with examples): `logging-config.en-us.md`
This PR is safer than the queue rewriting, because it's just for
logging, it won't break other logic.
## The old problems
The logging system is quite old and difficult to maintain:
* Unclear concepts: Logger, NamedLogger, MultiChannelledLogger,
SubLogger, EventLogger, WriterLogger etc
* Some code is diffuclt to konw whether it is right:
`log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs
`log.DelLogger("console")`
* The old system heavily depends on ini config system, it's difficult to
create new logger for different purpose, and it's very fragile.
* The "color" trick is difficult to use and read, many colors are
unnecessary, and in the future structured log could help
* It's difficult to add other log formats, eg: JSON format
* The log outputer doesn't have full control of its goroutine, it's
difficult to make outputer have advanced behaviors
* The logs could be lost in some cases: eg: no Fatal error when using
CLI.
* Config options are passed by JSON, which is quite fragile.
* INI package makes the KEY in `[log]` section visible in `[log.sub1]`
and `[log.sub1.subA]`, this behavior is quite fragile and would cause
more unclear problems, and there is no strong requirement to support
`log.<mode>.<logger>` syntax.
## The new design
See `logger.go` for documents.
## Screenshot
<details>
![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff)
![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9)
![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee)
</details>
## TODO
* [x] add some new tests
* [x] fix some tests
* [x] test some sub-commands (manually ....)
---------
Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Giteabot <teabot@gitea.io>
2023-05-22 04:05:11 +05:30
|
|
|
if err := releasereopen.GetManager().ReleaseReopen(); err != nil {
|
2020-07-06 05:37:07 +05:30
|
|
|
log.Error("Error whilst releasing and reopening logs: %v", err)
|
|
|
|
}
|
2019-11-30 20:10:22 +05:30
|
|
|
case syscall.SIGUSR2:
|
|
|
|
log.Warn("PID %d. Received SIGUSR2. Hammering...", pid)
|
2020-01-29 06:31:06 +05:30
|
|
|
g.DoImmediateHammer()
|
2019-11-30 20:10:22 +05:30
|
|
|
case syscall.SIGINT:
|
|
|
|
log.Warn("PID %d. Received SIGINT. Shutting down...", pid)
|
2020-01-29 06:31:06 +05:30
|
|
|
g.DoGracefulShutdown()
|
2019-11-30 20:10:22 +05:30
|
|
|
case syscall.SIGTERM:
|
|
|
|
log.Warn("PID %d. Received SIGTERM. Shutting down...", pid)
|
2020-01-29 06:31:06 +05:30
|
|
|
g.DoGracefulShutdown()
|
2019-11-30 20:10:22 +05:30
|
|
|
case syscall.SIGTSTP:
|
|
|
|
log.Info("PID %d. Received SIGTSTP.", pid)
|
|
|
|
default:
|
|
|
|
log.Info("PID %d. Received %v.", pid, sig)
|
2019-11-22 00:02:02 +05:30
|
|
|
}
|
2023-05-16 03:50:30 +05:30
|
|
|
case <-t.C:
|
|
|
|
g.notify(watchdogMsg)
|
2019-11-30 20:10:22 +05:30
|
|
|
case <-ctx.Done():
|
|
|
|
log.Warn("PID: %d. Background context for manager closed - %v - Shutting down...", pid, ctx.Err())
|
2020-01-29 06:31:06 +05:30
|
|
|
g.DoGracefulShutdown()
|
2022-06-12 19:20:18 +05:30
|
|
|
return
|
2019-11-22 00:02:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 15:21:28 +05:30
|
|
|
func (g *Manager) doFork() error {
|
2019-11-22 00:02:02 +05:30
|
|
|
g.lock.Lock()
|
|
|
|
if g.forked {
|
|
|
|
g.lock.Unlock()
|
|
|
|
return errors.New("another process already forked. Ignoring this one")
|
|
|
|
}
|
|
|
|
g.forked = true
|
|
|
|
g.lock.Unlock()
|
2023-05-16 03:50:30 +05:30
|
|
|
|
|
|
|
g.notify(reloadingMsg)
|
|
|
|
|
2019-11-22 00:02:02 +05:30
|
|
|
// We need to move the file logs to append pids
|
|
|
|
setting.RestartLogsWithPIDSuffix()
|
|
|
|
|
|
|
|
_, err := RestartProcess()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-29 06:31:06 +05:30
|
|
|
// DoGracefulRestart causes a graceful restart
|
|
|
|
func (g *Manager) DoGracefulRestart() {
|
|
|
|
if setting.GracefulRestartable {
|
|
|
|
log.Info("PID: %d. Forking...", os.Getpid())
|
|
|
|
err := g.doFork()
|
2022-02-19 22:06:25 +05:30
|
|
|
if err != nil {
|
|
|
|
if err.Error() == "another process already forked. Ignoring this one" {
|
|
|
|
g.DoImmediateHammer()
|
|
|
|
} else {
|
|
|
|
log.Error("Error whilst forking from PID: %d : %v", os.Getpid(), err)
|
|
|
|
}
|
2020-01-29 06:31:06 +05:30
|
|
|
}
|
2023-05-22 07:08:38 +05:30
|
|
|
// doFork calls RestartProcess which starts a new Gitea process, so this parent process needs to exit
|
|
|
|
// Otherwise some resources (eg: leveldb lock) will be held by this parent process and the new process will fail to start
|
|
|
|
log.Info("PID: %d. Shutting down after forking ...", os.Getpid())
|
|
|
|
g.doShutdown()
|
2020-01-29 06:31:06 +05:30
|
|
|
} else {
|
|
|
|
log.Info("PID: %d. Not set restartable. Shutting down...", os.Getpid())
|
2023-05-16 03:50:30 +05:30
|
|
|
g.notify(stoppingMsg)
|
2020-01-29 06:31:06 +05:30
|
|
|
g.doShutdown()
|
|
|
|
}
|
|
|
|
}
|