@bigtyphoon/melo 1.7.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (296) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +55 -0
  3. package/bin/commadtest.ts +10 -0
  4. package/bin/commands/add.ts +43 -0
  5. package/bin/commands/init.ts +292 -0
  6. package/bin/commands/kill.ts +21 -0
  7. package/bin/commands/list.ts +60 -0
  8. package/bin/commands/masterha.ts +40 -0
  9. package/bin/commands/restart.ts +48 -0
  10. package/bin/commands/start.ts +65 -0
  11. package/bin/commands/stop.ts +26 -0
  12. package/bin/melo.ts +30 -0
  13. package/bin/utils/constants.ts +27 -0
  14. package/bin/utils/utils.ts +130 -0
  15. package/dist/bin/commadtest.js +9 -0
  16. package/dist/bin/commands/add.js +40 -0
  17. package/dist/bin/commands/init.js +279 -0
  18. package/dist/bin/commands/kill.js +21 -0
  19. package/dist/bin/commands/list.js +65 -0
  20. package/dist/bin/commands/masterha.js +36 -0
  21. package/dist/bin/commands/restart.js +45 -0
  22. package/dist/bin/commands/start.js +58 -0
  23. package/dist/bin/commands/stop.js +20 -0
  24. package/dist/bin/melo.js +26 -0
  25. package/dist/bin/utils/constants.js +28 -0
  26. package/dist/bin/utils/utils.js +134 -0
  27. package/dist/lib/application.js +888 -0
  28. package/dist/lib/common/manager/appManager.js +112 -0
  29. package/dist/lib/common/manager/taskManager.js +39 -0
  30. package/dist/lib/common/remote/backend/msgRemote.js +63 -0
  31. package/dist/lib/common/remote/frontend/channelRemote.js +78 -0
  32. package/dist/lib/common/remote/frontend/sessionRemote.js +76 -0
  33. package/dist/lib/common/service/backendSessionService.js +337 -0
  34. package/dist/lib/common/service/channelService.js +514 -0
  35. package/dist/lib/common/service/connectionService.js +95 -0
  36. package/dist/lib/common/service/filterService.js +112 -0
  37. package/dist/lib/common/service/handlerService.js +187 -0
  38. package/dist/lib/common/service/sessionService.js +610 -0
  39. package/dist/lib/components/backendSession.js +14 -0
  40. package/dist/lib/components/channel.js +13 -0
  41. package/dist/lib/components/connection.js +12 -0
  42. package/dist/lib/components/connector.js +437 -0
  43. package/dist/lib/components/dictionary.js +93 -0
  44. package/dist/lib/components/master.js +39 -0
  45. package/dist/lib/components/monitor.js +25 -0
  46. package/dist/lib/components/protobuf.js +156 -0
  47. package/dist/lib/components/proxy.js +236 -0
  48. package/dist/lib/components/pushScheduler.js +62 -0
  49. package/dist/lib/components/remote.js +127 -0
  50. package/dist/lib/components/server.js +63 -0
  51. package/dist/lib/components/session.js +20 -0
  52. package/dist/lib/connectors/commands/handshake.js +119 -0
  53. package/dist/lib/connectors/commands/heartbeat.js +67 -0
  54. package/dist/lib/connectors/commands/kick.js +15 -0
  55. package/dist/lib/connectors/common/coder.js +90 -0
  56. package/dist/lib/connectors/common/handler.js +57 -0
  57. package/dist/lib/connectors/hybrid/IHybridSocket.js +3 -0
  58. package/dist/lib/connectors/hybrid/switcher.js +100 -0
  59. package/dist/lib/connectors/hybrid/tcpprocessor.js +40 -0
  60. package/dist/lib/connectors/hybrid/tcpsocket.js +171 -0
  61. package/dist/lib/connectors/hybrid/wsprocessor.js +49 -0
  62. package/dist/lib/connectors/hybridconnector.js +89 -0
  63. package/dist/lib/connectors/hybridsocket.js +139 -0
  64. package/dist/lib/connectors/mqtt/generate.js +113 -0
  65. package/dist/lib/connectors/mqtt/mqttadaptor.js +81 -0
  66. package/dist/lib/connectors/mqtt/protocol.js +48 -0
  67. package/dist/lib/connectors/mqttconnector.js +107 -0
  68. package/dist/lib/connectors/mqttsocket.js +59 -0
  69. package/dist/lib/connectors/sioconnector.js +135 -0
  70. package/dist/lib/connectors/siosocket.js +69 -0
  71. package/dist/lib/connectors/udpconnector.js +76 -0
  72. package/dist/lib/connectors/udpsocket.js +93 -0
  73. package/dist/lib/filters/handler/serial.js +44 -0
  74. package/dist/lib/filters/handler/time.js +32 -0
  75. package/dist/lib/filters/handler/timeout.js +45 -0
  76. package/dist/lib/filters/handler/toobusy.js +36 -0
  77. package/dist/lib/filters/rpc/rpcLog.js +43 -0
  78. package/dist/lib/filters/rpc/toobusy.js +41 -0
  79. package/dist/lib/index.js +81 -0
  80. package/dist/lib/interfaces/IComponent.js +3 -0
  81. package/dist/lib/interfaces/IConnector.js +3 -0
  82. package/dist/lib/interfaces/IHandlerFilter.js +3 -0
  83. package/dist/lib/interfaces/ILifeCycle.js +3 -0
  84. package/dist/lib/interfaces/IPlugin.js +3 -0
  85. package/dist/lib/interfaces/IPushScheduler.js +3 -0
  86. package/dist/lib/interfaces/ISocket.js +3 -0
  87. package/dist/lib/interfaces/IStore.js +3 -0
  88. package/dist/lib/interfaces/define.js +3 -0
  89. package/dist/lib/master/master.js +129 -0
  90. package/dist/lib/master/starter.js +236 -0
  91. package/dist/lib/master/watchdog.js +120 -0
  92. package/dist/lib/melo.js +125 -0
  93. package/dist/lib/modules/console.js +436 -0
  94. package/dist/lib/modules/masterwatcher.js +98 -0
  95. package/dist/lib/modules/monitorwatcher.js +124 -0
  96. package/dist/lib/modules/onlineUser.js +69 -0
  97. package/dist/lib/modules/restartNotifyModule.js +107 -0
  98. package/dist/lib/modules/watchServer.js +737 -0
  99. package/dist/lib/monitor/monitor.js +80 -0
  100. package/dist/lib/pushSchedulers/buffer.js +96 -0
  101. package/dist/lib/pushSchedulers/direct.js +58 -0
  102. package/dist/lib/pushSchedulers/multi.js +80 -0
  103. package/dist/lib/server/server.js +500 -0
  104. package/dist/lib/util/appUtil.js +306 -0
  105. package/dist/lib/util/constants.js +117 -0
  106. package/dist/lib/util/countDownLatch.js +51 -0
  107. package/dist/lib/util/events.js +20 -0
  108. package/dist/lib/util/handlerHelper.js +8 -0
  109. package/dist/lib/util/log.js +14 -0
  110. package/dist/lib/util/moduleUtil.js +101 -0
  111. package/dist/lib/util/pathUtil.js +134 -0
  112. package/dist/lib/util/remoterHelper.js +8 -0
  113. package/dist/lib/util/utils.js +358 -0
  114. package/dist/test/application.js +522 -0
  115. package/dist/test/config/log4js.json +28 -0
  116. package/dist/test/config/master.json +9 -0
  117. package/dist/test/config/servers.json +6 -0
  118. package/dist/test/filters/handler/serial.js +41 -0
  119. package/dist/test/filters/handler/time.js +41 -0
  120. package/dist/test/filters/handler/timeout.js +41 -0
  121. package/dist/test/filters/handler/toobusy.js +57 -0
  122. package/dist/test/filters/rpc/rpcLog.js +22 -0
  123. package/dist/test/filters/rpc/toobusy.js +39 -0
  124. package/dist/test/manager/mockChannelManager.js +77 -0
  125. package/dist/test/manager/taskManager.js +68 -0
  126. package/dist/test/mock-base/app/servers/other-file +0 -0
  127. package/dist/test/mock-plugin/components/mockPlugin.js +10 -0
  128. package/dist/test/mock-plugin/events/mockEvent.js +12 -0
  129. package/dist/test/modules/console.js +242 -0
  130. package/dist/test/pomelo.js +19 -0
  131. package/dist/test/remote/channelRemote.js +159 -0
  132. package/dist/test/service/channel.js +134 -0
  133. package/dist/test/service/channelService.js +216 -0
  134. package/dist/test/service/connectionService.js +114 -0
  135. package/dist/test/service/filterService.js +144 -0
  136. package/dist/test/service/handlerService.js +65 -0
  137. package/dist/test/service/sessionService.js +387 -0
  138. package/dist/test/util/countDownLatch.js +70 -0
  139. package/dist/test/util/pathUtil.js +108 -0
  140. package/dist/test/util/utils.js +140 -0
  141. package/lib/application.ts +1240 -0
  142. package/lib/common/manager/appManager.ts +118 -0
  143. package/lib/common/manager/taskManager.ts +50 -0
  144. package/lib/common/remote/backend/msgRemote.ts +134 -0
  145. package/lib/common/remote/frontend/channelRemote.ts +91 -0
  146. package/lib/common/remote/frontend/sessionRemote.ts +91 -0
  147. package/lib/common/service/backendSessionService.ts +388 -0
  148. package/lib/common/service/channelService.ts +609 -0
  149. package/lib/common/service/connectionService.ts +112 -0
  150. package/lib/common/service/filterService.ts +118 -0
  151. package/lib/common/service/handlerService.ts +224 -0
  152. package/lib/common/service/sessionService.ts +731 -0
  153. package/lib/components/backendSession.ts +14 -0
  154. package/lib/components/channel.ts +11 -0
  155. package/lib/components/connection.ts +13 -0
  156. package/lib/components/connector.ts +533 -0
  157. package/lib/components/dictionary.ts +121 -0
  158. package/lib/components/master.ts +41 -0
  159. package/lib/components/monitor.ts +30 -0
  160. package/lib/components/protobuf.ts +208 -0
  161. package/lib/components/proxy.ts +282 -0
  162. package/lib/components/pushScheduler.ts +70 -0
  163. package/lib/components/remote.ts +166 -0
  164. package/lib/components/server.ts +71 -0
  165. package/lib/components/session.ts +22 -0
  166. package/lib/connectors/commands/handshake.ts +155 -0
  167. package/lib/connectors/commands/heartbeat.ts +83 -0
  168. package/lib/connectors/commands/kick.ts +11 -0
  169. package/lib/connectors/common/coder.ts +93 -0
  170. package/lib/connectors/common/handler.ts +62 -0
  171. package/lib/connectors/hybrid/IHybridSocket.ts +9 -0
  172. package/lib/connectors/hybrid/switcher.ts +142 -0
  173. package/lib/connectors/hybrid/tcpprocessor.ts +43 -0
  174. package/lib/connectors/hybrid/tcpsocket.ts +223 -0
  175. package/lib/connectors/hybrid/wsprocessor.ts +57 -0
  176. package/lib/connectors/hybridconnector.ts +134 -0
  177. package/lib/connectors/hybridsocket.ts +168 -0
  178. package/lib/connectors/mqtt/generate.ts +103 -0
  179. package/lib/connectors/mqtt/mqttadaptor.ts +114 -0
  180. package/lib/connectors/mqtt/protocol.ts +49 -0
  181. package/lib/connectors/mqttconnector.ts +134 -0
  182. package/lib/connectors/mqttsocket.ts +79 -0
  183. package/lib/connectors/sioconnector.ts +161 -0
  184. package/lib/connectors/siosocket.ts +85 -0
  185. package/lib/connectors/udpconnector.ts +113 -0
  186. package/lib/connectors/udpsocket.ts +110 -0
  187. package/lib/filters/handler/serial.ts +46 -0
  188. package/lib/filters/handler/time.ts +35 -0
  189. package/lib/filters/handler/timeout.ts +50 -0
  190. package/lib/filters/handler/toobusy.ts +37 -0
  191. package/lib/filters/rpc/rpcLog.ts +42 -0
  192. package/lib/filters/rpc/toobusy.ts +41 -0
  193. package/lib/index.ts +74 -0
  194. package/lib/interfaces/IComponent.ts +47 -0
  195. package/lib/interfaces/IConnector.ts +20 -0
  196. package/lib/interfaces/IHandlerFilter.ts +15 -0
  197. package/lib/interfaces/ILifeCycle.ts +16 -0
  198. package/lib/interfaces/IPlugin.ts +65 -0
  199. package/lib/interfaces/IPushScheduler.ts +52 -0
  200. package/lib/interfaces/ISocket.ts +26 -0
  201. package/lib/interfaces/IStore.ts +10 -0
  202. package/lib/interfaces/define.ts +15 -0
  203. package/lib/master/master.ts +148 -0
  204. package/lib/master/starter.ts +234 -0
  205. package/lib/master/watchdog.ts +135 -0
  206. package/lib/melo.ts +152 -0
  207. package/lib/modules/console.ts +465 -0
  208. package/lib/modules/masterwatcher.ts +120 -0
  209. package/lib/modules/monitorwatcher.ts +151 -0
  210. package/lib/modules/onlineUser.ts +78 -0
  211. package/lib/modules/restartNotifyModule.ts +128 -0
  212. package/lib/modules/watchServer.ts +766 -0
  213. package/lib/monitor/monitor.ts +99 -0
  214. package/lib/pushSchedulers/buffer.ts +117 -0
  215. package/lib/pushSchedulers/direct.ts +65 -0
  216. package/lib/pushSchedulers/multi.ts +94 -0
  217. package/lib/server/server.ts +554 -0
  218. package/lib/util/appUtil.ts +313 -0
  219. package/lib/util/constants.ts +154 -0
  220. package/lib/util/countDownLatch.ts +72 -0
  221. package/lib/util/events.ts +15 -0
  222. package/lib/util/handlerHelper.ts +5 -0
  223. package/lib/util/log.ts +11 -0
  224. package/lib/util/moduleUtil.ts +110 -0
  225. package/lib/util/pathUtil.ts +132 -0
  226. package/lib/util/remoterHelper.ts +68 -0
  227. package/lib/util/utils.ts +365 -0
  228. package/package.json +93 -0
  229. package/template/game-server/.vscode/launch.json +27 -0
  230. package/template/game-server/app/servers/connector/handler/entryHandler.ts +50 -0
  231. package/template/game-server/app/servers/connector/remote/authRemoter.ts +36 -0
  232. package/template/game-server/app.ts +30 -0
  233. package/template/game-server/app.ts.mqtt +32 -0
  234. package/template/game-server/app.ts.sio +36 -0
  235. package/template/game-server/app.ts.sio.wss +34 -0
  236. package/template/game-server/app.ts.udp +31 -0
  237. package/template/game-server/app.ts.wss +40 -0
  238. package/template/game-server/config/adminServer.json +5 -0
  239. package/template/game-server/config/adminUser.json +22 -0
  240. package/template/game-server/config/clientProtos.json +1 -0
  241. package/template/game-server/config/dictionary.json +1 -0
  242. package/template/game-server/config/log4js.json +150 -0
  243. package/template/game-server/config/master.json +8 -0
  244. package/template/game-server/config/serverProtos.json +1 -0
  245. package/template/game-server/config/servers.json +12 -0
  246. package/template/game-server/copy.js +5 -0
  247. package/template/game-server/package.json +28 -0
  248. package/template/game-server/preload.ts +40 -0
  249. package/template/game-server/tsconfig.json +36 -0
  250. package/template/npm-install.bat +4 -0
  251. package/template/npm-install.sh +5 -0
  252. package/template/readme.md +31 -0
  253. package/template/shared/server.crt +15 -0
  254. package/template/shared/server.key +15 -0
  255. package/template/web-server/app.js +25 -0
  256. package/template/web-server/app.js.https +36 -0
  257. package/template/web-server/bin/component.bat +1 -0
  258. package/template/web-server/bin/component.sh +1 -0
  259. package/template/web-server/package.json +10 -0
  260. package/template/web-server/public/css/base.css +76 -0
  261. package/template/web-server/public/image/logo.png +0 -0
  262. package/template/web-server/public/image/sp.png +0 -0
  263. package/template/web-server/public/index.html +57 -0
  264. package/template/web-server/public/index.html.sio +58 -0
  265. package/template/web-server/public/js/lib/build/build.js +1730 -0
  266. package/template/web-server/public/js/lib/build/build.js.wss +1721 -0
  267. package/template/web-server/public/js/lib/component.json +6 -0
  268. package/template/web-server/public/js/lib/local/boot/component.json +11 -0
  269. package/template/web-server/public/js/lib/local/boot/index.js +11 -0
  270. package/template/web-server/public/js/lib/meloclient.js +456 -0
  271. package/template/web-server/public/js/lib/meloclient.js.wss +456 -0
  272. package/template/web-server/public/js/lib/socket.io.js +3 -0
  273. package/test/application.ts +607 -0
  274. package/test/filters/handler/serial.ts +47 -0
  275. package/test/filters/handler/time.ts +47 -0
  276. package/test/filters/handler/timeout.ts +46 -0
  277. package/test/filters/handler/toobusy.ts +59 -0
  278. package/test/filters/rpc/rpcLog.ts +20 -0
  279. package/test/filters/rpc/toobusy.ts +40 -0
  280. package/test/manager/mockChannelManager.ts +92 -0
  281. package/test/manager/taskManager.ts +78 -0
  282. package/test/mock-base/app/servers/other-file +0 -0
  283. package/test/mock-plugin/components/mockPlugin.ts +8 -0
  284. package/test/mock-plugin/events/mockEvent.ts +12 -0
  285. package/test/modules/console.ts +264 -0
  286. package/test/pomelo.ts +18 -0
  287. package/test/remote/channelRemote.ts +176 -0
  288. package/test/service/channel.ts +161 -0
  289. package/test/service/channelService.ts +243 -0
  290. package/test/service/connectionService.ts +142 -0
  291. package/test/service/filterService.ts +165 -0
  292. package/test/service/handlerService.ts +77 -0
  293. package/test/service/sessionService.ts +464 -0
  294. package/test/util/countDownLatch.ts +81 -0
  295. package/test/util/pathUtil.ts +122 -0
  296. package/test/util/utils.ts +165 -0
