From db20b766e2020950ed64a89065c8599a2ad69ff2 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期二, 14 一月 2020 18:01:12 +0800
Subject: [PATCH] update

---
 run.go |  103 ++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 77 insertions(+), 26 deletions(-)

diff --git a/run.go b/run.go
index ceb4a62..50cf58f 100644
--- a/run.go
+++ b/run.go
@@ -2,15 +2,18 @@
 
 import (
 	"context"
+	"os"
 	"sync"
 	"time"
 
-	"basic.com/libgowrapper/sdkhelper.git"
+	"face/common"
 
 	"basic.com/libgowrapper/sdkstruct.git"
 	"basic.com/pubsub/protomsg.git"
 	"basic.com/valib/gogpu.git"
 )
+
+const maxTryBeforeReboot = 10
 
 type face struct {
 	handle *SDKFace
@@ -29,6 +32,40 @@
 	ipc2Rule            string
 	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)
+		}
+	}
 }
 
 // Create create sdk
@@ -39,7 +76,7 @@
 		fn("Face SDK Create Error When New SDK")
 		return nil
 	}
-	cfg, err := sdkhelper.ReadConfig(config)
+	cfg, err := common.ReadConfig(config)
 	if err != nil {
 		fn("Face SDK Create Error When Read Config: ", err)
 		return nil
@@ -67,9 +104,9 @@
 	}
 
 	w, h, detThrd, detNum, detAngle, propThrd, extThrd, trckInterval, trckSmpl, maxChan, gpuM :=
-		1280, 720, sdkhelper.Atoi(cfg.Param[dt]), sdkhelper.Atoi(cfg.Param[dn]), sdkhelper.Atoi(cfg.Param[da]),
-		sdkhelper.Atoi(cfg.Param[pt]), sdkhelper.Atoi(cfg.Param[et]), sdkhelper.Atoi(cfg.Param[ti]), sdkhelper.Atoi(cfg.Param[ts]),
-		sdkhelper.Atoi(cfg.Param[mc]), sdkhelper.Atoi(cfg.Param[gm])
+		1280, 720, common.Atoi(cfg.Param[dt]), common.Atoi(cfg.Param[dn]), common.Atoi(cfg.Param[da]),
+		common.Atoi(cfg.Param[pt]), common.Atoi(cfg.Param[et]), common.Atoi(cfg.Param[ti]), common.Atoi(cfg.Param[ts]),
+		common.Atoi(cfg.Param[mc]), common.Atoi(cfg.Param[gm])
 
 	if detAngle > 0 {
 	}
@@ -114,6 +151,15 @@
 		ipc2Rule:            ipc2Rule,
 		ruleMsgMaxCacheSize: ruleMaxSize,
 		reserved:            reserved,
+
+		running:     true,
+		rebootUntil: maxTryBeforeReboot,
+	}
+}
+
+func (f *face) release() {
+	if f.handle != nil {
+		f.handle.Free()
 	}
 }
 
@@ -125,14 +171,14 @@
 		postPull = `_1`
 		postPush = `_2`
 	)
-	ipcRcv := sdkhelper.GetIpcAddress(s.shm, s.id+postPull)
-	ipcSnd := sdkhelper.GetIpcAddress(s.shm, s.id+postPush)
-	chRcv := make(chan []byte)
-	chSnd := make(chan sdkstruct.MsgSDK)
+	ipcRcv := common.GetIpcAddress(s.shm, s.id+postPull)
+	ipcSnd := common.GetIpcAddress(s.shm, s.id+postPush)
+	chRcv := make(chan []byte, s.maxChannel)
+	chSnd := make(chan sdkstruct.MsgSDK, s.maxChannel)
 
-	rcver := sdkhelper.NewReciever(ipcRcv, chRcv, s.shm, s.fnLogger)
-	snder := sdkhelper.NewSender(ipcSnd, chSnd, s.shm, s.fnLogger)
-	torule := sdkhelper.NewToRule(s.ipc2Rule, s.ruleMsgMaxCacheSize, s.fnLogger)
+	rcver := common.NewReciever(ipcRcv, chRcv, s.shm, s.fnLogger)
+	snder := common.NewSender(ipcSnd, chSnd, s.shm, s.fnLogger)
+	torule := common.NewToRule(s.ipc2Rule, s.ruleMsgMaxCacheSize, s.fnLogger)
 
 	snder.ApplyCallbackFunc(torule.Push)
 
