Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user-defined signal handling to freeze and warm up the Minecraft server #271

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/progmgr/progmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ var (
msh *program = &program{
startTime: time.Now(),
sigExit: make(chan os.Signal, 1),
sigUser: make(chan os.Signal, 1),
mgrActive: false,
}
)

type program struct {
startTime time.Time // msh program start time
sigExit chan os.Signal // channel through which OS termination signals are notified
sigUser chan os.Signal // channel through which User-defined signals are notified
mgrActive bool // indicates if msh manager is running
}

Expand All @@ -46,6 +48,8 @@ func MshMgr() {
// set msh.sigExit to relay termination signals
signal.Notify(msh.sigExit, syscall.SIGINT, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGQUIT)

SetupUserSignalHandling(msh)

msh.mgrActive = true

for {
Expand Down
40 changes: 40 additions & 0 deletions lib/progmgr/usersignal-posix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//go:build linux || darwin
// +build linux darwin

package progmgr

import (
"msh/lib/errco"
"msh/lib/servctrl"
"os/signal"
"syscall"
)

// On POSIX, this function setups a goroutine that handles incoming SIGUSR1 and
// SIGUSR2 signals and freeze or warms the server respectively
func SetupUserSignalHandling(program *program) {
// set program.sigUser to relay user-defined signals
signal.Notify(program.sigUser, syscall.SIGUSR1, syscall.SIGUSR2)
go handleUserSignal()
}

// handleUserSignal is responsable for handling SIGUSR1 and SIGUSR2, to both freeze
// and warm the minecraft server respectively
// [goroutine]
func handleUserSignal() {
for {
sig := <-msh.sigUser
errco.NewLogln(errco.TYPE_INF, errco.LVL_1, errco.ERROR_NIL, "received signal: %s", sig.String())

const (
SIGNAL_FREEZE = syscall.SIGUSR1
SIGNAL_WARM = syscall.SIGUSR2
)

if sig == SIGNAL_WARM {
servctrl.WarmMS()
} else if sig == SIGNAL_FREEZE {
servctrl.FreezeMS(false)
}
}
}
10 changes: 10 additions & 0 deletions lib/progmgr/usersignal-windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build windows
// +build windows

package progmgr

// On Windows, we simply do nothing and never handle user-defined signals, since
// SIGUSR does not exists.
func SetupUserSignalHandling(program *program) {
return
}