@colyseus/tools 0.15.30 → 0.15.31

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/post-deploy.js +48 -18
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/tools",
3
- "version": "0.15.30",
3
+ "version": "0.15.31",
4
4
  "description": "Colyseus Tools for Production",
5
5
  "input": "./src/index.ts",
6
6
  "main": "./build/index.js",
@@ -41,8 +41,8 @@
41
41
  "cors": "^2.8.5",
42
42
  "dotenv": "^8.2.0",
43
43
  "express": "^4.16.2",
44
- "@colyseus/ws-transport": "^0.15.1",
45
- "@colyseus/core": "^0.15.19"
44
+ "@colyseus/core": "^0.15.23",
45
+ "@colyseus/ws-transport": "^0.15.1"
46
46
  },
47
47
  "publishConfig": {
48
48
  "access": "public"
package/post-deploy.js CHANGED
@@ -7,8 +7,6 @@ const path = require('path');
7
7
  const opts = { env: process.env.NODE_ENV || "production" };
8
8
  const maxCPU = os.cpus().length;
9
9
 
10
- const NGINX_SERVERS_CONFIG_FILE = '/etc/nginx/colyseus_servers.conf';
11
-
12
10
  // allow deploying from other path as root.
13
11
  if (process.env.npm_config_local_prefix) {
14
12
  process.chdir(process.env.npm_config_local_prefix);
@@ -31,7 +29,7 @@ pm2.list(function(err, apps) {
31
29
 
32
30
  if (apps.length === 0) {
33
31
  // first deploy
34
- pm2.start(CONFIG_FILE, {...opts}, updateAndReloadNginx);
32
+ pm2.start(CONFIG_FILE, {...opts}, onAppRunning);
35
33
 
36
34
  } else {
37
35
  // reload existing apps
@@ -42,17 +40,59 @@ pm2.list(function(err, apps) {
42
40
 
43
41
  // scale app to use all CPUs available
44
42
  if (apps.length !== maxCPU) {
45
- pm2.scale(name, maxCPU, updateAndReloadNginx);
43
+ pm2.scale(name, maxCPU, onAppRunning);
46
44
 
47
45
  } else {
48
- updateAndReloadNginx();
46
+ onAppRunning();
49
47
  }
50
48
  });
51
49
  }
52
50
  });
53
51
 
52
+ function onAppRunning() {
53
+ updateColyseusBootService();
54
+ updateAndReloadNginx();
55
+ }
56
+
57
+ function updateColyseusBootService() {
58
+ //
59
+ // Update colyseus-boot.service to use the correct paths for the application
60
+ //
61
+
62
+ const COLYSEUS_CLOUD_BOOT_SERVICE = '/etc/systemd/system/colyseus-boot.service';
63
+
64
+ // ignore if no boot service found
65
+ if (!fs.existsSync(COLYSEUS_CLOUD_BOOT_SERVICE)) { return; }
66
+
67
+ const workingDirectory = pm2.cwd;
68
+ const execStart = __filename;
69
+
70
+ const contents = fs.readFileSync(COLYSEUS_CLOUD_BOOT_SERVICE, 'utf8');
71
+
72
+ fs.writeFileSync(COLYSEUS_CLOUD_BOOT_SERVICE, contents
73
+ .replace(/WorkingDirectory=(.*)/, `WorkingDirectory=${workingDirectory}`)
74
+ .replace(/ExecStart=(.*)/, `ExecStart=${execStart}`));
75
+ }
76
+
54
77
  function updateAndReloadNginx() {
78
+ //
79
+ // If you are self-hosting and reading this file, consider using the
80
+ // following in your self-hosted environment:
81
+ //
82
+ // #!/bin/bash
83
+ // # Requires fswatch (`apt install fswatch`)
84
+ // # Reload NGINX when colyseus_servers.conf changes
85
+ // fswatch /etc/nginx/colyseus_servers.conf -m poll_monitor --event=Updated | while read event
86
+ // do
87
+ // service nginx reload
88
+ // done
89
+
90
+ const NGINX_SERVERS_CONFIG_FILE = '/etc/nginx/colyseus_servers.conf';
91
+
55
92
  pm2.list(function(err, apps) {
93
+ if (apps.length === 0) {
94
+ err = "no apps running.";
95
+ }
56
96
  bailOnErr(err);
57
97
 
58
98
  const port = 2567;
@@ -62,28 +102,18 @@ function updateAndReloadNginx() {
62
102
  addresses.push(`unix:/run/colyseus/${ port + app.pm2_env.NODE_APP_INSTANCE }.sock`);
63
103
  });
64
104
 
105
+ // write NGINX config
65
106
  fs.writeFileSync(NGINX_SERVERS_CONFIG_FILE, addresses.map(address => `server ${address};`).join("\n"), bailOnErr);
66
107
 
67
108
  // "pm2 save"
68
- pm2.dump(function(err, ret) {
109
+ pm2.dump(function (err, ret) {
69
110
  bailOnErr(err);
70
111
 
71
112
  // exit with success!
72
113
  process.exit();
73
114
  });
74
- });
75
115
 
76
- //
77
- // If you are self-hosting and reading this file, consider using the
78
- // following in your self-hosted environment:
79
- //
80
- // #!/bin/bash
81
- // # Requires fswatch (`apt install fswatch`)
82
- // # Reload NGINX when colyseus_servers.conf changes
83
- // fswatch /etc/nginx/colyseus_servers.conf -m poll_monitor --event=Updated | while read event
84
- // do
85
- // service nginx reload
86
- // done
116
+ });
87
117
  }
88
118
 
89
119
  function bailOnErr(err) {