Skip to content

Commit

Permalink
Refactor server
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmerfield committed Jan 24, 2025
1 parent 7040830 commit 8f08b30
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 29 deletions.
11 changes: 4 additions & 7 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ if (cluster.isMaster) {
const clfdate = require("helper/clfdate");
const notify = require("helper/systemd-notify");

const NUMBER_OF_CORES = require("os").cpus().length;

const NUMBER_OF_WORKERS =
NUMBER_OF_CORES > 4
? Math.round(NUMBER_OF_CORES / 2)
: 2;
// we used to fork multiple workers, but now we only fork one
// since we run multiple docker containers instead
// todo: remove this code
const NUMBER_OF_WORKERS = 1;

const publishScheduledEntries = require("./scheduler/publish-scheduled-entries");

Expand All @@ -24,7 +22,6 @@ if (cluster.isMaster) {
// Write the master process PID so we can signal it
fs.outputFileSync(config.pidfile, process.pid.toString(), "utf-8");

// Fork workers based on how many CPUs are available
for (let i = 0; i < NUMBER_OF_WORKERS; i++) {
cluster.fork();
}
Expand Down
1 change: 1 addition & 0 deletions app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ if (config.webhooks.server_host) {
server.use(vhost(config.webhooks.server_host, require("./clients/webhooks")));
}

console.log(clfdate(), "Setting up CDN on", "cdn." + config.host);
// CDN server
server.use(vhost("cdn." + config.host, require("./cdn")));

Expand Down
2 changes: 1 addition & 1 deletion config/openresty/build-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const locals = {
blot_directory: config.blot_directory,
disable_http2: process.env.DISABLE_HTTP2,
node_ip: NODE_SERVER_IP,
node_port: config.port,
node_port: "8088",

// used in production by the node application container running inside docker
// to communicate with the openresty cache purge endpoint on localhost
Expand Down
15 changes: 6 additions & 9 deletions config/openresty/conf/http.conf
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,19 @@ proxy_cache_path {{{cache_directory}}} levels=1:2 keys_zone=PROXY_CACHE:30m in
lua_shared_dict cacher_dictionary 75m;

upstream blot_node {
server {{node_ip}}:{{node_port}};
# server {{node_ip}}:{{node_port}};
server 127.0.0.1:8088;
server 127.0.0.1:8089;

# Activates the cache for connections to upstream servers.
keepalive 64;
}

# Define the upstream servers for blue-green deployments
upstream blot_node_containerized {
# Blue container
server 127.0.0.1:8088 max_fails=3 fail_timeout=5s;
server 127.0.0.1:8088;

# Green container
server 127.0.0.1:8089 max_fails=3 fail_timeout=5s;

# Enables keepalive connections for upstream servers
keepalive 64;
# Activates the cache for connections to upstream servers.
keepalive 64;
}

# we want to detect if the request is from a preview subdomain of blot.im
Expand Down
14 changes: 3 additions & 11 deletions config/openresty/conf/reverse-proxy.conf
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
add_header 'Blot-Server' '{{server_label}}' always;
add_header 'Blot-Upstream' $upstream_server always;
add_header 'Blot-Upstream-Addr' $upstream_addr always;

set $upstream_server blot_node;

if ($remote_addr = '79.35.122.153') {
set $upstream_server blot_node_containerized;
}
# if ($remote_addr = '79.35.122.153') {
# set $upstream_server blot_node_containerized;
# }

proxy_pass http://$upstream_server;
proxy_http_version 1.1;

# Connection timeout settings
proxy_connect_timeout 5s;
proxy_send_timeout 10s;

# Retry the next upstream server on failure
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Expand Down
2 changes: 1 addition & 1 deletion scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ DOCKER_RUN_COMMAND="docker run --pull=always -d \
--env-file /etc/blot/secrets.env \
-v /var/www/blot/data:/usr/src/app/data \
--restart unless-stopped \
--memory=1g --cpus=1 \
--memory=1.5g --cpus=1 \
ghcr.io/davidmerfield/blot:$GIT_COMMIT_HASH"

# Configurable health check timeout
Expand Down

0 comments on commit 8f08b30

Please sign in to comment.