@colyseus/tools 0.15.0-preview.5 → 0.15.0-preview.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 (2) hide show
  1. package/package.json +5 -2
  2. package/post-deploy.js +73 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/tools",
3
- "version": "0.15.0-preview.5",
3
+ "version": "0.15.0-preview.6",
4
4
  "description": "Colyseus Tools for Production",
5
5
  "input": "./src/index.ts",
6
6
  "main": "./build/index.js",
@@ -9,6 +9,9 @@
9
9
  "scripts": {
10
10
  "start": "ts-node-dev example/app.ts"
11
11
  },
12
+ "bin": {
13
+ "colyseus-post-deploy": "post-deploy.js"
14
+ },
12
15
  "repository": {
13
16
  "type": "git",
14
17
  "url": "git+ssh://git@github.com/colyseus/colyseus.git"
@@ -43,5 +46,5 @@
43
46
  "publishConfig": {
44
47
  "access": "public"
45
48
  },
46
- "gitHead": "56064c155c934ed45a1f4a5439ad4a5ca409623a"
49
+ "gitHead": "780c535e19ea47713c7eccc5d64de407bcfb65ce"
47
50
  }
package/post-deploy.js ADDED
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env node
2
+ const pm2 = require('pm2');
3
+ const os = require('os');
4
+ const fs = require('fs');
5
+
6
+ const opts = { env: process.env.NODE_ENV || "production" };
7
+ const maxCPU = os.cpus().length;
8
+
9
+ const NGINX_CONFIG_FILE = '/etc/nginx/colyseus_servers.conf';
10
+
11
+ pm2.list(function(err, apps) {
12
+ bailOnErr(err);
13
+
14
+ if (apps.length === 0) {
15
+ // first deploy
16
+ pm2.start('ecosystem.config.js', {...opts}, updateAndReloadNginx);
17
+
18
+ } else {
19
+ // reload existing apps
20
+ pm2.reload('ecosystem.config.js', {...opts}, function(err, apps) {
21
+ bailOnErr(err);
22
+
23
+ const name = apps[0].name;
24
+
25
+ // scale app to use all CPUs available
26
+ if (apps.length !== maxCPU) {
27
+ pm2.scale(name, maxCPU, updateAndReloadNginx);
28
+
29
+ } else {
30
+ updateAndReloadNginx();
31
+ }
32
+ });
33
+ }
34
+ });
35
+
36
+ function updateAndReloadNginx() {
37
+ pm2.list(function(err, apps) {
38
+ bailOnErr(err);
39
+
40
+ const port = 2567;
41
+ const addresses = [];
42
+
43
+ apps.forEach(function(app) {
44
+ addresses.push(`127.0.0.1:${ port + app.pm2_env.NODE_APP_INSTANCE }`);
45
+ });
46
+
47
+ fs.writeFileSync(NGINX_CONFIG_FILE, addresses.map(address => `server ${address};`).join("\n"));
48
+
49
+ // "pm2 save"
50
+ pm2.dump(function(err, ret) {
51
+ bailOnErr(err);
52
+ });
53
+ });
54
+
55
+ //
56
+ // If you are self-hosting and reading this file, consider using the
57
+ // following in your self-hosted environment:
58
+ //
59
+ // #!/bin/bash
60
+ // # Requires fswatch (`apt install fswatch`)
61
+ // # Reload NGINX when colyseus_servers.conf changes
62
+ // fswatch /etc/nginx/colyseus_servers.conf -m poll_monitor --event=Updated | while read event
63
+ // do
64
+ // service nginx reload
65
+ // done
66
+ }
67
+
68
+ function bailOnErr(err) {
69
+ if (err) {
70
+ console.error(err);
71
+ process.exit(1);
72
+ }
73
+ }