@colyseus/tools 0.16.14 → 0.16.15

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 (2) hide show
  1. package/package.json +3 -3
  2. package/report-stats.js +56 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/tools",
3
- "version": "0.16.14",
3
+ "version": "0.16.15",
4
4
  "description": "Simplify the development and production settings for your Colyseus project.",
5
5
  "input": "./src/index.ts",
6
6
  "main": "./build/index.js",
@@ -50,11 +50,11 @@
50
50
  "@types/cors": "^2.8.10",
51
51
  "@types/dotenv": "^8.2.0",
52
52
  "uwebsockets-express": "^1.1.10",
53
+ "@colyseus/core": "^0.16.24",
54
+ "@colyseus/redis-driver": "^0.16.1",
53
55
  "@colyseus/ws-transport": "^0.16.5",
54
56
  "@colyseus/redis-presence": "^0.16.4",
55
57
  "@colyseus/bun-websockets": "^0.16.5",
56
- "@colyseus/redis-driver": "^0.16.1",
57
- "@colyseus/core": "^0.16.21",
58
58
  "@colyseus/uwebsockets-transport": "^0.16.10"
59
59
  },
60
60
  "peerDependencies": {
package/report-stats.js CHANGED
@@ -3,12 +3,18 @@
3
3
  const fs = require('fs');
4
4
  const net = require('net');
5
5
  const pm2 = require('pm2');
6
+ const dotenv = require('dotenv');
6
7
 
7
8
  const COLYSEUS_CLOUD_URL = `${process.env.ENDPOINT}/vultr/stats`;
8
9
 
9
10
  const FAILED_ATTEMPS_FILE = "/var/tmp/pm2-stats-attempts.txt";
10
11
  const FETCH_TIMEOUT = 30000;
11
12
 
13
+ // load environment variables (Colyseus Cloud environment variables)
14
+ if (process.env.APP_ROOT_PATH) {
15
+ dotenv.config({ path: `${process.env.APP_ROOT_PATH}/.env.cloud` });
16
+ }
17
+
12
18
  async function retryFailedAttempts() {
13
19
  /**
14
20
  * Retry cached failed attempts
@@ -127,6 +133,9 @@ pm2.Client.executeRemote('getMonitorData', {}, async function(err, list) {
127
133
  version: 1,
128
134
  ip,
129
135
  time: new Date(),
136
+ statuses: {
137
+ driver: await checkDriverIsAccessible()
138
+ },
130
139
  aggregate,
131
140
  apps,
132
141
  };
@@ -165,6 +174,11 @@ pm2.Client.executeRemote('getMonitorData', {}, async function(err, list) {
165
174
  }
166
175
  });
167
176
 
177
+ /**
178
+ * Check if a socket file is active
179
+ * @param {string} sockFilePath - The path to the socket file
180
+ * @returns {Promise<boolean>} true if the socket file is active, false otherwise
181
+ */
168
182
  function checkSocketIsActive(sockFilePath) {
169
183
  return new Promise((resolve, _) => {
170
184
  const client = net.createConnection({ path: sockFilePath, timeout: 5000 })
@@ -176,3 +190,45 @@ function checkSocketIsActive(sockFilePath) {
176
190
  .on('timeout', () => resolve(false));
177
191
  });
178
192
  }
193
+
194
+ /**
195
+ * Check if the driver is accessible
196
+ * @returns {Promise<boolean | string>} returns true if the driver is accessible, false otherwise or the error message
197
+ */
198
+ async function checkDriverIsAccessible() {
199
+ try {
200
+ if (process.env.REDIS_URI) {
201
+ const url = new URL(process.env.REDIS_URI);
202
+ return await isPortOpen({
203
+ host: url.hostname,
204
+ port: url.port
205
+ });
206
+ } else {
207
+ return true;
208
+ }
209
+ } catch (e) {
210
+ return e.message || false;
211
+ }
212
+ }
213
+
214
+ /**
215
+ * Check if a port is open
216
+ * @param {{ host: string, port: number, timeout?: number }} options - The options to check
217
+ * @returns {Promise<boolean | string>}
218
+ */
219
+ function isPortOpen({ host, port, timeout = 2000 }) {
220
+ return new Promise((resolve) => {
221
+ const socket = new net.Socket();
222
+ const onError = (value) => {
223
+ socket.destroy();
224
+ resolve(value || false);
225
+ };
226
+ socket.setTimeout(timeout);
227
+ socket.once("error", (e) => onError(e.message));
228
+ socket.once("timeout", () => onError("timeout"));
229
+ socket.connect(port, host, () => {
230
+ socket.end(); // immediately close
231
+ resolve(true);
232
+ });
233
+ });
234
+ }