Skip to content

Commit

Permalink
Merge pull request #235 from consuldemocracy/puma_socket_activation
Browse files Browse the repository at this point in the history
Enable graceful appication restarts
  • Loading branch information
Senen authored Feb 13, 2024
2 parents e26afe6 + 5ce1152 commit ea4358a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
17 changes: 16 additions & 1 deletion roles/puma/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
src: "{{ playbook_dir }}/roles/puma/templates/puma.service"
dest: "{{ home_dir }}/.config/systemd/user/{{ puma_service_unit_name }}.service"

- name: Copy Puma socket file to the systemd folder
template:
src: "{{ playbook_dir }}/roles/puma/templates/puma.socket"
dest: "{{ home_dir }}/.config/systemd/user/{{ puma_service_unit_name }}.socket"

- name: Get distribution codename
shell: lsb_release -c --short
register: distro_codename
Expand All @@ -36,9 +41,19 @@
shell: "id -u"
register: current_uid

- name: Enable puma socket activation
systemd:
name: "{{ puma_service_unit_name }}.socket"
daemon_reload: true
enabled: true
state: started
scope: user
environment:
XDG_RUNTIME_DIR: "/run/user/{{ current_uid.stdout }}"

- name: Start puma
systemd:
name: "{{ puma_service_unit_name }}"
name: "{{ puma_service_unit_name }}.service"
daemon_reload: true
enabled: true
state: started
Expand Down
4 changes: 3 additions & 1 deletion roles/puma/templates/puma.service
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
[Unit]
Description=Puma HTTP Server for {{ app_name }} ({{ env }})
After=network.target
Requires={{ puma_service_unit_name }}.socket

[Service]
Type=simple
WorkingDirectory={{ release_dir }}
ExecStart=/bin/bash -lc '{{ fnm_command }} && {{ rvm_command }} && fnm exec bundle exec puma -C {{ puma_config_file }} -e {{ env }}'
ExecStart=/bin/bash -lc '{{ rvm_command }} && bundle exec puma -C {{ puma_config_file }} -e {{ env }}'
ExecReload=/bin/kill -USR1 $MAINPID
StandardOutput=append:{{ puma_access_log }}
StandardError=append:{{ puma_error_log }}
Environment=EXECJS_RUNTIME=Disabled
Restart=always
RestartSec=1
SyslogIdentifier=puma
Expand Down
20 changes: 20 additions & 0 deletions roles/puma/templates/puma.socket
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[Unit]
Description=Puma HTTP Server Accept Sockets for {{ app_name }} ({{ env }})

[Socket]
ListenStream={{ shared_dir }}/tmp/sockets/puma.sock

# Don't let systemd accept the request, wait for Puma to do that.
# Systemd will start the puma service upon first request if it wasn't started.
#
# You might also want to set your Nginx upstream to have a fail_timeout large enough to accomodate your app's
# startup time.
Accept=no

ReusePort=true
Backlog=1024

SyslogIdentifier=puma_socket

[Install]
WantedBy=sockets.target

0 comments on commit ea4358a

Please sign in to comment.