From bd14440386077d4254e9c24941f6577e8de34e36 Mon Sep 17 00:00:00 2001 From: Oneric Date: Tue, 24 Sep 2024 16:45:54 +0200 Subject: [PATCH 1/3] openrc: overhaul service file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - pass env vars the properâ„¢ way - write log to file - drop superfluous command_background - make settings easily overwritable via conf.d to avoid needing to edit the service file directly if e.g. Akkoma was installed to another location --- installation/init.d/akkoma | 56 ++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/installation/init.d/akkoma b/installation/init.d/akkoma index bd17516f2..cd7b70e7e 100755 --- a/installation/init.d/akkoma +++ b/installation/init.d/akkoma @@ -1,23 +1,40 @@ #!/sbin/openrc-run supervisor=supervise-daemon -command_user=akkoma:akkoma -command_background=1 -# Ask process to terminate within 30 seconds, otherwise kill it -retry="SIGTERM/30/SIGKILL/5" -pidfile="/var/run/akkoma.pid" -directory=/opt/akkoma -healthcheck_delay=60 -healthcheck_timer=30 no_new_privs="yes" +pidfile="/var/run/akkoma.pid" -: ${akkoma_port:-4000} +# Ask process first to terminate itself within 60s, otherwise kill it +retry="SIGTERM/60/SIGKILL/5" -# Needs OpenRC >= 0.42 -#respawn_max=0 -#respawn_delay=5 +# if you really want to use start-stop-daemon instead, +# also put the following in the config: +# command_background=1 + +# Adjust defaults as needed in /etc/conf.d/akkoma; +# no need to directly edit the service file +command_user="${command_user:-akkoma:akkoma}" +directory="${directory:-/var/lib/akkoma/akkoma}" +akkoma_port="${akkoma_port:-4000}" +# whether to allow connecting a remote exlixir shell to the running Akkoma instance +akkoma_console=${akkoma_console:-NO} + +output_log="${output_log:-/var/log/akkoma}" +error_log="${error_log:-/var/log/akkoma}" + +# 0 means unlimited restarts +respawn_max="${respawn_max:-0}" +respawn_delay="${respawn_delay:-5}" +# define respawn period to only count crashes within a +# sliding time window towards respawn_max, e.g.: +# respawn_period=2850 + +healthcheck_delay="${healthcheck_delay:-60}" +healthcheck_timer="${healthcheck_timer:-30}" + +MIX_ENV=prod +ERL_EPMD_ADDRESS="${ERL_EPMD_ADDRESS:-127.0.0.1}" +supervise_daemon_args="${supervise_daemon_args} --env MIX_ENV=${MIX_ENV} --env ERL_EPMD_ADDRESS=${ERL_EPMD_ADDRESS}" -# put akkoma_console=YES in /etc/conf.d/akkoma if you want to be able to -# connect to akkoma via an elixir console if yesno "${akkoma_console}"; then command=elixir command_args="--name akkoma@127.0.0.1 --erl '-kernel inet_dist_listen_min 9001 inet_dist_listen_max 9001 inet_dist_use_interface {127,0,0,1}' -S mix phx.server" @@ -31,13 +48,18 @@ else command_args="phx.server" fi -export MIX_ENV=prod -export ERL_EPMD_ADDRESS=127.0.0.1 - depend() { need nginx postgresql } +start_pre() { + # Ensure logfile ownership and perms are alright + checkpath --file --owner "$command_user" "$output_log" "$error_log" \ + || eerror "Logfile(s) not owned by $command_user, or not a file!" + checkpath --writable "$output_log" "$error_log" \ + || eerror "Logfile(s) not writable!" +} + healthcheck() { # put akkoma_health=YES in /etc/conf.d/akkoma if you want healthchecking # and make sure you have curl installed From 2901fda29c467ec333f692b50aa0bd4bdce205d4 Mon Sep 17 00:00:00 2001 From: Oneric Date: Tue, 24 Sep 2024 16:47:51 +0200 Subject: [PATCH 2/3] openrc: recompile with lower CPU prio --- installation/init.d/akkoma | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/installation/init.d/akkoma b/installation/init.d/akkoma index cd7b70e7e..747abc2c9 100755 --- a/installation/init.d/akkoma +++ b/installation/init.d/akkoma @@ -58,6 +58,12 @@ start_pre() { || eerror "Logfile(s) not owned by $command_user, or not a file!" checkpath --writable "$output_log" "$error_log" \ || eerror "Logfile(s) not writable!" + + # If a recompile is needed perform it with lowest prio + # (delaying the actual start) to avoid hogging too much + # CPU from other services + cd "$directory" + doas -u "${command_user%%:*}" env MIX_ENV="$MIX_ENV" nice -n 19 "$command" compile } healthcheck() { From a8a231c5b23ceadf30eb7ce212aaf30da2ef9074 Mon Sep 17 00:00:00 2001 From: Oneric Date: Tue, 24 Sep 2024 17:36:54 +0200 Subject: [PATCH 3/3] Don't busy wait in default from-source service files --- installation/akkoma.service | 3 +++ installation/init.d/akkoma | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/installation/akkoma.service b/installation/akkoma.service index 717693495..4945e108c 100644 --- a/installation/akkoma.service +++ b/installation/akkoma.service @@ -19,6 +19,9 @@ Environment="MIX_ENV=prod" ; Don't listen epmd on 0.0.0.0 Environment="ERL_EPMD_ADDRESS=127.0.0.1" +; Don't busy wait +Environment="ERL_AFLAGS=+sbwt none +sbwtdcpu none +sbwtdio none" + ; Make sure that all paths fit your installation. ; Path to the home directory of the user running the Akkoma service. Environment="HOME=/var/lib/akkoma" diff --git a/installation/init.d/akkoma b/installation/init.d/akkoma index 747abc2c9..a03b494c0 100755 --- a/installation/init.d/akkoma +++ b/installation/init.d/akkoma @@ -33,7 +33,10 @@ healthcheck_timer="${healthcheck_timer:-30}" MIX_ENV=prod ERL_EPMD_ADDRESS="${ERL_EPMD_ADDRESS:-127.0.0.1}" -supervise_daemon_args="${supervise_daemon_args} --env MIX_ENV=${MIX_ENV} --env ERL_EPMD_ADDRESS=${ERL_EPMD_ADDRESS}" +ERL_AFLAGS="${ERL_AFLAGS:-+sbwt none +sbwtdcpu none +sbwtdio none}" +supervise_daemon_args="${supervise_daemon_args} --env MIX_ENV=${MIX_ENV}" +supervise_daemon_args="${supervise_daemon_args} --env ERL_EPMD_ADDRESS=${ERL_EPMD_ADDRESS}" +supervise_daemon_args="${supervise_daemon_args} --env ERL_AFLAGS='${ERL_AFLAGS}'" if yesno "${akkoma_console}"; then command=elixir