Skip to content

Commit 2efe33d

Browse files
committed
Auto merge of #1900 - kzys:use-fastboot, r=carols10cents
Enable Fastboot with USE_FASTBOOT environment variable set to 1 This PR enables Fastboot on `/policies` like before, but only if USE_FASTBOOT is defined.
2 parents ea248e1 + 6e373b6 commit 2efe33d

File tree

9 files changed

+120
-7
lines changed

9 files changed

+120
-7
lines changed

Procfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
release: bin/diesel migration run
2-
web: bin/start-nginx ./target/release/server
2+
web: ./script/start-web.sh
33
background_worker: ./target/release/background-worker

config/environment.js

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ module.exports = function(environment) {
2222
// Here you can pass flags/options to your application instance
2323
// when it is created
2424
},
25+
26+
fastboot: {
27+
hostWhitelist: ['crates.io', /^localhost:\d+$/, /\.herokuapp\.com$/],
28+
},
2529
};
2630

2731
if (environment === 'development') {

config/nginx.conf.erb

+7
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ http {
6464
proxy_pass http://app_server;
6565
}
6666

67+
<% if ENV['USE_FASTBOOT'] %>
68+
# Just in case, only forward "/policies" to Ember for a moment
69+
location = /policies {
70+
proxy_pass http://localhost:9000;
71+
}
72+
<% end %>
73+
6774
location ~ ^/api/v./crates/new$ {
6875
proxy_pass http://app_server;
6976

fastboot.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* eslint-disable no-console */
2+
/* eslint-env node */
3+
4+
'use strict';
5+
6+
const fs = require('fs');
7+
const os = require('os');
8+
const FastBootAppServer = require('fastboot-app-server');
9+
10+
// because fastboot-app-server uses cluster, but it might change in future
11+
const cluster = require('cluster');
12+
13+
class LoggerWithoutTimestamp {
14+
constructor() {
15+
this.prefix = cluster.isMaster ? 'master' : 'worker';
16+
}
17+
writeLine() {
18+
this._write('info', Array.prototype.slice.apply(arguments));
19+
}
20+
21+
writeError() {
22+
this._write('error', Array.prototype.slice.apply(arguments));
23+
}
24+
25+
_write(level, args) {
26+
args[0] = `[${level}][${this.prefix}] ${args[0]}`;
27+
console.log.apply(console, args);
28+
}
29+
}
30+
31+
function writeAppInitializedWhenReady(logger) {
32+
let timeout;
33+
34+
timeout = setInterval(function() {
35+
logger.writeLine('waiting backend');
36+
if (fs.existsSync('/tmp/backend-initialized')) {
37+
logger.writeLine('backend is up. let heroku know the app is ready');
38+
fs.writeFileSync('/tmp/app-initialized', 'hello');
39+
clearInterval(timeout);
40+
} else {
41+
logger.writeLine('backend is still not up');
42+
}
43+
}, 1000);
44+
}
45+
46+
var logger = new LoggerWithoutTimestamp();
47+
48+
logger.writeLine(`${os.cpus().length} cores available`);
49+
50+
let workerCount = process.env.WEB_CONCURRENCY || 1;
51+
52+
let server = new FastBootAppServer({
53+
distPath: 'dist',
54+
port: 9000,
55+
ui: logger,
56+
workerCount: workerCount,
57+
});
58+
59+
if (!cluster.isWorker) {
60+
writeAppInitializedWhenReady(logger);
61+
}
62+
63+
server.start();

fastboot/initializers/ajax.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default {
2+
name: 'ajax-service',
3+
initialize() {
4+
// This is to override Fastboot's initializer which prevents ember-fetch from working
5+
// https://github.com/ember-fastboot/ember-cli-fastboot/blob/master/fastboot/initializers/ajax.js
6+
// https://github.com/ember-cli/ember-fetch#ajax-service
7+
},
8+
};

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
"lint:js": "eslint . --cache",
2222
"lint:deps": "ember dependency-lint",
2323
"prettier": "prettier --write '{app,tests,mirage}/**/*.js'",
24-
"start": "FASTBOOT_DISABLED=true ember serve",
25-
"start:live": "FASTBOOT_DISABLED=true ember serve --proxy https://crates.io",
26-
"start:local": "FASTBOOT_DISABLED=true ember serve --proxy http://127.0.0.1:8888",
27-
"start:staging": "FASTBOOT_DISABLED=true ember serve --proxy https://staging-crates-io.herokuapp.com",
24+
"start": "./script/ember.sh serve",
25+
"start:live": "./script/ember.sh serve --proxy https://crates.io",
26+
"start:local": "./script/ember.sh serve --proxy http://127.0.0.1:8888",
27+
"start:staging": "./script/ember.sh serve --proxy https://staging-crates-io.herokuapp.com",
2828
"test": "ember exam --split=2 --parallel",
2929
"test-coverage": "COVERAGE=true npm run test && ember coverage-merge && rm -rf coverage_* coverage/coverage-summary.json && nyc report"
3030
},

script/ember.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#! /bin/sh
2+
set -ue
3+
4+
export FASTBOOT_DISABLED
5+
6+
if [ "${USE_FASTBOOT:-0}" = '1' ]; then
7+
unset FASTBOOT_DISABLED
8+
else
9+
FASTBOOT_DISABLED=1
10+
fi
11+
12+
ember "$@"

script/start-web.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#! /bin/bash
2+
set -ue
3+
4+
if [[ "${USE_FASTBOOT:-0}" = 1 ]]; then
5+
export USE_FASTBOOT=1
6+
node --optimize_for_size --max_old_space_size=200 fastboot.js &
7+
bin/start-nginx ./target/release/server &
8+
wait -n
9+
else
10+
unset USE_FASTBOOT
11+
bin/start-nginx ./target/release/server
12+
fi

src/bin/server.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ fn main() {
3939
boot::categories::sync(categories_toml).unwrap();
4040

4141
let heroku = dotenv::var("HEROKU").is_ok();
42+
let fastboot = dotenv::var("USE_FASTBOOT").is_ok();
43+
4244
let port = if heroku {
4345
8888
4446
} else {
@@ -102,8 +104,13 @@ fn main() {
102104
// Creating this file tells heroku to tell nginx that the application is ready
103105
// to receive traffic.
104106
if heroku {
105-
println!("Writing to /tmp/app-initialized");
106-
File::create("/tmp/app-initialized").unwrap();
107+
let path = if fastboot {
108+
"/tmp/backend-initialized"
109+
} else {
110+
"/tmp/app-initialized"
111+
};
112+
println!("Writing to {}", path);
113+
File::create(path).unwrap();
107114
}
108115

109116
// Block the main thread until the server has shutdown

0 commit comments

Comments
 (0)