zhangmeng
2020-01-20 accc3295d6daacd70494ecd3a08033f505c29f66
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package master
 
import (
    "analysis/app"
    "analysis/logo"
    "analysis/util"
    "context"
    "encoding/json"
    "os"
    "strconv"
    "strings"
    "time"
 
    "basic.com/libgowrapper/sdkstruct.git"
    "basic.com/valib/pubsub.git"
)
 
func reaper(ctxt context.Context) {
    pidChan := make(chan int, 1)
    Reap(pidChan)
    go waitForRestart(ctxt, pidChan)
}
 
// Run run
func Run(ctx context.Context, soFile, configPath string) bool {
    reaper(ctx)
 
    fetcher := NewFetcher(soFile)
    if fetcher == nil {
        logo.Errorln("New Fetcher Load so File Funcs Error From File: ", soFile)
        return false
    }
 
    logo.Infoln("~~~~~~Created Fetcher, Now Sync From DB")
 
    ip := "tcp://" + util.FSI.IP
    url := ip + ":" + strconv.Itoa(util.FSI.DataPort)
    hearturl := ip + ":" + strconv.Itoa(util.FSI.HBPort)
 
    chMsg, err := fetcher.fnInit(url, hearturl, 0, []string{pubsub.Topic_Sdk}, "analysis-master"+strconv.Itoa(os.Getpid()))
    for {
        if err == nil {
            break
        }
        logo.Infoln("Analysis Fetcher INIT Error! URL:", url)
        time.Sleep(time.Second)
        chMsg, err = fetcher.fnInit(url, hearturl, 0, []string{pubsub.Topic_Sdk}, "analysis-master"+strconv.Itoa(os.Getpid()))
    }
 
    for {
        select {
        case <-ctx.Done():
            return true
        case msg := <-chMsg:
            //              sdktype       process_name   topic        null
            //                yolo/face  yolo_0/yolo_1  channel
            var sdk map[string](map[string](map[string]interface{}))
 
            if err := json.Unmarshal(msg.Msg, &sdk); err != nil {
                logo.Infoln("Fetcher SDK unmarshal err:", err)
                continue
            }
 
            logo.Infoln("~~~~~~Recv New SDKInfos")
            chCameras <- CameraInfo{
                Cameras: cameras,
            }
            logo.Infoln("~~~~~~Recv New SDKInfos Over")
 
        default:
            time.Sleep(10 * time.Millisecond)
        }
    }
 
}
 
func manualStart(ctx context.Context, sdks []sdkstruct.SDKInfo, configPath string) bool {
    rPath := configPath
 
    params := app.GetParams()
 
    for _, v := range sdks {
 
        file := rPath + v.SdkType + ".json"
        if rPath[len(rPath)-1] != '/' {
            file = rPath + "/" + v.SdkType + ".json"
        }
 
        cfg, err := app.ReadConfig(file)
        if err != nil {
            logo.Errorln("Master Read: ", file, " Config Error: ", err)
            continue
        }
 
        if len(cfg.Env) > 0 {
            envs := strings.Split(cfg.Env, ":")
            normal := true
            for _, v := range envs {
                if !util.IsFileExist(v) {
                    normal = false
                    break
                }
            }
            if !normal {
                logo.Infoln("Can't Find Runtime Path, Skip It: ", file)
                continue
            }
        }
 
        logo.Infoln(file, " CONFIG: ", cfg)
 
        args := []string{
            `-role=slave`,
            "-sdk=" + v.SdkType,
            "-id=" + v.IpcID,
            "-" + util.ConfigPath + "=" + file,
        }
 
        args = append(args, params...)
        pid, err := runProc(ctx, "./analysis", args, cfg.Env)
 
        if err != nil {
            logo.Errorf("ANALYSIS START SLAVE PROC %s IPC: %s error %+v\n", v.SdkType, v.IpcID, err)
        }
        logo.Infof("START SDK %s ID %s PID %d Env: %s\n", v.SdkType, v.IpcID, pid, cfg.Env)
    }
    return true
 
}