-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from guoming0000/main
add reboot for utils
- Loading branch information
Showing
3 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package reboot | ||
|
||
//package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"runtime" | ||
"time" | ||
|
||
"github.com/sunmi-OS/gocore/v2/glog" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
|
||
func AutoRestart(ctx context.Context, f func() error) error { | ||
defer func() { | ||
var r any | ||
if r = recover(); r != nil { | ||
buf := make([]byte, 64*1024) | ||
buf = buf[:runtime.Stack(buf, false)] | ||
fmt.Fprintf(os.Stderr, "reboot: panic recovered: %s\n%s\n", r, buf) | ||
glog.Error("panic in reboot proc, err: %s, stack: %s", r, buf) | ||
|
||
AutoRestart(ctx, f) // panic, restart | ||
} | ||
}() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
glog.InfoC(ctx, "reboot AutoRestart ctx done") | ||
return ctx.Err() | ||
default: | ||
err := f() | ||
if errors.Is(err, context.Canceled) { | ||
return nil | ||
} | ||
// 如果返回异常,先休息会 | ||
if err != nil { | ||
time.Sleep(time.Second * 3) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
|
||
func AsyncFunc(g *errgroup.Group, f func() error) error { | ||
defer func() { | ||
var r any | ||
if r = recover(); r != nil { | ||
buf := make([]byte, 64*1024) | ||
buf = buf[:runtime.Stack(buf, false)] | ||
fmt.Fprintf(os.Stderr, "reboot: panic recovered: %s\n%s\n", r, buf) | ||
glog.FatalF("panic in reboot proc, err: %s, stack: %s", r, buf) | ||
|
||
restartAsyncLoop(g, f) // panic, restart | ||
} | ||
}() | ||
|
||
return f() | ||
} | ||
|
||
func restartAsyncLoop(g *errgroup.Group, f func() error) { | ||
f2 := func() error { | ||
return AsyncFunc(g, f) | ||
} | ||
g.Go(f2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package reboot | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/sunmi-OS/gocore/v2/glog" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
func startBooter(g *errgroup.Group, f func() error) { | ||
|
||
f2 := func() error { | ||
return AsyncFunc(g, f) | ||
} | ||
g.Go(f2) | ||
} | ||
|
||
func TestBootWithErrgroup(t *testing.T) { | ||
var g errgroup.Group | ||
|
||
startBooter(&g, bizFunc) | ||
|
||
if err := g.Wait(); err != nil { | ||
fmt.Println("err:", err) | ||
} | ||
|
||
time.Sleep(20 * time.Second) | ||
|
||
} | ||
|
||
func bizFunc() error { | ||
// manual make panic for test | ||
for i := 0; i < 10; i++ { | ||
glog.InfoF("running in goroutine:%d", i) | ||
time.Sleep(1 * time.Second) | ||
if i == 5 { | ||
panic("just boom") | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// use method | ||
func TestBootAutoRestart(t *testing.T) { | ||
ctx, canel := context.WithCancel(context.Background()) | ||
go AutoRestart(ctx, bizFunc) | ||
|
||
time.Sleep(20 * time.Second) | ||
canel() | ||
time.Sleep(20 * time.Second) | ||
} |