init: 1.0.0
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
//go:build windows
|
||||
|
||||
package proc
|
||||
|
||||
import "time"
|
||||
|
||||
// ShutdownConf is empty on windows.
|
||||
type ShutdownConf struct{}
|
||||
|
||||
// AddShutdownListener returns fn itself on windows, lets callers call fn on their own.
|
||||
func AddShutdownListener(fn func()) func() {
|
||||
return fn
|
||||
}
|
||||
|
||||
// AddWrapUpListener returns fn itself on windows, lets callers call fn on their own.
|
||||
func AddWrapUpListener(fn func()) func() {
|
||||
return fn
|
||||
}
|
||||
|
||||
// SetTimeToForceQuit does nothing on windows.
|
||||
func SetTimeToForceQuit(duration time.Duration) {
|
||||
}
|
||||
|
||||
// Setup does nothing on windows.
|
||||
func Setup(conf ShutdownConf) {
|
||||
}
|
||||
|
||||
// Shutdown does nothing on windows.
|
||||
func Shutdown() {
|
||||
}
|
||||
|
||||
// WrapUp does nothing on windows.
|
||||
func WrapUp() {
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
//go:build linux || darwin
|
||||
|
||||
package proc
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/perfect-panel/ppanel-server/pkg/threading"
|
||||
)
|
||||
|
||||
const (
|
||||
//nolint:unused
|
||||
wrapUpTime = time.Second
|
||||
// why we use 5500 milliseconds is because most of our queue are blocking mode with 5 seconds
|
||||
waitTime = 5500 * time.Millisecond
|
||||
)
|
||||
|
||||
var (
|
||||
wrapUpListeners = new(listenerManager)
|
||||
shutdownListeners = new(listenerManager)
|
||||
delayTimeBeforeForceQuit = waitTime
|
||||
)
|
||||
|
||||
// AddShutdownListener adds fn as a shutdown listener.
|
||||
// The returned func can be used to wait for fn getting called.
|
||||
func AddShutdownListener(fn func()) (waitForCalled func()) {
|
||||
return shutdownListeners.addListener(fn)
|
||||
}
|
||||
|
||||
// AddWrapUpListener adds fn as a wrap up listener.
|
||||
// The returned func can be used to wait for fn getting called.
|
||||
func AddWrapUpListener(fn func()) (waitForCalled func()) {
|
||||
return wrapUpListeners.addListener(fn)
|
||||
}
|
||||
|
||||
// SetTimeToForceQuit sets the waiting time before force quitting.
|
||||
func SetTimeToForceQuit(duration time.Duration) {
|
||||
delayTimeBeforeForceQuit = duration
|
||||
}
|
||||
|
||||
// Shutdown calls the registered shutdown listeners, only for test purpose.
|
||||
func Shutdown() {
|
||||
shutdownListeners.notifyListeners()
|
||||
}
|
||||
|
||||
// WrapUp wraps up the process, only for test purpose.
|
||||
func WrapUp() {
|
||||
wrapUpListeners.notifyListeners()
|
||||
}
|
||||
|
||||
//nolint:unused
|
||||
func gracefulStop(signals chan os.Signal, sig syscall.Signal) {
|
||||
signal.Stop(signals)
|
||||
|
||||
go wrapUpListeners.notifyListeners()
|
||||
|
||||
time.Sleep(wrapUpTime)
|
||||
go shutdownListeners.notifyListeners()
|
||||
|
||||
time.Sleep(delayTimeBeforeForceQuit - wrapUpTime)
|
||||
_ = syscall.Kill(syscall.Getpid(), sig)
|
||||
}
|
||||
|
||||
type listenerManager struct {
|
||||
lock sync.Mutex
|
||||
waitGroup sync.WaitGroup
|
||||
listeners []func()
|
||||
}
|
||||
|
||||
func (lm *listenerManager) addListener(fn func()) (waitForCalled func()) {
|
||||
lm.waitGroup.Add(1)
|
||||
|
||||
lm.lock.Lock()
|
||||
lm.listeners = append(lm.listeners, func() {
|
||||
defer lm.waitGroup.Done()
|
||||
fn()
|
||||
})
|
||||
lm.lock.Unlock()
|
||||
|
||||
return func() {
|
||||
lm.waitGroup.Wait()
|
||||
}
|
||||
}
|
||||
|
||||
func (lm *listenerManager) notifyListeners() {
|
||||
lm.lock.Lock()
|
||||
defer lm.lock.Unlock()
|
||||
|
||||
group := threading.NewRoutineGroup()
|
||||
for _, listener := range lm.listeners {
|
||||
group.RunSafe(listener)
|
||||
}
|
||||
group.Wait()
|
||||
|
||||
lm.listeners = nil
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
//go:build linux || darwin
|
||||
|
||||
package proc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestShutdown(t *testing.T) {
|
||||
SetTimeToForceQuit(time.Hour)
|
||||
assert.Equal(t, time.Hour, delayTimeBeforeForceQuit)
|
||||
|
||||
var val int
|
||||
called := AddWrapUpListener(func() {
|
||||
val++
|
||||
})
|
||||
WrapUp()
|
||||
called()
|
||||
assert.Equal(t, 1, val)
|
||||
|
||||
called = AddShutdownListener(func() {
|
||||
val += 2
|
||||
})
|
||||
Shutdown()
|
||||
called()
|
||||
assert.Equal(t, 3, val)
|
||||
}
|
||||
|
||||
func TestNotifyMoreThanOnce(t *testing.T) {
|
||||
ch := make(chan struct{}, 1)
|
||||
|
||||
go func() {
|
||||
var val int
|
||||
called := AddWrapUpListener(func() {
|
||||
val++
|
||||
})
|
||||
WrapUp()
|
||||
WrapUp()
|
||||
called()
|
||||
assert.Equal(t, 1, val)
|
||||
|
||||
called = AddShutdownListener(func() {
|
||||
val += 2
|
||||
})
|
||||
Shutdown()
|
||||
Shutdown()
|
||||
called()
|
||||
assert.Equal(t, 3, val)
|
||||
ch <- struct{}{}
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-ch:
|
||||
fmt.Printf("TestNotifyMoreThanOnce done\n")
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("timeout, check error logs")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user