派生自 libgowrapper/face

zhangmeng
2020-01-15 54dfcb8da5af779453051ab2f9657115083eb57d
debug
1个文件已添加
1个文件已修改
110 ■■■■■ 已修改文件
common/reboot.go 64 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
run.go 46 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
common/reboot.go
New file
@@ -0,0 +1,64 @@
package common
import (
    "context"
    "os"
    "sync"
    "time"
)
const maxTryBeforeReboot = 10
// Disturber stop
type Disturber struct {
    mtx   sync.Mutex
    live  bool
    until int
}
// NewDisturber new
func NewDisturber() *Disturber {
    return &Disturber{
        live:  true,
        until: 0,
    }
}
// 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("Face No Running: ", d.until)
                if d.until > maxTryBeforeReboot {
                    fn("Face Too Long Running, Reboot")
                    os.Exit(0)
                }
            }
            time.Sleep(time.Second)
        }
    }
}
run.go
@@ -2,7 +2,6 @@
import (
    "context"
    "os"
    "sync"
    "time"
    "unsafe"
@@ -13,8 +12,6 @@
    "basic.com/valib/gogpu.git"
    "github.com/gogo/protobuf/proto"
)
const maxTryBeforeReboot = 10
type face struct {
    handle *SDKFace
@@ -34,39 +31,7 @@
    ruleMsgMaxCacheSize int
    reserved            map[string]interface{}
    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)
        }
    }
    stopper *common.Disturber
}
// Create create sdk
@@ -153,8 +118,7 @@
        ruleMsgMaxCacheSize: ruleMaxSize,
        reserved:            reserved,
        running:     true,
        rebootUntil: maxTryBeforeReboot,
        stopper: common.NewDisturber(),
    }
}
@@ -189,7 +153,7 @@
    go s.run(ctx, chRcv, chSnd)
    go s.maybeReboot(ctx)
    go s.stopper.MaybeReboot(ctx, s.fnLogger)
}
//////////////////////////////////////////////////////////////////
@@ -352,9 +316,7 @@
            // f.fnLogger("Face~~~EjectResult", dtchn)
            common.EjectResult(data, rMsg, out)
            // f.fnLogger("Face~~~EjectResult Over", dtchn)
            f.mtxRunning.Lock()
            f.running = true
            f.mtxRunning.Unlock()
            f.stopper.Prevent()
            var id, name string
            if rMsg.Msg.Tasklab != nil {