Browse Source

Reload gunicorn from cronjob, delete gunicorn_reloader

Closes tildes-community/tildes-cf#3

See merge request tildes-community/tildes-cf!6
develop
talklittle 1 month ago
committed by Andrew Shu
parent
commit
d63e39832a
  1. 5
      ansible/roles/cronjobs/tasks/main.yml
  2. 5
      ansible/roles/gunicorn/files/gunicorn_reloader.path
  3. 10
      ansible/roles/gunicorn/files/gunicorn_reloader.service
  4. 29
      ansible/roles/gunicorn/tasks/main.yml
  5. 11
      tildes/scripts/generate_site_icons_css.py

5
ansible/roles/cronjobs/tasks/main.yml

@ -35,10 +35,13 @@
hour: 0
minute: 10
# Also reloads gunicorn if CSS updated, to update the cache-busting strings
- name: Add cronjob for generating site-icons CSS file
cron:
name: generate_site_icons_css
job: "{{ bin_dir }}/python -c \"from scripts.generate_site_icons_css import generate_css; generate_css()\""
job: >
{{ bin_dir }}/python -c "from scripts.generate_site_icons_css import generate_css; generated = generate_css(); exit(0 if generated else 1)"
&& sudo systemctl reload gunicorn.service
user: "{{ app_username }}"
minute: "*/5"

5
ansible/roles/gunicorn/files/gunicorn_reloader.path

@ -1,5 +0,0 @@
[Path]
PathChanged=/opt/tildes/static/css/site-icons.css
[Install]
WantedBy=multi-user.target

10
ansible/roles/gunicorn/files/gunicorn_reloader.service

@ -1,10 +0,0 @@
[Unit]
Description=gunicorn reloader
After=network.target
[Service]
Type=oneshot
ExecStart=/bin/systemctl reload gunicorn.service
[Install]
WantedBy=multi-user.target

29
ansible/roles/gunicorn/tasks/main.yml

@ -29,34 +29,7 @@
state: started
enabled: true
# Set up the gunicorn_reloader service, which reloads gunicorn whenever certain files
# are changed (such as static files, to update the cache-busting strings)
- name: Create gunicorn_reloader service file
copy:
src: gunicorn_reloader.service
dest: /etc/systemd/system/gunicorn_reloader.service
owner: root
group: root
mode: 0644
- name: Create gunicorn_reloader path-monitoring file
copy:
src: gunicorn_reloader.path
dest: /etc/systemd/system/gunicorn_reloader.path
owner: root
group: root
mode: 0644
- name: Start and enable gunicorn_reloader path-monitoring service, with fallback
block:
- name: Start and enable gunicorn_reloader path-monitoring service
systemd_service:
name: gunicorn_reloader.path
state: started
enabled: true
rescue:
# Likely Docker; systemctl3.py doesn't support .path, so enable .service here
- name: Start and enable gunicorn.service (if .path service fails)
- name: Start and enable gunicorn service
systemd_service:
name: gunicorn.service
state: started

11
tildes/scripts/generate_site_icons_css.py

@ -34,10 +34,13 @@ def _is_output_file_outdated() -> bool:
return False
def generate_css() -> None:
"""Generate the CSS file for site icons and replace the old one."""
def generate_css() -> bool:
"""Generate the CSS file for site icons and replace the old one.
Return True if generated, otherwise return False if skipped.
"""
if not _is_output_file_outdated():
return
return False
with NamedTemporaryFile(mode="w") as temp_file:
for filename in os.listdir(ICON_FOLDER):
@ -54,3 +57,5 @@ def generate_css() -> None:
# set file permissions to 644 (rw-r--r--)
os.chmod(OUTPUT_FILE, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
return True
Loading…
Cancel
Save