From 677aeb36d939c7425e09b72cb20bed701951e5a2 Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期三, 15 一月 2020 15:58:46 +0800 Subject: [PATCH] debug --- run.go | 46 +++++++++++++++++++++-- /dev/null | 64 -------------------------------- 2 files changed, 42 insertions(+), 68 deletions(-) diff --git a/common/reboot.go b/common/reboot.go deleted file mode 100644 index e4a9e4c..0000000 --- a/common/reboot.go +++ /dev/null @@ -1,64 +0,0 @@ -package common - -import ( - "context" - "os" - "sync" - "time" -) - -// Disturber stop -type Disturber struct { - mtx sync.Mutex - live bool - until int - maxTry int -} - -// NewDisturber new -func NewDisturber(maxTry int) *Disturber { - return &Disturber{ - live: true, - until: 0, - maxTry: maxTry, - } -} - -// Prevent prevent -func (d *Disturber) Prevent() { - d.mtx.Lock() - defer d.mtx.Unlock() - d.live = true -} - -// MaybeReboot reboot -func (d *Disturber) MaybeReboot(ctx context.Context, fn func(...interface{})) { - d.live = true - for { - select { - case <-ctx.Done(): - return - default: - d.mtx.Lock() - running := d.live - d.mtx.Unlock() - - if running { - d.until = 0 - - d.mtx.Lock() - d.live = false - d.mtx.Unlock() - - } else { - d.until++ - fn("!!!!!!No Running: ", d.until) - if d.until > d.maxTry { - fn("!!!!!!Too Long Running, Reboot: ", d.maxTry) - os.Exit(0) - } - } - time.Sleep(time.Second) - } - } -} diff --git a/run.go b/run.go index d18577a..30256d6 100644 --- a/run.go +++ b/run.go @@ -2,6 +2,7 @@ import ( "context" + "os" "sync" "time" "unsafe" @@ -12,6 +13,8 @@ "basic.com/valib/gogpu.git" "github.com/gogo/protobuf/proto" ) + +const maxTryBeforeReboot = 10 type face struct { handle *SDKFace @@ -31,7 +34,39 @@ ruleMsgMaxCacheSize int reserved map[string]interface{} - stopper *common.Disturber + running bool + rebootUntil int + mtxRunning sync.Mutex +} + +func (f *face) maybeReboot(ctx context.Context) { + for { + select { + case <-ctx.Done(): + return + default: + f.mtxRunning.Lock() + running := f.running + f.mtxRunning.Unlock() + + if running { + f.rebootUntil = 0 + + f.mtxRunning.Lock() + f.running = false + f.mtxRunning.Unlock() + + } else { + f.rebootUntil++ + f.fnLogger("Face No Running: ", f.rebootUntil) + if f.rebootUntil > maxTryBeforeReboot { + f.fnLogger("Face Too Long Running, Reboot") + os.Exit(0) + } + } + time.Sleep(time.Second) + } + } } // Create create sdk @@ -118,7 +153,8 @@ ruleMsgMaxCacheSize: ruleMaxSize, reserved: reserved, - stopper: common.NewDisturber(10), + running: true, + rebootUntil: maxTryBeforeReboot, } } @@ -153,7 +189,7 @@ go s.run(ctx, chRcv, chSnd) - go s.stopper.MaybeReboot(ctx, s.fnLogger) + go s.maybeReboot(ctx) } ////////////////////////////////////////////////////////////////// @@ -316,7 +352,9 @@ // f.fnLogger("Face~~~EjectResult", dtchn) common.EjectResult(data, rMsg, out) // f.fnLogger("Face~~~EjectResult Over", dtchn) - f.stopper.Prevent() + f.mtxRunning.Lock() + f.running = true + f.mtxRunning.Unlock() var id, name string if rMsg.Msg.Tasklab != nil { -- Gitblit v1.8.0