Skip to content

Run the :system service as the Capistrano user and make its environment configurable - #13

Merged
burisu merged 2 commits into
mainfrom
system-user-and-unit-env
Nov 9, 2026
Merged

burisu merged 2 commits into
mainfrom
system-user-and-unit-env

Conversation

@burisu

@burisu burisu commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Takes over what holds up in #6, after checking each point.

Changes

  • :system mode: the unit gets User=<output of id -un on the server>. Without a User= line, systemd runs the service as root. Make systemd unit configurable and fix :system-mode defaults #6 used fetch(:run_as, fetch(:user)), which renders an empty User= when the user comes from server "…", user: "…" or from the SSH config, and systemd then runs the service as root too.
  • :solid_queue_service_unit_env_vars and :solid_queue_service_unit_env_files add Environment="…" and EnvironmentFile= lines, after the template's own variables so they override them. They default to fetch(:service_unit_env_vars, []) and fetch(:service_unit_env_files, []), as in capistrano3-puma 8.1.0 (puma/systemd.rb:20-21) and capistrano-sidekiq 3.2.0 (sidekiq/systemd.rb:25-26). default_env and MALLOC_ARENA_MAX=2 stay in the unit.
  • Description=SolidQueue background job for <application> (<stage>).
  • README: Configuration section (defaults, user or system service, order and precedence of the service environment). CHANGELOG: Unreleased entries, crediting Make systemd unit configurable and fix :system-mode defaults #6.

Not taken from #6

  • WantedBy=multi-user.target in :system mode: an enabled system service with WantedBy=default.target starts at boot on Ubuntu 20.04 (systemd 245) and 24.04 (systemd 255), with graphical.target or multi-user.target as default target. The change wouldn't change behavior.
  • Dropping default_env and MALLOC_ARENA_MAX=2 from the unit: installed services would change silently at their next solid_queue:install. The new variables can override them instead.
  • RAILS_ENV=<stage>: since 0.1.3, it comes from :rails_env (Use :rails_env for RAILS_ENV in the systemd service #9).
  • :solid_queue_command: rake solid_queue:start still exists in Solid Queue 1.7.2 (lib/solid_queue/tasks.rb:13).
  • set_if_empty :solid_queue_systemctl_user, :user: no effect, every read of the variable compares it with :system.

Tests

  • test/capistrano/test_service_unit.rb (cap preprod solid_queue:generate in a minimal Capistrano project): description; User=<current user> in :system mode and no User= line in user mode; Environment="…" and EnvironmentFile= lines, written after MALLOC_ARENA_MAX=2; fallback to the shared variables. The four tests on new lines failed before the change.
  • Mutation: removing the new description, the :system condition, the User= line, the Environment= or EnvironmentFile= line, or either default makes at least one test fail.
  • In a Docker container running systemd 255 (Ubuntu 24.04), with a unit generated by this branch (ExecStart replaced by a command that dumps id -un and env): the service runs as the User= user; MALLOC_ARENA_MAX=4 from the new variable overrides the template's 2; a variable from EnvironmentFile= overrides the same one from Environment=; a missing -/… file is ignored.
  • Also checked in that container: with no User= line or an empty one, a system service runs as root; a missing environment file without - keeps the service from starting; a root-owned 0644 log file still receives the output of a User=deploy service.
  • bundle exec rake (tests + standard): green locally (Ruby 4.0.6).

Upgrade

Installed :system services keep running as root until cap <stage> solid_queue:install runs again. Files the service created as root may then need to be given back to the deploy user. Solid Queue 1.7.2 writes no pid file by default (supervisor_pidfile is unset).

🤖 Generated with Claude Code

burisu and others added 2 commits November 9, 2026 10:07
… son environnement

Repris de la PR #6, après vérification de chaque point dans systemd
(Ubuntu 24.04, systemd 255) :

- en mode :system, l'unité n'avait pas de User= et Solid Queue tournait
  en root. Elle reçoit maintenant User=<id -un sur le serveur> : la
  valeur de la PR #6 (fetch(:run_as, fetch(:user))) donnait « User= »
  vide quand l'utilisateur vient de server "…", user: ou de la config
  SSH, et systemd lance alors le service en root ;
- :solid_queue_service_unit_env_vars et
  :solid_queue_service_unit_env_files ajoutent des lignes Environment= et
  EnvironmentFile=, par défaut :service_unit_env_vars et
  :service_unit_env_files comme capistrano3-puma et capistrano-sidekiq.
  Elles viennent après les variables du template, donc les remplacent ;
- la description du service nomme l'application et le stage.

Non repris : WantedBy=multi-user.target (un service système en
WantedBy=default.target démarre au boot sous systemd 245 et 255),
l'abandon de default_env et de MALLOC_ARENA_MAX=2, :solid_queue_command
(rake solid_queue:start existe toujours dans Solid Queue 1.7.2) et
set_if_empty :solid_queue_systemctl_user, :user (sans effet : le code ne
compare qu'à :system).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Section Configuration du README : variables et valeurs par défaut lues
dans set_defaults, service utilisateur ou système, ordre et priorité
des variables d'environnement du service (vérifiés dans systemd 255 :
une ligne Environment= plus loin remplace la précédente, EnvironmentFile=
remplace Environment=, un fichier absent sans « - » empêche le
démarrage).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@burisu
burisu marked this pull request as ready for review November 9, 2026 09:12
@burisu
burisu merged commit 3bb149a into main Nov 9, 2026
5 checks passed
@burisu
burisu deleted the system-user-and-unit-env branch November 9, 2026 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant