@@ -12,7 +12,6 @@ import (
1212 "os/signal"
1313 "runtime/pprof"
1414 "strconv"
15- "sync"
1615 "syscall"
1716 "time"
1817
@@ -22,51 +21,6 @@ import (
2221 "code.gitea.io/gitea/modules/setting"
2322)
2423
25- // Manager manages the graceful shutdown process
26- type Manager struct {
27- isChild bool
28- forked bool
29- lock * sync.RWMutex
30- state state
31- shutdownCtx context.Context
32- hammerCtx context.Context
33- terminateCtx context.Context
34- managerCtx context.Context
35- shutdownCtxCancel context.CancelFunc
36- hammerCtxCancel context.CancelFunc
37- terminateCtxCancel context.CancelFunc
38- managerCtxCancel context.CancelFunc
39- runningServerWaitGroup sync.WaitGroup
40- createServerWaitGroup sync.WaitGroup
41- terminateWaitGroup sync.WaitGroup
42-
43- toRunAtShutdown []func ()
44- toRunAtTerminate []func ()
45- }
46-
47- func newGracefulManager (ctx context.Context ) * Manager {
48- manager := & Manager {
49- isChild : len (os .Getenv (listenFDsEnv )) > 0 && os .Getppid () > 1 ,
50- lock : & sync.RWMutex {},
51- }
52- manager .createServerWaitGroup .Add (numberOfServersToCreate )
53- manager .start (ctx )
54- return manager
55- }
56-
57- type systemdNotifyMsg string
58-
59- const (
60- readyMsg systemdNotifyMsg = "READY=1"
61- stoppingMsg systemdNotifyMsg = "STOPPING=1"
62- reloadingMsg systemdNotifyMsg = "RELOADING=1"
63- watchdogMsg systemdNotifyMsg = "WATCHDOG=1"
64- )
65-
66- func statusMsg (msg string ) systemdNotifyMsg {
67- return systemdNotifyMsg ("STATUS=" + msg )
68- }
69-
7024func pidMsg () systemdNotifyMsg {
7125 return systemdNotifyMsg ("MAINPID=" + strconv .Itoa (os .Getpid ()))
7226}
@@ -89,27 +43,13 @@ func (g *Manager) notify(msg systemdNotifyMsg) {
8943 }
9044}
9145
92- func (g * Manager ) start (ctx context.Context ) {
93- // Make contexts
94- g .terminateCtx , g .terminateCtxCancel = context .WithCancel (ctx )
95- g .shutdownCtx , g .shutdownCtxCancel = context .WithCancel (ctx )
96- g .hammerCtx , g .hammerCtxCancel = context .WithCancel (ctx )
97- g .managerCtx , g .managerCtxCancel = context .WithCancel (ctx )
98-
99- // Next add pprof labels to these contexts
100- g .terminateCtx = pprof .WithLabels (g .terminateCtx , pprof .Labels ("graceful-lifecycle" , "with-terminate" ))
101- g .shutdownCtx = pprof .WithLabels (g .shutdownCtx , pprof .Labels ("graceful-lifecycle" , "with-shutdown" ))
102- g .hammerCtx = pprof .WithLabels (g .hammerCtx , pprof .Labels ("graceful-lifecycle" , "with-hammer" ))
103- g .managerCtx = pprof .WithLabels (g .managerCtx , pprof .Labels ("graceful-lifecycle" , "with-manager" ))
104-
46+ func (g * Manager ) start () {
10547 // Now label this and all goroutines created by this goroutine with the graceful-lifecycle manager
10648 pprof .SetGoroutineLabels (g .managerCtx )
107- defer pprof .SetGoroutineLabels (ctx )
49+ defer pprof .SetGoroutineLabels (g .ctx )
50+
51+ g .isChild = len (os .Getenv (listenFDsEnv )) > 0 && os .Getppid () > 1
10852
109- // Set the running state & handle signals
110- if ! g .setStateTransition (stateInit , stateRunning ) {
111- panic ("invalid graceful manager state: transition from init to running failed" )
112- }
11353 g .notify (statusMsg ("Starting Gitea" ))
11454 g .notify (pidMsg ())
11555 go g .handleSignals (g .managerCtx )
@@ -118,11 +58,9 @@ func (g *Manager) start(ctx context.Context) {
11858 startupDone := make (chan struct {})
11959 go func () {
12060 defer close (startupDone )
121- // Wait till we're done getting all of the listeners and then close
122- // the unused ones
61+ // Wait till we're done getting all the listeners and then close the unused ones
12362 g .createServerWaitGroup .Wait ()
124- // Ignore the error here there's not much we can do with it
125- // They're logged in the CloseProvidedListeners function
63+ // Ignore the error here there's not much we can do with it, they're logged in the CloseProvidedListeners function
12664 _ = CloseProvidedListeners ()
12765 g .notify (readyMsg )
12866 }()
@@ -133,7 +71,7 @@ func (g *Manager) start(ctx context.Context) {
13371 return
13472 case <- g .IsShutdown ():
13573 func () {
136- // When waitgroup counter goes negative it will panic - we don't care about this so we can just ignore it.
74+ // When WaitGroup counter goes negative it will panic - we don't care about this so we can just ignore it.
13775 defer func () {
13876 _ = recover ()
13977 }()
@@ -255,29 +193,3 @@ func (g *Manager) DoGracefulRestart() {
255193 g .doShutdown ()
256194 }
257195}
258-
259- // DoImmediateHammer causes an immediate hammer
260- func (g * Manager ) DoImmediateHammer () {
261- g .notify (statusMsg ("Sending immediate hammer" ))
262- g .doHammerTime (0 * time .Second )
263- }
264-
265- // DoGracefulShutdown causes a graceful shutdown
266- func (g * Manager ) DoGracefulShutdown () {
267- g .lock .Lock ()
268- if ! g .forked {
269- g .lock .Unlock ()
270- g .notify (stoppingMsg )
271- } else {
272- g .lock .Unlock ()
273- g .notify (statusMsg ("Shutting down after fork" ))
274- }
275- g .doShutdown ()
276- }
277-
278- // RegisterServer registers the running of a listening server, in the case of unix this means that the parent process can now die.
279- // Any call to RegisterServer must be matched by a call to ServerDone
280- func (g * Manager ) RegisterServer () {
281- KillParent ()
282- g .runningServerWaitGroup .Add (1 )
283- }
0 commit comments