From feefd1f4d57deca286a74a223644ea00203a8b6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Mon, 25 Jul 2022 12:19:32 +0200 Subject: [PATCH] Improve autoserve logic, reduce repeated reloads. --- scripts/autoserve.py | 17 ++++++++++++++--- scripts/blog.py | 10 ++++++++-- .../wait_for_update.js | 0 3 files changed, 22 insertions(+), 5 deletions(-) rename wait_for_update.js => scripts/wait_for_update.js (100%) diff --git a/scripts/autoserve.py b/scripts/autoserve.py index a9eb997..6e69025 100644 --- a/scripts/autoserve.py +++ b/scripts/autoserve.py @@ -12,6 +12,8 @@ import inotify.adapters PORT = 8000 +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) + MONITORED_EVENT_TYPES = ( 'IN_CREATE', # 'IN_MODIFY', @@ -66,7 +68,7 @@ class Server(http.server.SimpleHTTPRequestHandler): return else: # 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'' self.wfile.write(new_data) new_data_len = len(new_data) @@ -92,10 +94,19 @@ def notify_reloads(): def start_notifier(): 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 if any([type in MONITORED_EVENT_TYPES for type in types]): - notify_reloads() + print("Detected change!", types, directory, file) + should_reload = True def serve(): Handler = Server diff --git a/scripts/blog.py b/scripts/blog.py index 050f0ec..82f1cf3 100644 --- a/scripts/blog.py +++ b/scripts/blog.py @@ -213,9 +213,12 @@ def main(source_top, dest_top): if filepath.startswith(STATIC_PATH): t0 = time.time() update_statics() + is_static_resource = False for static in STATIC_RESOURCES: src_path = static[0] dest_path = static[1] + if file == os.path.basename(src_path): + is_static_resource = True if len(static) > 2: before, after = static[2] @@ -229,8 +232,11 @@ def main(source_top, dest_top): with open(target_dest, 'wt') as f: f.write(data) - docs = regen_all(source_top, dest_top, docs) - logging.info("Updated all in {:.2f}s".format(time.time() - t0)) + if is_static_resource: + 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: try: diff --git a/wait_for_update.js b/scripts/wait_for_update.js similarity index 100% rename from wait_for_update.js rename to scripts/wait_for_update.js