Skip to content

Commit

Permalink
great update, disruptive
Browse files Browse the repository at this point in the history
  • Loading branch information
PotatoCloud committed Jan 17, 2023
1 parent 98dd955 commit 8b3b55a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
22 changes: 9 additions & 13 deletions ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,27 @@ type DestroyUnit struct {
}

func registerDestroy(seq int, name string, unit Handler) {
destroys = append(destroys, DestroyUnit{
destroyUnits = append(destroyUnits, DestroyUnit{
Seq: seq,
Name: name,
Unit: unit,
})
}

func SetupActive(fn func()) {
activeFunc = fn
}

func RegisterHandler(seq int, name string, handler func(ctx *context.Context) Handler) {
creates = append(creates, Unit{
units = append(units, Unit{
Seq: seq,
Name: name,
Handle: handler,
})
}

func sortCreates() {
sort.Slice(creates, func(i, j int) bool {
return creates[i].Seq < creates[j].Seq
})
}

func init() {
func Bootstrap(activeFunc func()) {
if activeFunc == nil {
panic("===== unset active func =====")
}
activeFunc()
sort.Slice(units, func(i, j int) bool {
return units[i].Seq < units[j].Seq
})
}
2 changes: 1 addition & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func main() {

// register services
pkgCtl.SetupActive(example_units.Active)
pkgCtl.Bootstrap(example_units.Active)

var (
ctx, cancel = context.WithCancel(context.Background())
Expand Down
5 changes: 2 additions & 3 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
)

var (
activeFunc = func() {}
closeListener = make(chan struct{}, 1)
cancelFunc context.CancelFunc
creates = make([]Unit, 0)
destroys = make([]DestroyUnit, 0)
units = make([]Unit, 0)
destroyUnits = make([]DestroyUnit, 0)
)

var Log *log.Logger
Expand Down
13 changes: 6 additions & 7 deletions tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func ForceExit() error {
// this func can only be called when ListenAndDestroy is used, otherwise an error will be return
func Exit() error {
if cancelFunc == nil {
return errors.New("not set cancelFunc")
return errors.New("unset cancelFunc")
}
return Destroy(cancelFunc)
}
Expand All @@ -50,11 +50,11 @@ func ExitWithTimeout(d time.Duration) error {
// Destroy unregister all services immediately after calling
func Destroy(cancel context.CancelFunc) (err error) {
cancel()
for i := 0; i < len(destroys); i++ {
if err = destroys[i].Unit.Destroy(); err != nil {
for i := 0; i < len(destroyUnits); i++ {
if err = destroyUnits[i].Unit.Destroy(); err != nil {
Log.Printf(
"unit %s destroy fail, error: %s",
destroys[i].Name,
destroyUnits[i].Name,
err.Error(),
)
}
Expand All @@ -66,8 +66,7 @@ func Destroy(cancel context.CancelFunc) (err error) {

// Startup all registered services in order
func Startup(ctx *context.Context) error {
sortCreates()
size := len(creates)
size := len(units)
if size == 0 {
return errors.New("no unit require register")
}
Expand All @@ -79,7 +78,7 @@ func Startup(ctx *context.Context) error {
if len(ec) == 1 {
return <-ec
}
unit := creates[i]
unit := units[i]
uHandler := unit.Handle(ctx)
if err = uHandler.Create(); err != nil {
Log.Printf("unit %s create fail, error: %s", unit.Name, err.Error())
Expand Down

0 comments on commit 8b3b55a

Please sign in to comment.