Module.start raises on missing func; Module.kill kills Task if available; Module.get_mtime returns None of file is missing instead of raising error; added try/except/print to ModuleManager._run_all; added module killing when removing module from cache; slowed down the output
This commit is contained in:
@ -13,7 +13,7 @@ class ModuleManager:
|
||||
self.update()
|
||||
|
||||
|
||||
def run(self, sleep_time=1):
|
||||
def run(self, sleep_time=2):
|
||||
while True:
|
||||
print("-" * 20)
|
||||
self.update()
|
||||
@ -28,11 +28,16 @@ class ModuleManager:
|
||||
|
||||
def _update_cached(self):
|
||||
for fn, mod in tuple(self.modules.items()):
|
||||
if not mod.file_exists():
|
||||
mod_missing = not mod.file_exists()
|
||||
mod_has_changed = mod.has_changed()
|
||||
|
||||
if mod_missing or mod_has_changed:
|
||||
if mod_missing:
|
||||
print(f"{fn} does not exist... removing cached module")
|
||||
del self.modules[fn]
|
||||
elif mod.has_changed():
|
||||
elif mod_has_changed:
|
||||
print(f"{fn} has changed... removing cached module for reloading")
|
||||
|
||||
mod.kill()
|
||||
del self.modules[fn]
|
||||
|
||||
|
||||
@ -49,7 +54,7 @@ class ModuleManager:
|
||||
mod.load()
|
||||
except ModuleLoadError as e:
|
||||
print(f"loading {fn} raised", printable_exception(e))
|
||||
except MissingRunFunctionError as e:
|
||||
except MissingRunFunctionError:
|
||||
print(f"missing run function in {fn}")
|
||||
|
||||
self.modules[fn] = mod
|
||||
@ -61,7 +66,10 @@ class ModuleManager:
|
||||
print(f"{mod.name} is running already")
|
||||
else:
|
||||
print(f"{mod.name} starting")
|
||||
try:
|
||||
mod.start()
|
||||
except MissingRunFunctionError:
|
||||
print(f"{mod.name} has no function to run")
|
||||
|
||||
|
||||
@property
|
||||
|
@ -17,12 +17,17 @@ class Module:
|
||||
|
||||
def start(self):
|
||||
if not self.func:
|
||||
print(f"{self.name} has no function to run")
|
||||
return
|
||||
raise MissingRunFunctionError
|
||||
|
||||
self.task = task = Task(self.func, self.name)
|
||||
task.start()
|
||||
return task
|
||||
|
||||
|
||||
def kill(self):
|
||||
if self.task:
|
||||
self.task.kill()
|
||||
|
||||
@property
|
||||
def is_running(self):
|
||||
if self.task:
|
||||
@ -48,7 +53,10 @@ class Module:
|
||||
return self.mtime != self.get_mtime()
|
||||
|
||||
def get_mtime(self):
|
||||
try:
|
||||
return self.fname.stat().st_mtime
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.fname} -> {self.name} ({self.mtime})"
|
||||
|
Reference in New Issue
Block a user