Improve autoserve logic, reduce repeated reloads.

This commit is contained in:
Sergio Martínez Portela 2022-07-25 12:19:32 +02:00
parent 8d831af216
commit feefd1f4d5
3 changed files with 22 additions and 5 deletions

View File

@ -12,6 +12,8 @@ import inotify.adapters
PORT = 8000 PORT = 8000
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
MONITORED_EVENT_TYPES = ( MONITORED_EVENT_TYPES = (
'IN_CREATE', 'IN_CREATE',
# 'IN_MODIFY', # 'IN_MODIFY',
@ -66,7 +68,7 @@ class Server(http.server.SimpleHTTPRequestHandler):
return return
else: else:
# Append update waiter # Append update waiter
with open('wait_for_update.js', 'rb') as f: with open(os.path.join(THIS_DIR, 'wait_for_update.js'), 'rb') as f:
new_data = b'<script>' + f.read() + b'</script>' new_data = b'<script>' + f.read() + b'</script>'
self.wfile.write(new_data) self.wfile.write(new_data)
new_data_len = len(new_data) new_data_len = len(new_data)
@ -92,10 +94,19 @@ def notify_reloads():
def start_notifier(): def start_notifier():
notifier = inotify.adapters.InotifyTree(os.getcwd()) notifier = inotify.adapters.InotifyTree(os.getcwd())
for event in notifier.event_gen(yield_nones=False): should_reload = False
for event in notifier.event_gen(yield_nones=True):
if event is None:
if should_reload:
print("Reloading!")
should_reload = False
notify_reloads()
continue
(ev, types, directory, file) = event (ev, types, directory, file) = event
if any([type in MONITORED_EVENT_TYPES for type in types]): if any([type in MONITORED_EVENT_TYPES for type in types]):
notify_reloads() print("Detected change!", types, directory, file)
should_reload = True
def serve(): def serve():
Handler = Server Handler = Server

View File

@ -213,9 +213,12 @@ def main(source_top, dest_top):
if filepath.startswith(STATIC_PATH): if filepath.startswith(STATIC_PATH):
t0 = time.time() t0 = time.time()
update_statics() update_statics()
is_static_resource = False
for static in STATIC_RESOURCES: for static in STATIC_RESOURCES:
src_path = static[0] src_path = static[0]
dest_path = static[1] dest_path = static[1]
if file == os.path.basename(src_path):
is_static_resource = True
if len(static) > 2: if len(static) > 2:
before, after = static[2] before, after = static[2]
@ -229,8 +232,11 @@ def main(source_top, dest_top):
with open(target_dest, 'wt') as f: with open(target_dest, 'wt') as f:
f.write(data) f.write(data)
docs = regen_all(source_top, dest_top, docs) if is_static_resource:
logging.info("Updated all in {:.2f}s".format(time.time() - t0)) logging.info("Updated static resources in {:.2f}s".format(time.time() - t0))
else:
docs = regen_all(source_top, dest_top, docs)
logging.info("Updated all in {:.2f}s".format(time.time() - t0))
else: else:
try: try: