1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/discourse_ynh.git synced 2024-09-03 18:26:18 +02:00

Use unicorn instead of puma, hence not needing a separate sidekiq service and drastically reducing memory footprint

Limit number of workers to 2 on ARM devices
See here: https://meta.discourse.org/t/does-discourse-require-2gb-to-work-lag-free/25278/8
This commit is contained in:
Jimmy Monin 2018-05-04 12:51:16 +02:00
parent 9bc9fb0ce0
commit f67986641f
8 changed files with 55 additions and 102 deletions

View file

@ -1,15 +0,0 @@
[Unit]
Description=__APP__ sidekiq service
After=network.target
[Service]
User=__APP__
Group=__APP__
WorkingDirectory=__FINALPATH__
Environment=RAILS_ENV=production
ExecStart=__RBENVROOT__/shims/bundle exec sidekiq -C config/sidekiq.yml
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target

View file

@ -56,7 +56,7 @@
proxy_set_header X-Request-Start "t=${msec}"; proxy_set_header X-Request-Start "t=${msec}";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https; proxy_set_header X-Forwarded-Proto https;
proxy_pass http://unix:__FINALPATH__/tmp/sockets/puma.sock; proxy_pass http://unix:__FINALPATH__/tmp/sockets/unicorn.sock;
break; break;
} }
@ -123,7 +123,7 @@
try_files $uri =404; try_files $uri =404;
} }
proxy_pass http://unix:__FINALPATH__/tmp/sockets/puma.sock; proxy_pass http://unix:__FINALPATH__/tmp/sockets/unicorn.sock;
break; break;
} }
@ -135,7 +135,7 @@
proxy_set_header X-Forwarded-Proto https; proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Sendfile-Type X-Accel-Redirect; proxy_set_header X-Sendfile-Type X-Accel-Redirect;
proxy_set_header X-Accel-Mapping __FINALPATH__/public/=/downloads/; proxy_set_header X-Accel-Mapping __FINALPATH__/public/=/downloads/;
proxy_pass http://unix:__FINALPATH__/tmp/sockets/puma.sock; proxy_pass http://unix:__FINALPATH__/tmp/sockets/unicorn.sock;
break; break;
} }
@ -158,7 +158,7 @@
# proxy_cache one; # proxy_cache one;
proxy_cache_valid 200 301 302 7d; proxy_cache_valid 200 301 302 7d;
proxy_cache_valid any 1m; proxy_cache_valid any 1m;
proxy_pass http://unix:__FINALPATH__/tmp/sockets/puma.sock; proxy_pass http://unix:__FINALPATH__/tmp/sockets/unicorn.sock;
break; break;
} }
@ -192,7 +192,7 @@
proxy_set_header X-Forwarded-Proto https; proxy_set_header X-Forwarded-Proto https;
proxy_http_version 1.1; proxy_http_version 1.1;
proxy_buffering off; proxy_buffering off;
proxy_pass http://unix:__FINALPATH__/tmp/sockets/puma.sock; proxy_pass http://unix:__FINALPATH__/tmp/sockets/unicorn.sock;
break; break;
} }
@ -212,5 +212,5 @@
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https; proxy_set_header X-Forwarded-Proto https;
proxy_pass http://unix:__FINALPATH__/tmp/sockets/puma.sock; proxy_pass http://unix:__FINALPATH__/tmp/sockets/unicorn.sock;
} }

View file

@ -1,11 +1,9 @@
[Unit] [Unit]
Description=__APP__ puma service Description=__APP__ service
Wants=postgresql.service Wants=postgresql.service
Wants=redis-server.service Wants=redis-server.service
After=redis-server.service After=redis-server.service
After=postgresql.service After=postgresql.service
After=discourse-sidekiq.service
Requires=discourse-sidekiq.service
[Service] [Service]
User=__APP__ User=__APP__
@ -13,9 +11,8 @@ Group=__APP__
WorkingDirectory=__FINALPATH__ WorkingDirectory=__FINALPATH__
Environment=__ADDITIONAL_ENV__ Environment=__ADDITIONAL_ENV__
Environment=RAILS_ENV=production Environment=RAILS_ENV=production
ExecStart=__RBENVROOT__/shims/bundle exec puma --config config/puma.rb -e production Environment=LD_PRELOAD=__LIBJEMALLOC__
ExecStop=__RBENVROOT__/shims/bundle exec pumactl stop ExecStart=__RBENVROOT__/shims/bundle exec unicorn --config config/unicorn.conf.rb -E production
RemainAfterExit=true
Restart=always Restart=always
RestartSec=10 RestartSec=10

View file

@ -48,17 +48,10 @@ ynh_backup "/etc/nginx/conf.d/$domain.d/$app.conf"
# BACKUP SYSTEMD # BACKUP SYSTEMD
#================================================= #=================================================
ynh_backup "/etc/systemd/system/$app-puma.service" ynh_backup "/etc/systemd/system/$app.service"
ynh_backup "/etc/systemd/system/$app-sidekiq.service"
#================================================= #=================================================
# BACKUP THE POSTGRESQL DATABASE # BACKUP THE POSTGRESQL DATABASE
#================================================= #=================================================
ynh_psql_dump_db "$db_name" > ${YNH_CWD}/db.sql ynh_psql_dump_db "$db_name" > ${YNH_CWD}/db.sql
#=================================================
# BACKUP LOG FILE
#=================================================
ynh_backup "/var/log/$app"

View file

@ -161,19 +161,14 @@ ynh_store_file_checksum "$ldap_config_file"
echo "svgo: false" > $final_path/.image_optim.yml echo "svgo: false" > $final_path/.image_optim.yml
#================================================= #=================================================
# SETUP PUMA, A RUBY SERVER # SETUP UNICORN, A RUBY SERVER
#================================================= #=================================================
puma_config_file="$final_path/config/puma.rb" unicorn_config_file="$final_path/config/unicorn.conf.rb"
# Set log files location # Use socket connection
ynh_replace_string "#{APP_ROOT}/log/puma" "/var/log/$app/puma" "$puma_config_file" ynh_replace_string 'listen (ENV\["UNICORN_PORT"\] || 3000).to_i' '# listen (ENV["UNICORN_PORT"] || 3000).to_i' "$unicorn_config_file"
# Set application absolute path ynh_replace_string '# listen "#{discourse_path}/tmp/sockets/unicorn.sock"' 'listen "#{discourse_path}/tmp/sockets/unicorn.sock"' "$unicorn_config_file"
ynh_replace_string "/home/discourse/discourse" "/var/www/$app" "$puma_config_file"
# Don't daemonize so we get logs in journalctl
ynh_replace_string "daemonize true" "daemonize false" "$puma_config_file"
# Don't preload threads to avoid warnings
ynh_replace_string "preload_app" "#preload_app" "$puma_config_file"
# Calculate and store the config file checksum # Calculate and store the config file checksum
ynh_store_file_checksum "$puma_config_file" ynh_store_file_checksum "$unicorn_config_file"
# Set a secret value # Set a secret value
cp ../conf/secrets.yml "$final_path/config/secrets.yml" cp ../conf/secrets.yml "$final_path/config/secrets.yml"
@ -206,11 +201,6 @@ if [ -n "$(uname -m | grep arm)" ] ; then
ln -s $(ldconfig -p | grep libpsl | awk 'END {print $NF}') libpsl.so) ln -s $(ldconfig -p | grep libpsl | awk 'END {print $NF}') libpsl.so)
fi fi
#=================================================
# SETUP SIDEKIQ
#=================================================
cp ../conf/sidekiq.yml "$final_path/config/sidekiq.yml"
#================================================= #=================================================
# PREPARE THE DATABASE # PREPARE THE DATABASE
#================================================= #=================================================
@ -271,21 +261,20 @@ patch -p1 < $YNH_CWD/../conf/ldap-auth-fix-subfolder.patch)
# SETUP SYSTEMD # SETUP SYSTEMD
#================================================= #=================================================
ynh_replace_string "__RBENVROOT__" "$RBENV_ROOT" "../conf/discourse-puma.service" ynh_replace_string "__RBENVROOT__" "$RBENV_ROOT" "../conf/systemd.service"
ynh_replace_string "__RBENVROOT__" "$RBENV_ROOT" "../conf/discourse-sidekiq.service"
# We assume for the moment that ARM devices are only dual core, so # We assume for the moment that ARM devices are only dual core, so
# we restrict the number of workers to 2 (the default is 4) # we restrict the number of workers to 2 (the default is 3)
if [ -n "$(uname -m | grep arm)" ] ; then if [ -n "$(uname -m | grep arm)" ] ; then
additional_env="NUM_WEBS=2" additional_env="UNICORN_WORKERS=2"
unicorn_workers=2
else else
additional_env="" additional_env=""
unicorn_workers=3
fi fi
ynh_replace_string "__ADDITIONAL_ENV__" "$additional_env" "../conf/discourse-puma.service" ynh_replace_string "__ADDITIONAL_ENV__" "$additional_env" "../conf/systemd.service"
ynh_add_systemd_config
ynh_add_systemd_config $app-puma discourse-puma.service
ynh_add_systemd_config $app-sidekiq discourse-sidekiq.service
#================================================= #=================================================
# GENERIC FINALIZATION # GENERIC FINALIZATION
@ -304,18 +293,14 @@ chown -R $app: $final_path
# SETUP LOGROTATE # SETUP LOGROTATE
#================================================= #=================================================
mkdir -p /var/log/$app
chown -R $app: /var/log/$app
# Use logrotate to manage application logfile(s) # Use logrotate to manage application logfile(s)
ynh_use_logrotate ynh_use_logrotate "$final_path/log/unicorn.stderr.log"
#================================================= #=================================================
# ADVERTISE SERVICE IN ADMIN PANEL # ADVERTISE SERVICE IN ADMIN PANEL
#================================================= #=================================================
yunohost service add $app-puma --log "/var/log/$app/puma.stderr.log" yunohost service add $app --log "$final_path/log/unicorn.stderr.log"
yunohost service add $app-sidekiq --log "/var/www/$app/log/production.log"
#================================================= #=================================================
# SETUP SSOWAT # SETUP SSOWAT
@ -334,9 +319,8 @@ fi
systemctl reload nginx systemctl reload nginx
#================================================= #=================================================
# START PUMA AND SIDEKIQ # START UNICORN
#================================================= #=================================================
# Wait for discourse-puma to be fully started # Wait for discourse to be fully started
# As discourse-sidekiq is a dependency, it is automatically started before ynh_check_starting "INFO -- : worker=$((unicorn_workers-1)) ready" "$final_path/log/unicorn.stderr.log" "120" "$app"
ynh_check_starting "Use Ctrl-C to stop" systemd "120" "$app-puma"

View file

@ -25,8 +25,7 @@ final_path=$(ynh_app_setting_get $app final_path)
# STOP AND REMOVE SERVICE # STOP AND REMOVE SERVICE
#================================================= #=================================================
ynh_remove_systemd_config discourse-puma ynh_remove_systemd_config
ynh_remove_systemd_config discourse-sidekiq
#================================================= #=================================================
# REMOVE SERVICE FROM ADMIN PANEL # REMOVE SERVICE FROM ADMIN PANEL
@ -35,8 +34,7 @@ ynh_remove_systemd_config discourse-sidekiq
if yunohost service status | grep -q $app if yunohost service status | grep -q $app
then then
echo "Remove $app services" echo "Remove $app services"
yunohost service remove $app-puma yunohost service remove $app
yunohost service remove $app-sidekiq
fi fi
#================================================= #=================================================

View file

@ -90,8 +90,7 @@ ynh_psql_execute_file_as_root ./db.sql "$db_name"
#================================================= #=================================================
systemctl daemon-reload systemctl daemon-reload
systemctl enable $app-puma.service systemctl enable $app.service
systemctl enable $app-sidekiq.service
#================================================= #=================================================
# SETUP LOGROTATE # SETUP LOGROTATE
@ -117,8 +116,7 @@ chown -R $app: /var/log/$app
# ADVERTISE SERVICE IN ADMIN PANEL # ADVERTISE SERVICE IN ADMIN PANEL
#================================================= #=================================================
yunohost service add $app-puma --log "/var/log/$app/puma.stderr.log" yunohost service add $app --log "$final_path/log/unicorn.stderr.log"
yunohost service add $app-sidekiq --log "/var/www/$app/log/production.log"
#================================================= #=================================================
# GENERIC FINALIZATION # GENERIC FINALIZATION
@ -126,8 +124,13 @@ yunohost service add $app-sidekiq --log "/var/www/$app/log/production.log"
# RELOAD NGINX AND DISCOURSE # RELOAD NGINX AND DISCOURSE
#================================================= #=================================================
# Wait for discourse-puma to be fully started if [ -n "$(uname -m | grep arm)" ] ; then
# As discourse-sidekiq is a dependency, it is automatically started before unicorn_workers=2
ynh_check_starting "Use Ctrl-C to stop" systemd "120" "$app-puma" else
unicorn_workers=3
fi
# Wait for discourse to be fully started
ynh_check_starting "INFO -- : worker=$((unicorn_workers-1)) ready" "$final_path/log/unicorn.stderr.log" "120" "$app"
systemctl reload nginx systemctl reload nginx

