Commit c451e382 authored by chrissi^'s avatar chrissi^
Browse files

scheduler: Support critical tasks



A task can be set to critical if the system can be considered
disfunctional if it has thrown an error. If a critical task raises an
Exception the machine will be resetted.
Signed-off-by: chrissi^'s avatarChris Fiege <chris@tinyhost.de>
parent 027a0023
......@@ -20,18 +20,37 @@ class Scheduler:
self._idleCallbacks = []
self._raise_mode = raise_mode
def register(self, callback, timeout):
def register(self, callback, timeout, critical=False):
"""
Register a new task.
@param callback: function to call.
@param timeout: timeout in seconds or None for an idle task
@param critical: indicated if a task is critical.
If a critical tasks raises an error the scheduler will
issue a machine.reset()
raise_mode has priority over this setting.
"""
if timeout is not None:
self._callbacks.append((callback, timeout))
self._callbacks.append((callback, timeout, critical))
self._counter.append(0)
else:
self._idleCallbacks.append(callback)
self._idleCallbacks.append((callback, 0, critical))
def _run_callback(self, i, callback):
try:
print("Running task #{}".format(i))
callback[0]()
except Exception as e:
if self._raise_mode:
raise e
else:
print("Callback #{} {} raised exception:".format(i, callback[0]))
print(e)
if callback[2] == True:
print("This task is set to critical. Going to reset this controller!")
import machine
machine.reset()
def run(self):
"""Blocks forever and runs the tasks"""
......@@ -41,14 +60,7 @@ class Scheduler:
cnt = 0
while next > time.time():
for callback in self._idleCallbacks:
try:
callback()
except Exception as e:
if self._raise_mode:
raise e
else:
print("Callback #{} {} raised exception:".format(i, callback))
print(e)
self._run_callback(-1, callbakck)
cnt += 1
#prepare next run
......@@ -60,12 +72,4 @@ class Scheduler:
self._counter[i] +=1
if self._counter[i] == self._callbacks[i][1]:
self._counter[i] = 0
print("Running task #{}".format(i))
try:
self._callbacks[i][0]()
except Exception as e:
if self._raise_mode:
raise e
else:
print("Callback #{} {} raised exception:".format(i, self._callbacks[i][0]))
print(e)
self._run_callback(i, self._callbacks[i])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment