15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const fs = require('fs');
const EventEmitter = require('events');
const Conf = require('../config/cache');
const Helper = require('../utils/helper');
const Ps = require('../ps');
const SpawnProcess = require('./spawnProcess');
const Channel = require('../const/channel');
const extend = require('../utils/extend');
const GetPort = require('../utils/get-port');
 
/**
 * Cross-language service
 * 跨语言服务
 */
const CrossLanguageService = {
 
  emitter: undefined,
 
  /**
   * pid唯一
   * {pid:{name,entity}, pid:{name,entity}, ...}
   */
  children: {},
 
  /**
   * name唯一
   * {name:pid, name:pid, ...}
   */
  childrenMap: {},
 
  /**
   * create
   */
  async create() {
 
    // boot services
    const servicesCfg = Conf.getValue('cross');
    //await Helper.sleep(5 * 1000);
 
    for (let key of Object.keys(servicesCfg)) {
      let cfg = servicesCfg[key];
      if (cfg.enable == true) {
        this.run(key)
      }
    }
  },
 
  /**
   * _initEventEmitter
   */
  _initEventEmitter() {
    if (this.emitter) {
      return
    }
    this.emitter = new EventEmitter();  
    this.emitter.on(Channel.events.childProcessExit, (data) => {
      const child = this.children[data.pid];
      delete this.childrenMap[child.name];
      delete this.children[data.pid];
    });
    this.emitter.on(Channel.events.childProcessError, (data) => {
      const child = this.children[data.pid];
      delete this.childrenMap[child.name];
      delete this.children[data.pid];
    });
  },
 
  /**
   * run
   */
  async run(service, opt = {}) {
    // init dir
    this._initPath();
 
    const allConfig = Conf.all();
    const defaultOpt = allConfig.cross[service] || {};
    const targetConf = extend(true, {}, defaultOpt, opt);
    if (Object.keys(targetConf).length == 0) {
      throw new Error(`[ee-core] [cross] The service [${service}] config does not exit`);
    }
 
    // eventEmitter
    this._initEventEmitter();
    
    // format params
    let tmpArgs = targetConf.args;
    let confPort = parseInt(Helper.getValueFromArgv(tmpArgs, 'port'));
    // 某些程序给它传入不存在的参数时会报错
    if (isNaN(confPort) && targetConf.port > 0) {
      confPort = targetConf.port;
    }
    if (confPort > 0) {
      // 动态生成port,传入的端口必须为int
      confPort = await GetPort({ port: confPort });
      // 替换port
      targetConf.args = Helper.replaceArgsValue(tmpArgs, "port", String(confPort));
    }
 
    // 创建进程
    const subProcess = new SpawnProcess(this, { targetConf, port: confPort });
    let uniqueName = targetConf.name;
    if (this.childrenMap.hasOwnProperty(uniqueName)) {
      uniqueName = uniqueName + "-" + String(subProcess.pid);
    }
    this.childrenMap[uniqueName] = subProcess.pid;
    subProcess.name = uniqueName;
    this.children[subProcess.pid] = {
      name: uniqueName,
      entity: subProcess
    };
 
    return subProcess;
  },
 
  killAll() {
    Object.keys(this.children).forEach(pid => {
      this.kill(pid)
    });
  },
 
  kill(pid) {
    const entity = this.getProc(pid);
    if (entity) {
      entity.kill();
    }
  },
 
  killByName(name) {
    const entity = this.getProcByName(name);
    if (entity) {
      entity.kill();
    }
  },
 
  getUrl(name) {
    const entity = this.getProcByName(name);
    const url = entity.getUrl();
 
    return url;
  },
 
  // 获取 proc
  getProcByName(name) {
    const pid = this.childrenMap[name];
    if (!pid) {
      throw new Error(`[ee-core] [cross] The process named [${name}] does not exit`);
    }
    const entity = this.getProc(pid);
 
    return entity;
  },
 
  // 获取 proc
  getProc(pid) {
    const child = this.children[pid];
    if (!pid) {
      throw new Error(`[ee-core] [cross] The process pid [${pid}] does not exit`);
    }
 
    return child.entity;
  },  
 
  /**
   * 获取pids
   */  
  getPids() {
    let pids = Object.keys(this.children);
    return pids;
  },  
 
  /**
   * init path
   */  
  _initPath() {
    const pathname = Ps.getUserHomeConfigDir();
    if (!fs.existsSync(pathname)) {
      Helper.mkdir(pathname, {mode: 0o755});
    }
  },
 
}
 
module.exports = CrossLanguageService;