View file

@ -43,8 +43,7 @@ check_memory_requirements_upgrade
#================================================= #=================================================
# Stop services # Stop services
systemctl stop $app-sidekiq systemctl stop $app
systemctl stop $app-puma
# Backup the current version of the app # Backup the current version of the app
if [[ $(ynh_app_setting_get $app disable_backup_before_upgrade) != '1' ]] if [[ $(ynh_app_setting_get $app disable_backup_before_upgrade) != '1' ]]
@ -137,19 +136,14 @@ if ! ynh_is_upstream_up_to_date ; then
ynh_store_file_checksum "$ldap_config_file" ynh_store_file_checksum "$ldap_config_file"
#================================================= #=================================================
# SETUP PUMA, A RUBY SERVER # SETUP UNICORN, A RUBY SERVER
#================================================= #=================================================
puma_config_file="$final_path/config/puma.rb" unicorn_config_file="$final_path/config/unicorn.conf.rb"
# Set log files location # Use socket connection
ynh_replace_string "#{APP_ROOT}/log/puma" "/var/log/$app/puma" "$puma_config_file" ynh_replace_string 'listen (ENV\["UNICORN_PORT"\] || 3000).to_i' '# listen (ENV["UNICORN_PORT"] || 3000).to_i' "$unicorn_config_file"
# Set application absolute path ynh_replace_string '# listen "#{discourse_path}/tmp/sockets/unicorn.sock"' 'listen "#{discourse_path}/tmp/sockets/unicorn.sock"' "$unicorn_config_file"
ynh_replace_string "/home/discourse/discourse" "/var/www/$app" "$puma_config_file"
# Don't daemonize so we get logs in journalctl
ynh_replace_string "daemonize true" "daemonize false" "$puma_config_file"
# Don't preload threads to avoid warnings
ynh_replace_string "preload_app" "#preload_app" "$puma_config_file"
# Calculate and store the config file checksum # Calculate and store the config file checksum
ynh_store_file_checksum "$puma_config_file" ynh_store_file_checksum "$unicorn_config_file"
# Set a secret value # Set a secret value
cp ../conf/secrets.yml "$final_path/config/secrets.yml" cp ../conf/secrets.yml "$final_path/config/secrets.yml"
@ -177,11 +171,6 @@ if ! ynh_is_upstream_up_to_date ; then
ln -s $(ldconfig -p | grep libpsl | awk 'END {print $NF}') libpsl.so) ln -s $(ldconfig -p | grep libpsl | awk 'END {print $NF}') libpsl.so)
fi fi
#=================================================
# SETUP SIDEKIQ
#=================================================
cp ../conf/sidekiq.yml "$final_path/config/sidekiq.yml"
#================================================= #=================================================
# PREPARE THE DATABASE # PREPARE THE DATABASE
#================================================= #=================================================
@ -217,9 +206,13 @@ fi
systemctl reload nginx systemctl reload nginx
#================================================= #=================================================
# START PUMA AND SIDEKIQ # START UNICORN
#================================================= #=================================================
# Wait for discourse-puma to be fully started if [ -n "$(uname -m | grep arm)" ] ; then
# As discourse-sidekiq is a dependency, it is automatically started before unicorn_workers=2
ynh_check_starting "Use Ctrl-C to stop" systemd "120" "$app-puma" else
unicorn_workers=3
fi
# Wait for discourse to be fully started
ynh_check_starting "INFO -- : worker=$((unicorn_workers-1)) ready" "$final_path/log/unicorn.stderr.log" "120" "$app"