@@ -141,11 +187,12 @@
 	go torule.Run(ctx)
 
 	go s.run(ctx, chRcv, chSnd)
+
+	go s.maybeReboot(ctx)
 }
 
 //////////////////////////////////////////////////////////////////
 const (
-	cacheFrameNum   = 3
 	trackChnTimeout = time.Duration(10)
 )
 
@@ -178,8 +225,8 @@
 
 func (f *face) run(ctx context.Context, in <-chan []byte, out chan<- sdkstruct.MsgSDK) {
 
-	chMsg := make(chan protomsg.SdkMessage)
-	go sdkhelper.UnserilizeProto(ctx, in, chMsg, f.fnLogger)
+	chMsg := make(chan protomsg.SdkMessage, f.maxChannel)
+	go common.UnserilizeProto(ctx, in, chMsg, f.fnLogger)
 
 	for {
 		select {
@@ -187,9 +234,9 @@
 			f.handle.Free()
 			return
 		case rMsg := <-chMsg:
-			if !sdkhelper.ValidRemoteMessage(rMsg, f.typ, f.fnLogger) {
+			if !common.ValidRemoteMessage(rMsg, f.typ, f.fnLogger) {
 				f.fnLogger("FACE TRACK VALIDREMOTEMESSAGE INVALID")
-				sdkhelper.EjectResult(nil, rMsg, out)
+				common.EjectResult(nil, rMsg, out)
 				continue
 			}
 
@@ -198,18 +245,18 @@
 				f.ftrackChans[rMsg.Cid] <- rMsg
 			} else {
 
-				f.ftrackChans[rMsg.Cid] = make(chan protomsg.SdkMessage, cacheFrameNum)
+				f.ftrackChans[rMsg.Cid] = make(chan protomsg.SdkMessage, f.maxChannel)
 				chn := f.getAvailableChn()
 				if chn < 0 {
 					f.fnLogger("TOO MUCH CHANNEL")
-					sdkhelper.EjectResult(nil, rMsg, out)
+					common.EjectResult(nil, rMsg, out)
 					continue
 				}
 				f.ftrackChannels[rMsg.Cid] = chn
 
-				i := sdkhelper.UnpackImage(rMsg, f.typ, f.fnLogger)
+				i := common.UnpackImage(rMsg, f.typ, f.fnLogger)
 				if i == nil {
-					sdkhelper.EjectResult(nil, rMsg, out)
+					common.EjectResult(nil, rMsg, out)
 					continue
 				}
 				// conv to bgr24 and resize
@@ -238,14 +285,14 @@
 
 		case rMsg := <-in:
 
-			if !sdkhelper.ValidRemoteMessage(rMsg, f.typ, f.fnLogger) {
-				sdkhelper.EjectResult(nil, rMsg, out)
+			if !common.ValidRemoteMessage(rMsg, f.typ, f.fnLogger) {
+				common.EjectResult(nil, rMsg, out)
 				continue
 			}
 
-			i := sdkhelper.UnpackImage(rMsg, f.typ, f.fnLogger)
+			i := common.UnpackImage(rMsg, f.typ, f.fnLogger)
 			if i == nil || i.Data == nil || i.Width <= 0 || i.Height <= 0 {
-				sdkhelper.EjectResult(nil, rMsg, out)
+				common.EjectResult(nil, rMsg, out)
 				continue
 			}
 
@@ -258,7 +305,11 @@
 
 			count, data, _ := f.handle.Run(i.Data, imgW, imgH, 3, dtchn)
 
-			sdkhelper.EjectResult(data, rMsg, out)
+			common.EjectResult(data, rMsg, out)
+			f.mtxRunning.Lock()
+			f.running = true
+			f.mtxRunning.Unlock()
+
 			var id, name string
 			if rMsg.Tasklab != nil {
 				id, name = rMsg.Tasklab.Taskid, rMsg.Tasklab.Taskname

--
Gitblit v1.8.0