Skip to content

Commit

Permalink
Fix regression from go-gitea#14623 - use debug SVC handler only on in…
Browse files Browse the repository at this point in the history
…teractive sessions (go-gitea#15210) (go-gitea#15211)

Backport go-gitea#15210

Unfortunately go-gitea#14623 changed from the deprecated IsInteractiveSession to
IsWindowsService without recognising that they are the complement of
each other.

This means that Windows SVC control is not working correctly. This PR
adds some Tracing statements but also fixes the bug.

Fix go-gitea#15159

Signed-off-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
zeripath committed Mar 31, 2021
1 parent 92b2883 commit 2bc7595
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions modules/graceful/manager_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,19 @@ func (g *Manager) start() {
// Set the running state
g.setState(stateRunning)
if skip, _ := strconv.ParseBool(os.Getenv("SKIP_MINWINSVC")); skip {
log.Trace("Skipping SVC check as SKIP_MINWINSVC is set")
return
}

// Make SVC process
run := svc.Run
isInteractive, err := svc.IsWindowsService()
isWindowsService, err := svc.IsWindowsService()
if err != nil {
log.Error("Unable to ascertain if running as an Interactive Session: %v", err)
log.Error("Unable to ascertain if running as an Windows Service: %v", err)
return
}
if isInteractive {
if !isWindowsService {
log.Trace("Not running a service ... using the debug SVC manager")
run = debug.Run
}
go func() {
Expand All @@ -94,38 +96,49 @@ func (g *Manager) Execute(args []string, changes <-chan svc.ChangeRequest, statu
status <- svc.Status{State: svc.StartPending, WaitHint: uint32(setting.StartupTimeout / time.Millisecond)}
}

log.Trace("Awaiting server start-up")
// Now need to wait for everything to start...
if !g.awaitServer(setting.StartupTimeout) {
log.Trace("... start-up failed ... Stopped")
return false, 1
}

log.Trace("Sending Running state to SVC")

// We need to implement some way of svc.AcceptParamChange/svc.ParamChange
status <- svc.Status{
State: svc.Running,
Accepts: svc.AcceptStop | svc.AcceptShutdown | acceptHammerCode,
}

log.Trace("Started")

waitTime := 30 * time.Second

loop:
for {
select {
case <-g.ctx.Done():
log.Trace("Shutting down")
g.DoGracefulShutdown()
waitTime += setting.GracefulHammerTime
break loop
case <-g.shutdownRequested:
log.Trace("Shutting down")
waitTime += setting.GracefulHammerTime
break loop
case change := <-changes:
switch change.Cmd {
case svc.Interrogate:
log.Trace("SVC sent interrogate")
status <- change.CurrentStatus
case svc.Stop, svc.Shutdown:
log.Trace("SVC requested shutdown - shutting down")
g.DoGracefulShutdown()
waitTime += setting.GracefulHammerTime
break loop
case hammerCode:
log.Trace("SVC requested hammer - shutting down and hammering immediately")
g.DoGracefulShutdown()
g.DoImmediateHammer()
break loop
Expand All @@ -134,6 +147,8 @@ loop:
}
}
}

log.Trace("Sending StopPending state to SVC")
status <- svc.Status{
State: svc.StopPending,
WaitHint: uint32(waitTime / time.Millisecond),
Expand All @@ -145,8 +160,10 @@ hammerLoop:
case change := <-changes:
switch change.Cmd {
case svc.Interrogate:
log.Trace("SVC sent interrogate")
status <- change.CurrentStatus
case svc.Stop, svc.Shutdown, hammerCmd:
log.Trace("SVC requested hammer - hammering immediately")
g.DoImmediateHammer()
break hammerLoop
default:
Expand All @@ -156,6 +173,8 @@ hammerLoop:
break hammerLoop
}
}

log.Trace("Stopped")
return false, 0
}

Expand Down

0 comments on commit 2bc7595

Please sign in to comment.