Rebuild assetcache on inotify events.

remotes/1693176582716663532/tmp_refs/heads/watchparty
Snakes 2022-09-27 02:02:02 -04:00
parent b52edce516
commit d0d67085e3
Signed by: Snakes
GPG Key ID: E745A82778055C7E
4 changed files with 24 additions and 16 deletions

View File

@ -1,6 +1,8 @@
import os
import zlib
from collections import defaultdict
import gevent
import gevent_inotifyx as inotify
ASSET_DIR = 'files/assets'
ASSET_SUBDIRS = ['/css', '/js']
@ -29,4 +31,19 @@ def assetcache_path(asset_path):
return url
def assetcache_watch_directories(asset_dir, subdirs):
fd = inotify.init()
try:
for sd in subdirs:
inotify.add_watch(fd, asset_dir + sd, inotify.IN_CLOSE_WRITE)
while True:
for event in inotify.get_events(fd, 0):
print("Rebuilding assetcache: " + event.name, flush=True)
assetcache_build(asset_dir, subdirs)
break
gevent.sleep(0.5)
finally:
os.close(fd)
assetcache_build(ASSET_DIR, ASSET_SUBDIRS)
gevent.spawn(assetcache_watch_directories, ASSET_DIR, ASSET_SUBDIRS)

View File

@ -1,4 +1,5 @@
from files.__main__ import app, cache
from jinja2 import pass_context
from .get import *
from os import listdir, environ
from .const import *
@ -18,7 +19,8 @@ def post_embed(id, v):
@app.template_filter("asset")
def template_asset(asset_path):
@pass_context
def template_asset(ctx, asset_path):
return assetcache_path(asset_path)

View File

@ -7,6 +7,7 @@ max_requests = 30000
max_requests_jitter = 30000
reload = True
reload_engine = 'poll'
#print_config = True
def worker_abort(worker):
@ -27,17 +28,3 @@ def worker_abort(worker):
import os
os.abort()
def config_reload_extra_files():
import os
extra_files = []
asset_dirs = ['files/assets/css/', 'files/assets/js/']
for dir in asset_dirs:
fnames = os.listdir(dir)
paths = map(lambda x: dir + x, fnames)
extra_files.extend(paths)
return extra_files
reload_extra_files = config_reload_extra_files()

View File

@ -9,9 +9,11 @@ Flask-Mail
Flask-Socketio
gevent
gevent-websocket
gevent_inotifyx
greenlet
gunicorn
imagehash
inotify
lxml
mistletoe
matplotlib
@ -30,4 +32,4 @@ pusher_push_notifications
pytest
youtube-dl
yattag
webptools
webptools