1
0
mirror of https://github.com/venthur/blag.git synced 2025-11-25 20:52:43 +00:00

Immediately rebuild once when entering autoreload

This commit is contained in:
Bastian Venthur
2021-11-06 14:12:57 +01:00
parent 60cfd0290a
commit 4f1632e3cd
2 changed files with 49 additions and 1 deletions

View File

@@ -55,6 +55,9 @@ def autoreload(args):
last modified time). If the last modified time has changed, a last modified time). If the last modified time has changed, a
rebuild is triggered. rebuild is triggered.
A rebuild is also performed immediately when this method is called
to avoid serving stale contents.
Parameters Parameters
---------- ----------
args : argparse.Namespace args : argparse.Namespace
@@ -62,7 +65,9 @@ def autoreload(args):
""" """
dirs = [args.input_dir, args.template_dir, args.static_dir] dirs = [args.input_dir, args.template_dir, args.static_dir]
logger.info(f'Monitoring {dirs} for changes...') logger.info(f'Monitoring {dirs} for changes...')
last_mtime = get_last_modified(dirs) # make sure we trigger the rebuild immediately when we enter the
# loop to avoid serving stale contents
last_mtime = 0
while True: while True:
mtime = get_last_modified(dirs) mtime = get_last_modified(dirs)
if mtime > last_mtime: if mtime > last_mtime:

View File

@@ -1,4 +1,5 @@
import time import time
import threading
from blag import devserver from blag import devserver
@@ -19,3 +20,45 @@ def test_get_last_modified(cleandir):
assert t2 > t1 assert t2 > t1
assert t2 == t3 assert t2 == t3
def test_autoreload_builds_immediately(args):
# create a dummy file that can be build
with open('content/test.md', 'w') as fh:
fh.write('boo')
t = threading.Thread(target=devserver.autoreload,
args=(args, ),
daemon=True,)
t0 = devserver.get_last_modified(['build'])
t.start()
# try for 5 seconds...
for i in range(5):
time.sleep(1)
t1 = devserver.get_last_modified(['build'])
print(t1)
if t1 > t0:
break
assert t1 > t0
def test_autoreload(args):
t = threading.Thread(target=devserver.autoreload,
args=(args, ),
daemon=True,)
t.start()
t0 = devserver.get_last_modified(['build'])
# create a dummy file that can be build
with open('content/test.md', 'w') as fh:
fh.write('boo')
# try for 5 seconds...
for i in range(5):
time.sleep(1)
t1 = devserver.get_last_modified(['build'])
print(t1)
if t1 > t0:
break
assert t1 > t0