mirror of
https://github.com/safing/portbase
synced 2025-09-04 03:29:59 +00:00
Add start complete signal to modules
This commit is contained in:
parent
cc4e2aa6eb
commit
a4db87e6ce
3 changed files with 35 additions and 3 deletions
|
@ -10,8 +10,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
startComplete = abool.NewBool(false)
|
|
||||||
|
|
||||||
modulesLock sync.Mutex
|
modulesLock sync.Mutex
|
||||||
modules = make(map[string]*Module)
|
modules = make(map[string]*Module)
|
||||||
modulesOrder []*Module
|
modulesOrder []*Module
|
||||||
|
|
|
@ -89,6 +89,7 @@ func TestErrors(t *testing.T) {
|
||||||
modules = make(map[string]*Module)
|
modules = make(map[string]*Module)
|
||||||
modulesOrder = make([]*Module, 0)
|
modulesOrder = make([]*Module, 0)
|
||||||
startComplete.UnSet()
|
startComplete.UnSet()
|
||||||
|
startCompleteSignal = make(chan struct{})
|
||||||
|
|
||||||
// test prep error
|
// test prep error
|
||||||
Register("prepfail", testFail, testStart("prepfail"), testStop("prepfail"))
|
Register("prepfail", testFail, testStart("prepfail"), testStop("prepfail"))
|
||||||
|
@ -101,6 +102,7 @@ func TestErrors(t *testing.T) {
|
||||||
modules = make(map[string]*Module)
|
modules = make(map[string]*Module)
|
||||||
modulesOrder = make([]*Module, 0)
|
modulesOrder = make([]*Module, 0)
|
||||||
startComplete.UnSet()
|
startComplete.UnSet()
|
||||||
|
startCompleteSignal = make(chan struct{})
|
||||||
|
|
||||||
// test prep clean exit
|
// test prep clean exit
|
||||||
Register("prepcleanexit", testCleanExit, testStart("prepcleanexit"), testStop("prepcleanexit"))
|
Register("prepcleanexit", testCleanExit, testStart("prepcleanexit"), testStop("prepcleanexit"))
|
||||||
|
@ -113,6 +115,7 @@ func TestErrors(t *testing.T) {
|
||||||
modules = make(map[string]*Module)
|
modules = make(map[string]*Module)
|
||||||
modulesOrder = make([]*Module, 0)
|
modulesOrder = make([]*Module, 0)
|
||||||
startComplete.UnSet()
|
startComplete.UnSet()
|
||||||
|
startCompleteSignal = make(chan struct{})
|
||||||
|
|
||||||
// test invalid dependency
|
// test invalid dependency
|
||||||
Register("database", testPrep, testStart("database"), testStop("database"), "invalid")
|
Register("database", testPrep, testStart("database"), testStop("database"), "invalid")
|
||||||
|
@ -131,6 +134,7 @@ func TestErrors(t *testing.T) {
|
||||||
modules = make(map[string]*Module)
|
modules = make(map[string]*Module)
|
||||||
modulesOrder = make([]*Module, 0)
|
modulesOrder = make([]*Module, 0)
|
||||||
startComplete.UnSet()
|
startComplete.UnSet()
|
||||||
|
startCompleteSignal = make(chan struct{})
|
||||||
|
|
||||||
// test dependency loop
|
// test dependency loop
|
||||||
Register("database", testPrep, testStart("database"), testStop("database"), "helper")
|
Register("database", testPrep, testStart("database"), testStop("database"), "helper")
|
||||||
|
@ -144,6 +148,7 @@ func TestErrors(t *testing.T) {
|
||||||
modules = make(map[string]*Module)
|
modules = make(map[string]*Module)
|
||||||
modulesOrder = make([]*Module, 0)
|
modulesOrder = make([]*Module, 0)
|
||||||
startComplete.UnSet()
|
startComplete.UnSet()
|
||||||
|
startCompleteSignal = make(chan struct{})
|
||||||
|
|
||||||
// test failing module start
|
// test failing module start
|
||||||
Register("startfail", testPrep, testFail, testStop("startfail"))
|
Register("startfail", testPrep, testFail, testStop("startfail"))
|
||||||
|
@ -156,6 +161,7 @@ func TestErrors(t *testing.T) {
|
||||||
modules = make(map[string]*Module)
|
modules = make(map[string]*Module)
|
||||||
modulesOrder = make([]*Module, 0)
|
modulesOrder = make([]*Module, 0)
|
||||||
startComplete.UnSet()
|
startComplete.UnSet()
|
||||||
|
startCompleteSignal = make(chan struct{})
|
||||||
|
|
||||||
// test failing module stop
|
// test failing module stop
|
||||||
Register("stopfail", testPrep, testStart("stopfail"), testFail)
|
Register("stopfail", testPrep, testStart("stopfail"), testFail)
|
||||||
|
@ -172,6 +178,7 @@ func TestErrors(t *testing.T) {
|
||||||
modules = make(map[string]*Module)
|
modules = make(map[string]*Module)
|
||||||
modulesOrder = make([]*Module, 0)
|
modulesOrder = make([]*Module, 0)
|
||||||
startComplete.UnSet()
|
startComplete.UnSet()
|
||||||
|
startCompleteSignal = make(chan struct{})
|
||||||
|
|
||||||
// test help flag
|
// test help flag
|
||||||
helpFlag = true
|
helpFlag = true
|
||||||
|
|
|
@ -6,8 +6,31 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/Safing/portbase/log"
|
"github.com/Safing/portbase/log"
|
||||||
|
"github.com/tevino/abool"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
startComplete = abool.NewBool(false)
|
||||||
|
startCompleteSignal = make(chan struct{})
|
||||||
|
)
|
||||||
|
|
||||||
|
// markStartComplete marks the startup as completed.
|
||||||
|
func markStartComplete() {
|
||||||
|
if startComplete.SetToIf(false, true) {
|
||||||
|
close(startCompleteSignal)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// StartCompleted returns whether starting has completed.
|
||||||
|
func StartCompleted() bool {
|
||||||
|
return startComplete.IsSet()
|
||||||
|
}
|
||||||
|
|
||||||
|
// WaitForStartCompletion returns as soon as starting has completed.
|
||||||
|
func WaitForStartCompletion() <-chan struct{} {
|
||||||
|
return startCompleteSignal
|
||||||
|
}
|
||||||
|
|
||||||
// Start starts all modules in the correct order. In case of an error, it will automatically shutdown again.
|
// Start starts all modules in the correct order. In case of an error, it will automatically shutdown again.
|
||||||
func Start() error {
|
func Start() error {
|
||||||
modulesLock.Lock()
|
modulesLock.Lock()
|
||||||
|
@ -44,8 +67,12 @@ func Start() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
startComplete.Set()
|
// complete startup
|
||||||
log.Infof("modules: started %d modules", len(modules))
|
log.Infof("modules: started %d modules", len(modules))
|
||||||
|
if startComplete.SetToIf(false, true) {
|
||||||
|
close(startCompleteSignal)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue