Skip to content

Commit 16d195e

Browse files
Make systemd unit configurable and fix :system-mode defaults
- Add :solid_queue_service_unit_env_files / :solid_queue_service_unit_env_vars for injecting EnvironmentFile= and Environment="KEY=VALUE" lines into the unit (inspired by capistrano-sidekiq). Both fall back to shared :service_unit_env_files / :service_unit_env_vars. - Add :solid_queue_command (default "rake solid_queue:start") so setups on SolidQueue >= 0.4 can switch to "bin/jobs" without templating. - Fix WantedBy: multi-user.target when running in :system mode (previously always default.target, which prevented boot activation of system units). - Fix :system mode running the service implicitly as root — now uses :solid_queue_user (falls back to :run_as, then :user). - Drop implicit rendering of Capistrano's default_env and the hardcoded MALLOC_ARENA_MAX=2; pass anything you need explicitly via the new hooks. RAILS_ENV is still derived from the current stage.
1 parent c2e6e6d commit 16d195e

4 files changed

Lines changed: 90 additions & 7 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
## [Unreleased]
22

3+
- Make systemd unit environment variables configurable via
4+
`:solid_queue_service_unit_env_files` and `:solid_queue_service_unit_env_vars`
5+
(inspired by `capistrano-sidekiq`). Both fall back to shared
6+
`:service_unit_env_files` / `:service_unit_env_vars` when set.
7+
- Make the executed command configurable via `:solid_queue_command`
8+
(default: `"rake solid_queue:start"`) so SolidQueue >= 0.4 setups can use
9+
`"bin/jobs"`.
10+
- Fix: `[Install] WantedBy=` now switches to `multi-user.target` when running
11+
in `:system` mode (previously always `default.target`, which prevented boot
12+
activation of system units).
13+
- Fix: in `:system` mode the service now runs as `:solid_queue_user`
14+
(defaults to `:run_as` → `:user`) instead of implicitly running as root.
15+
- **Breaking**: Capistrano's `default_env` is no longer rendered into the
16+
systemd unit file. `MALLOC_ARENA_MAX=2` is no longer hardcoded either. Pass
17+
what you need explicitly via the new configuration hooks. `RAILS_ENV` is
18+
still set from the current stage.
19+
320
## [0.1.2] - 2025-06-12
421

522
- Quiet task only runs on :solid_queue_role [#3](https://github.com/codeur/capistrano-solid_queue/pull/5) by [@bashcoder](https://github.com/bashcoder)

‎README.md‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,56 @@ through their Capistrano counterparts, ex: `bundle exec cap solid_queue:restart`
7070
The plugin has registered a Capistrano `hook` to run `bundle exec cap solid_queue:restart` after deploy.
7171
See [`#register_hooks`](lib/capistrano/solid_queue.rb)
7272

73+
## Configuration
74+
75+
The following variables can be set in your `deploy.rb` or stage files:
76+
77+
```ruby
78+
set :solid_queue_role, :app
79+
set :solid_queue_access_log, -> { File.join(shared_path, "log", "solid_queue.log") }
80+
set :solid_queue_error_log, -> { File.join(shared_path, "log", "solid_queue.log") }
81+
set :solid_queue_service_unit_name, -> { "#{fetch(:application)}_solid_queue_#{fetch(:stage)}" }
82+
set :solid_queue_systemctl_user, :user # or :system
83+
84+
# What runs after `bundle exec` — override for SolidQueue >= 0.4:
85+
set :solid_queue_command, "bin/jobs"
86+
87+
# Unix user for the service when running in :system mode
88+
# (defaults to :run_as, then to Capistrano's :user)
89+
set :solid_queue_user, "deploy"
90+
```
91+
92+
### Environment variables
93+
94+
Two hooks let you inject arbitrary environment into the generated systemd unit
95+
(inspired by [`capistrano-sidekiq`](https://github.com/seuros/capistrano-sidekiq)):
96+
97+
```ruby
98+
# Referenced via `EnvironmentFile=` — one line per file
99+
set :solid_queue_service_unit_env_files, %w[
100+
/etc/environment
101+
/home/deploy/apps/my_app/shared/.env
102+
]
103+
104+
# Rendered as `Environment="KEY=VALUE"` — one line per entry
105+
set :solid_queue_service_unit_env_vars, [
106+
"RAILS_LOG_TO_STDOUT=1",
107+
"DB_POOL=10",
108+
"SOLID_QUEUE_IN_PUMA=false"
109+
]
110+
```
111+
112+
If your project already sets `:service_unit_env_files` / `:service_unit_env_vars`
113+
(shared with other systemd-based capistrano plugins), they are picked up as
114+
the default — you only need to override when SolidQueue should see a different
115+
set of variables.
116+
117+
Only `RAILS_ENV=<stage>` is set implicitly (it is derived directly from the
118+
Capistrano stage). Capistrano's `default_env` is **not** rendered into the unit
119+
file — pass anything else you need (including tuning knobs like
120+
`MALLOC_ARENA_MAX`) via `:solid_queue_service_unit_env_vars` or
121+
`:solid_queue_service_unit_env_files`.
122+
73123
## Development
74124

75125
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

‎lib/capistrano/solid_queue.rb‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ def set_defaults
1111
set_if_empty :solid_queue_error_log, -> { File.join(shared_path, "log", "solid_queue.log") }
1212
set_if_empty :solid_queue_service_unit_name, -> { "#{fetch(:application)}_solid_queue_#{fetch(:stage)}" }
1313
set_if_empty :solid_queue_systemd_conf_dir, -> { fetch_systemd_unit_path }
14+
15+
# Command executed by the systemd unit (concatenated after `bundle exec`).
16+
# Override with e.g. "bin/jobs" for SolidQueue >= 0.4 setups.
17+
set_if_empty :solid_queue_command, "rake solid_queue:start"
18+
19+
# Unix user the service runs as when using `:system` mode. Falls back to
20+
# `:run_as` (shared with other systemd capistrano plugins) and finally
21+
# the SSH `:user` capistrano connects with.
22+
set_if_empty :solid_queue_user, -> { fetch(:run_as, fetch(:user)) }
23+
24+
# SolidQueue could have a stripped down or more complex version of the environment variables
25+
set_if_empty :solid_queue_service_unit_env_files, -> { fetch(:service_unit_env_files, []) }
26+
set_if_empty :solid_queue_service_unit_env_vars, -> { fetch(:service_unit_env_vars, []) }
1427
end
1528

1629
def define_tasks
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
[Unit]
2-
Description=SolidQueue background job
2+
Description=SolidQueue background job for <%= "#{fetch(:application)} (#{fetch(:stage)})" %>
33
After=syslog.target network.target
44

55
[Service]
66
Type=simple
7-
Environment='RAILS_ENV=<%= fetch(:stage) %>'
8-
<% fetch(:default_env).reject{ |k, _| k.to_s == 'path' }.each do |variable, value| -%>
9-
Environment='<%= variable.to_s.upcase %>=<%= value.to_s %>'
7+
<%= "User=#{fetch(:solid_queue_user)}" if fetch(:solid_queue_systemctl_user) == :system %>
8+
Environment=RAILS_ENV=<%= fetch(:stage) %>
9+
<% Array(fetch(:solid_queue_service_unit_env_files)).each do |file| -%>
10+
EnvironmentFile=<%= file %>
11+
<% end -%>
12+
<% Array(fetch(:solid_queue_service_unit_env_vars)).each do |environment_variable| -%>
13+
Environment="<%= environment_variable %>"
1014
<% end -%>
1115
WorkingDirectory=<%= current_path %>
12-
ExecStart=<%= capture(:echo, SSHKit.config.command_map[:bundle]).strip %> exec rake solid_queue:start
16+
ExecStart=<%= capture(:echo, SSHKit.config.command_map[:bundle]).strip %> exec <%= fetch(:solid_queue_command) %>
1317
ExecReload=/bin/kill -TSTP $MAINPID
1418
ExecStop=/bin/kill -TERM $MAINPID
15-
Environment=MALLOC_ARENA_MAX=2
1619

1720
# if we crash, restart
1821
RestartSec=1
@@ -24,4 +27,4 @@ Restart=on-failure
2427
SyslogIdentifier=<%= fetch(:solid_queue_service_unit_name) %>
2528

2629
[Install]
27-
WantedBy=default.target
30+
WantedBy=<%= fetch(:solid_queue_systemctl_user) == :system ? "multi-user.target" : "default.target" %>

0 commit comments

Comments
 (0)