Skip to content

Commit

Permalink
Use systemd to start Puma
Browse files Browse the repository at this point in the history
  • Loading branch information
javierm committed Sep 16, 2023
1 parent 60b75aa commit 6646653
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
6 changes: 6 additions & 0 deletions group_vars/all
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ database_user: "{{ deploy_user }}"
database_password: "{{ deploy_user }}"
database_hostname: "localhost"

# Puma
puma_service_unit_name: "puma_{{ app_name }}_{{ env }}"
puma_config_file: "{{ release_dir }}/config/puma/{{ env }}.rb"
puma_access_log: "{{ shared_dir }}/log/puma_access.log"
puma_error_log: "{{ shared_dir }}/log/puma_error.log"

#SMTP
smtp_address: "smtp.example.com"
smtp_port: 25
Expand Down
30 changes: 13 additions & 17 deletions roles/puma/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,22 @@
- "pids"
- "sockets"

- name: Check that puma is running
stat:
path: "{{ shared_dir }}/tmp/pids/puma.pid"
register: puma_process

- name: Get running puma process
shell: "cat {{ shared_dir }}/tmp/pids/puma.pid"
register: running_process
when: puma_process.stat.exists == True
- name: Create systemd folder
file:
path: "{{ home_dir }}/.config/systemd/user"
state: directory

- name: Kill running process
shell: "kill -QUIT {{ item }}"
with_items: "{{ running_process.stdout_lines }}"
when: puma_process.stat.exists == True
- name: Copy Puma service file to the systemd folder
template:
src: "{{ playbook_dir }}/roles/puma/templates/puma.service"
dest: "{{ home_dir }}/.config/systemd/user/puma.service"

- name: Start puma
shell: "source {{ home_dir }}/.rvm/scripts/rvm && bundle exec puma -C {{ release_dir }}/config/puma/{{ env }}.rb -e {{ env }} -d"
args:
chdir: "{{ release_dir }}"
executable: /bin/bash
systemd:
name: "{{ puma_service_unit_name }}"
enabled: true
state: started
scope: user

- name: Make sure Nginx has write access to the puma socket
shell: "chmod o+w tmp/sockets/*"
Expand Down
17 changes: 17 additions & 0 deletions roles/puma/templates/puma.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Unit]
Description=Puma HTTP Server for {{ app_name }} ({{ env }})
After=network.target

[Service]
Type=simple
WorkingDirectory={{ release_dir }}
ExecStart=bundle exec --keep-file-descriptors puma -C {{ puma_config_file }}
ExecReload=/bin/kill -USR1 $MAINPID
StandardOutput=append: {{ puma_access_log }}
StandardError=append: {{ puma_error_log }}
Restart=always
RestartSec=1
SyslogIdentifier=puma

[Install]
WantedBy=default.target

0 comments on commit 6646653

Please sign in to comment.