@@ -0,0 +1,234 @@
1
+ import * as cp from 'child_process';
2
+ import { getLogger } from '@bigtyphoon/melo-logger';
3
+ import * as util from 'util';
4
+ import * as utils from '../util/utils';
5
+ import * as Constants from '../util/constants';
6
+ import { ServerInfo } from '../util/constants';
7
+ import * as os from 'os';
8
+ import { melo } from '../melo';
9
+ import { Application } from '../application';
10
+ import * as path from 'path';
11
+
12
+ let logger = getLogger('melo', path.basename(__filename));
13
+
14
+
15
+ let cpus: { [serverId: string]: number } = {};
16
+ let env: string = Constants.RESERVED.ENV_DEV;
17
+
18
+ /**
19
+ * Run all servers
20
+ *
21
+ * @param {Object} app current application context
22
+ * @return {Void}
23
+ */
24
+ export function runServers(app: Application) {
25
+ let server, servers;
26
+ let condition = app.startId || app.type;
27
+ switch (condition) {
28
+ case Constants.RESERVED.MASTER:
29
+ break;
30
+ case Constants.RESERVED.ALL:
31
+ servers = app.getServersFromConfig();
32
+ for (let serverId in servers) {
33
+ run(app, servers[serverId]);
34
+ }
35
+ break;
36
+ default:
37
+ server = app.getServerFromConfig(condition);
38
+ if (!!server) {
39
+ run(app, server);
40
+ } else {
41
+ servers = app.get(Constants.RESERVED.SERVERS)[condition];
42
+ for (let i = 0; i < servers.length; i++) {
43
+ run(app, servers[i]);
44
+ }
45
+ }
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Run server
51
+ *
52
+ * @param {Object} app current application context
53
+ * @param {Object} server
54
+ * @return {Void}
55
+ */
56
+ export function run(app: Application, server: ServerInfo, cb ?: (err?: string | number) => void) {
57
+ env = app.get(Constants.RESERVED.ENV);
58
+ let cmd, key;
59
+ if (utils.isLocal(server.host)) {
60
+ let options: string[] = [];
61
+ if (!!server.args) {
62
+ if (typeof server.args === 'string') {
63
+ options.push(server.args.trim());
64
+ } else {
65
+ options = options.concat(server.args);
66
+ }
67
+ }
68
+ cmd = app.get(Constants.RESERVED.MAIN);
69
+ options.push(cmd);
70
+ options.push(util.format('env=%s', env));
71
+ for (key in server) {
72
+ if (key === Constants.RESERVED.CPU) {
73
+ cpus[server.id] = server[key];
74
+ }
75
+ options.push(util.format('%s=%s', key, (server as any)[key]));
76
+ }
77
+ localrun(process.execPath, null, options, cb);
78
+ } else {
79
+ cmd = util.format('cd "%s" && "%s"', app.getBase(), process.execPath);
80
+ let arg = server.args;
81
+ if (arg !== undefined) {
82
+ if (typeof arg === 'string') {
83
+ cmd += ' ' + arg.trim()
84
+ } else {
85
+ for (let v of arg) {
86
+ cmd += ' ' + v.trim()
87
+ }
88
+ }
89
+ }
90
+ cmd += util.format(' "%s" env=%s ', app.get(Constants.RESERVED.MAIN), env);
91
+ for (key in server) {
92
+ if (key === Constants.RESERVED.CPU) {
93
+ cpus[server.id] = server[key];
94
+ }
95
+ cmd += util.format(' %s=%s ', key, (server as any)[key]);
96
+ }
97
+ sshrun(cmd, server.host, cb);
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Bind process with cpu
103
+ *
104
+ * @param {String} sid server id
105
+ * @param {String} pid process id
106
+ * @param {String} host server host
107
+ * @return {Void}
108
+ */
109
+ export function bindCpu(app: Application, sid: string, pid: string, host: string) {
110
+ if (os.platform() === Constants.PLATFORM.LINUX && cpus[sid] !== undefined) {
111
+ if (utils.isLocal(host)) {
112
+ let options: string[] = [];
113
+ options.push('-pc');
114
+ options.push(String(cpus[sid]));
115
+ options.push(pid);
116
+ localrun(Constants.COMMAND.TASKSET, null, options);
117
+ } else {
118
+ let cmd = util.format('taskset -pc "%s" "%s"', cpus[sid], pid);
119
+ sshrun(cmd, host, null);
120
+ }
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Kill application in all servers
126
+ *
127
+ * @param {String} pids array of server's pid
128
+ * @param {String} serverIds array of serverId
129
+ */
130
+ export function kill(app: Application, pids: string[], servers: ServerInfo[]) {
131
+ let cmd;
132
+ for (let i = 0; i < servers.length; i++) {
133
+ let server = servers[i];
134
+ if (utils.isLocal(server.host)) {
135
+ let options: string[] = [];
136
+ if (os.platform() === Constants.PLATFORM.WIN) {
137
+ cmd = Constants.COMMAND.TASKKILL;
138
+ options.push('/pid');
139
+ options.push('/f');
140
+ } else {
141
+ cmd = Constants.COMMAND.KILL;
142
+ options.push(String(-9));
143
+ }
144
+ options.push(pids[i]);
145
+ localrun(cmd, null, options);
146
+ } else {
147
+ if (os.platform() === Constants.PLATFORM.WIN) {
148
+ cmd = util.format('taskkill /pid %s /f', pids[i]);
149
+ } else {
150
+ cmd = util.format('kill -9 %s', pids[i]);
151
+ }
152
+ sshrun(cmd, server.host);
153
+ }
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Use ssh to run command.
159
+ *
160
+ * @param {String} cmd command that would be executed in the remote server
161
+ * @param {String} host remote server host
162
+ * @param {Function} cb callback function
163
+ *
164
+ */
165
+ export function sshrun(cmd: string, host: string, cb ?: (err?: string | number) => void) {
166
+ let args = [];
167
+ args.push(host);
168
+ // melo masterha 命令下melo.app为undefined
169
+ if (melo.app) {
170
+ let ssh_params = melo.app.get(Constants.RESERVED.SSH_CONFIG_PARAMS);
171
+ if (!!ssh_params && Array.isArray(ssh_params)) {
172
+ args = args.concat(ssh_params);
173
+ }
174
+ }
175
+ args.push(cmd);
176
+
177
+ logger.info('Executing ' + cmd + ' on ' + host + ':22');
178
+ spawnProcess(Constants.COMMAND.SSH, host, args, cb);
179
+ return;
180
+ }
181
+
182
+ /**
183
+ * Run local command.
184
+ *
185
+ * @param {String} cmd
186
+ * @param {Callback} callback
187
+ *
188
+ */
189
+ export function localrun(cmd: string, host: string, options: string[], callback ?: (err?: string | number) => void) {
190
+ logger.info('Executing ' + cmd + ' ' + options + ' locally');
191
+ spawnProcess(cmd, host, options, callback);
192
+ }
193
+
194
+ /**
195
+ * Fork child process to run command.
196
+ *
197
+ * @param {String} command
198
+ * @param {Object} options
199
+ * @param {Callback} callback
200
+ *
201
+ */
202
+ let spawnProcess = function (command: string, host: string, options: string[], cb ?: (result: string | number) => void) {
203
+ let child = null;
204
+
205
+ if (env === Constants.RESERVED.ENV_DEV) {
206
+ child = cp.spawn(command, options);
207
+ let prefix = command === Constants.COMMAND.SSH ? '[' + host + '] ' : '';
208
+
209
+ child.stderr.on('data', function (chunk) {
210
+ let msg = chunk.toString();
211
+ process.stderr.write(msg);
212
+ if (!!cb) {
213
+ cb(msg);
214
+ }
215
+ });
216
+
217
+ child.stdout.on('data', function (chunk) {
218
+ let msg = prefix + chunk.toString();
219
+ process.stdout.write(msg);
220
+ });
221
+ } else {
222
+ child = cp.spawn(command, options, { detached: true, stdio: 'inherit' });
223
+ child.unref();
224
+ }
225
+
226
+ child.on('exit', function (code) {
227
+ if (code !== 0) {
228
+ logger.warn('child process exit with error, error code: %s, executed command: %s', code, command);
229
+ }
230
+ if (typeof cb === 'function') {
231
+ cb(code === 0 ? null : code);
232
+ }
233
+ });
234
+ };
@@ -0,0 +1,135 @@
1
+ import { getLogger } from '@bigtyphoon/melo-logger';
2
+ import * as utils from '../util/utils';
3
+ import * as Constants from '../util/constants';
4
+ import * as countDownLatch from '../util/countDownLatch';
5
+ import { EventEmitter } from 'events';
6
+ import * as util from 'util';
7
+ import { Application } from '../application';
8
+ import { ServerInfo } from '../util/constants';
9
+ import { ConsoleService, MasterAgent } from '@bigtyphoon/melo-admin';
10
+ import * as path from 'path';
11
+ let logger = getLogger('melo', path.basename(__filename));
12
+
13
+
14
+ export class Watchdog extends EventEmitter {
15
+
16
+ isStarted = false;
17
+ servers: {[serverId: string]: ServerInfo} = {};
18
+ _listeners: {[serverId: string]: number} = {};
19
+ count: number;
20
+ constructor(private app: Application, private service: ConsoleService) {
21
+ super();
22
+
23
+ this.count = Object.keys(app.getServersFromConfig()).length;
24
+
25
+ }
26
+
27
+
28
+ addServer(server: ServerInfo) {
29
+ if (!server) {
30
+ return;
31
+ }
32
+ this.servers[server.id] = server;
33
+ this.notify({ action: 'addServer', server: server });
34
+ }
35
+
36
+ removeServer(id: string) {
37
+ if (!id) {
38
+ return;
39
+ }
40
+ this.unsubscribe(id);
41
+ delete this.servers[id];
42
+ this.notify({ action: 'removeServer', id: id });
43
+ }
44
+
45
+ reconnectServer(server: ServerInfo) {
46
+ let self = this;
47
+ if (!server) {
48
+ return;
49
+ }
50
+ if (!this.servers[server.id]) {
51
+ this.servers[server.id] = server;
52
+ }
53
+ // replace server in reconnect server
54
+ this.notifyById(server.id, { action: 'replaceServer', servers: self.servers });
55
+ // notify other server to add server
56
+ this.notify({ action: 'addServer', server: server });
57
+ // add server in listener
58
+ this.subscribe(server.id);
59
+ }
60
+
61
+ subscribe(id: string) {
62
+ this._listeners[id] = 1;
63
+ }
64
+
65
+ unsubscribe(id: string) {
66
+ delete this._listeners[id];
67
+ }
68
+
69
+ query() {
70
+ return this.servers;
71
+ }
72
+
73
+ record(id: string) {
74
+ if (!this.isStarted && --this.count < 0) {
75
+ let usedTime = Date.now() - this.app.startTime;
76
+ this.notify({ action: 'startOver' });
77
+ this.isStarted = true;
78
+ logger.warn('all servers startup in %s ms', usedTime);
79
+ }
80
+ }
81
+
82
+ notifyById(id: string, msg: any) {
83
+ (this.service.agent as MasterAgent).request(id, Constants.KEYWORDS.MONITOR_WATCHER, msg, function (signal: any) {
84
+ if (signal !== Constants.SIGNAL.OK) {
85
+ logger.error('master watchdog fail to notify to monitor, id: %s, msg: %j', id, msg);
86
+ } else {
87
+ logger.debug('master watchdog notify to monitor success, id: %s, msg: %j', id, msg);
88
+ }
89
+ });
90
+ }
91
+
92
+ notify(msg: any) {
93
+ let _listeners = this._listeners;
94
+ let success = true;
95
+ let fails: string[] = [];
96
+ let timeouts: string[] = [];
97
+ let requests: {[key: string]: number} = {};
98
+ let count = Object.keys(_listeners).length;
99
+ if (count === 0) {
100
+ logger.warn('master watchdog _listeners is none, msg: %j', msg);
101
+ return;
102
+ }
103
+ let latch = countDownLatch.createCountDownLatch(count, { timeout: Constants.TIME.TIME_WAIT_COUNTDOWN }, function (isTimeout) {
104
+ if (!!isTimeout) {
105
+ for (let key in requests) {
106
+ if (!requests[key]) {
107
+ timeouts.push(key);
108
+ }
109
+ }
110
+ logger.error('master watchdog request timeout message: %j, timeouts: %j, fails: %j', msg, timeouts, fails);
111
+ }
112
+ if (!success) {
113
+ logger.error('master watchdog request fail message: %j, fails: %j', msg, fails);
114
+ }
115
+ });
116
+
117
+ let moduleRequest = function (self: Watchdog, id: string) {
118
+ return (function () {
119
+ (self.service.agent as MasterAgent).request(id, Constants.KEYWORDS.MONITOR_WATCHER, msg, function (signal: any) {
120
+ if (signal !== Constants.SIGNAL.OK) {
121
+ fails.push(id);
122
+ success = false;
123
+ }
124
+ requests[id] = 1;
125
+ latch.done();
126
+ });
127
+ })();
128
+ };
129
+
130
+ for (let id in _listeners) {
131
+ requests[id] = 0;
132
+ moduleRequest(this, id);
133
+ }
134
+ }
135
+ }
package/lib/melo.ts ADDED
@@ -0,0 +1,152 @@
1
+ /*!
2
+ * Melo
3
+ * Copyright(c) 2012 xiechengchao <xiecc@163.com>
4
+ * MIT Licensed
5
+ */
6
+
7
+ /**
8
+ * Module dependencies.
9
+ */
10
+ import * as fs from 'fs';
11
+ import * as path from 'path';
12
+ import { Application, ApplicationOptions } from './application';
13
+ import { isFunction } from 'util';
14
+ import { BackendSession } from './common/service/backendSessionService';
15
+ import { HybridConnector } from './connectors/hybridconnector';
16
+ import { UDPConnector } from './connectors/udpconnector';
17
+ import { MQTTConnector } from './connectors/mqttconnector';
18
+ import { SIOConnector } from './connectors/sioconnector';
19
+ import { DirectPushScheduler } from './pushSchedulers/direct';
20
+ import { BufferPushScheduler } from './pushSchedulers/buffer';
21
+ import { ChannelService } from './common/service/channelService';
22
+
23
+ import { ConnectionComponent } from './components/connection';
24
+ import { ConnectorComponent } from './components/connector';
25
+ import { DictionaryComponent } from './components/dictionary';
26
+ import { MasterComponent } from './components/master';
27
+ import { MonitorComponent } from './components/monitor';
28
+ import { ProtobufComponent } from './components/protobuf';
29
+ import { ProxyComponent } from './components/proxy';
30
+ import { PushSchedulerComponent } from './components/pushScheduler';
31
+ import { RemoteComponent } from './components/remote';
32
+ import { ServerComponent } from './components/server';
33
+ import {SessionComponent } from './components/session';
34
+
35
+
36
+ import { RpcToobusyFilter } from './filters/rpc/toobusy';
37
+ import { RpcLogFilter } from './filters/rpc/rpcLog';
38
+ import { ToobusyFilter } from './filters/handler/toobusy';
39
+ import { TimeFilter } from './filters/handler/time';
40
+ import { SerialFilter } from './filters/handler/serial';
41
+ import { TimeoutFilter } from './filters/handler/timeout';
42
+ let Package = require('../../package');
43
+
44
+ import {default as events} from './util/events';
45
+ import { BackendSessionComponent } from './components/backendSession';
46
+ import { ChannelComponent } from './components/channel';
47
+ /**
48
+ * Expose `createApplication()`.
49
+ *
50
+ * @module
51
+ */
52
+
53
+ export class Melo {
54
+ private _app: Application;
55
+ /**
56
+ * Framework version.
57
+ */
58
+
59
+ version = Package.version;
60
+
61
+ /**
62
+ * Event definitions that would be emitted by app.event
63
+ */
64
+ events = events;
65
+
66
+ /**
67
+ * auto loaded components
68
+ */
69
+ components =
70
+ {
71
+ backendSession : BackendSessionComponent,
72
+ channel : ChannelComponent,
73
+ connection : ConnectionComponent,
74
+ connector : ConnectorComponent,
75
+ dictionary : DictionaryComponent,
76
+ master : MasterComponent,
77
+ monitor : MonitorComponent,
78
+ protobuf : ProtobufComponent,
79
+ proxy : ProxyComponent,
80
+ pushScheduler : PushSchedulerComponent,
81
+ remote : RemoteComponent,
82
+ server : ServerComponent,
83
+ session : SessionComponent,
84
+ };
85
+
86
+ /**
87
+ * auto loaded filters
88
+ */
89
+ filters =
90
+ {
91
+ serial : SerialFilter,
92
+ time : TimeFilter,
93
+ timeout : TimeoutFilter,
94
+ toobusy : ToobusyFilter,
95
+ };
96
+
97
+ /**
98
+ * auto loaded rpc filters
99
+ */
100
+ rpcFilters =
101
+ {
102
+ rpcLog : RpcLogFilter,
103
+ toobusy : RpcToobusyFilter,
104
+ };
105
+
106
+
107
+ /**
108
+ * connectors
109
+ */
110
+ connectors =
111
+ {
112
+ sioconnector : SIOConnector,
113
+ hybridconnector : HybridConnector,
114
+ udpconnector : UDPConnector,
115
+ mqttconnector : MQTTConnector,
116
+ };
117
+
118
+ /**
119
+ * pushSchedulers
120
+ */
121
+ pushSchedulers =
122
+ {
123
+ direct : DirectPushScheduler,
124
+ buffer : BufferPushScheduler,
125
+ };
126
+
127
+ constructor() {
128
+ }
129
+
130
+ /**
131
+ * Create an melo application.
132
+ *
133
+ * @return {Application}
134
+ * @memberOf Melo
135
+ * @api public
136
+ */
137
+ createApp(opts ?: ApplicationOptions) {
138
+ let app = new Application();
139
+ app.init(opts);
140
+ this._app = app;
141
+ return app;
142
+ }
143
+
144
+ /**
145
+ * Get application
146
+ */
147
+ get app() {
148
+ return this._app;
149
+ }
150
+ }
151
+
152
+ export let melo = new Melo();