Reload gunicorn from cronjob, delete gunicorn_reloader

Closes tildes-community/tildes-cf#3

See merge request tildes-community/tildes-cf!6
This commit is contained in:
talklittle
2025-01-16 19:12:49 +00:00
committed by Deimos
parent b97eaed93c
commit 58784c4465
5 changed files with 17 additions and 51 deletions

View File

@@ -35,10 +35,13 @@
hour: 0 hour: 0
minute: 10 minute: 10
# Also reloads gunicorn if CSS updated, to update the cache-busting strings
- name: Add cronjob for generating site-icons CSS file - name: Add cronjob for generating site-icons CSS file
cron: cron:
name: generate_site_icons_css 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 }}" user: "{{ app_username }}"
minute: "*/5" minute: "*/5"

View File

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

View File

@@ -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

View File

@@ -29,35 +29,8 @@
state: started state: started
enabled: true enabled: true
# Set up the gunicorn_reloader service, which reloads gunicorn whenever certain files - name: Start and enable gunicorn service
# are changed (such as static files, to update the cache-busting strings) systemd_service:
- name: Create gunicorn_reloader service file name: gunicorn.service
copy: state: started
src: gunicorn_reloader.service enabled: true
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)
systemd_service:
name: gunicorn.service
state: started
enabled: true

View File

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