Added abort to command
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Pünkösd Marcell 2021-04-11 14:10:14 +02:00
parent 203b513c9e
commit 964a072e80
4 changed files with 27 additions and 10 deletions

View File

@ -8,6 +8,10 @@ class AbstractCommand(ABC):
def execute(self): def execute(self):
pass pass
@abstractmethod
def abort(self):
pass
@abstractmethod @abstractmethod
def describe(self) -> dict: def describe(self) -> dict:
pass pass

View File

@ -2,7 +2,7 @@ from typing import Dict
from .abstract_plugin import AbstractCommand, AbstractPlugin, AbstractCommandCompiler from .abstract_plugin import AbstractCommand, AbstractPlugin, AbstractCommandCompiler
import logging import logging
import time from threading import Event
class SleepCommand(AbstractCommand): class SleepCommand(AbstractCommand):
@ -17,11 +17,19 @@ class SleepCommand(AbstractCommand):
self._secs = secs self._secs = secs
self._logger = logger self._logger = logger
self._event = Event()
def execute(self): def execute(self):
self._logger.debug(f"Sleeping for {self._secs} seconds") self._logger.debug(f"Sleeping for {self._secs} seconds")
time.sleep(self._secs) self._event.clear()
self._logger.debug(f"Slept for {self._secs} seconds") aborted = self._event.wait(timeout=self._secs)
if aborted:
self._logger.warning("Sleeping aborted externally!")
else:
self._logger.debug(f"Slept for {self._secs} seconds")
def abort(self):
self._event.set() # <- force the event.wait to return
def describe(self) -> dict: def describe(self) -> dict:
return { return {

View File

@ -29,6 +29,10 @@ class SyncCommand(AbstractCommand):
self._procsync_instance.sync(self._name, self._nodes, Config.SYNC_TIMEOUT) self._procsync_instance.sync(self._name, self._nodes, Config.SYNC_TIMEOUT)
self._logger.debug(f"Event {self._name} synchronized!") self._logger.debug(f"Event {self._name} synchronized!")
def abort(self):
# TODO
pass
def describe(self) -> dict: def describe(self) -> dict:
return { return {
"command": "sync", "command": "sync",

View File

@ -33,7 +33,7 @@ class ProgramExecutor(Thread):
def abort(self): def abort(self):
self._state = ProgramExecutorStates.ABORTED self._state = ProgramExecutorStates.ABORTED
# TODO: implement abort self._program[self._pc].abort()
def get_status(self) -> dict: def get_status(self) -> dict:
return { return {
@ -55,7 +55,7 @@ class ProgramExecutor(Thread):
self._state = ProgramExecutorStates.RUNNING self._state = ProgramExecutorStates.RUNNING
self._logger.info("Start running program") self._logger.info("Start running program")
while True: # needed for loop while self._state == ProgramExecutorStates.RUNNING: # needed for loop
self._loop_counter += 1 self._loop_counter += 1
for i, step in enumerate(self._program): for i, step in enumerate(self._program):
self._pc = i self._pc = i
@ -69,12 +69,13 @@ class ProgramExecutor(Thread):
self._state = ProgramExecutorStates.CRASHED self._state = ProgramExecutorStates.CRASHED
return return
# TODO: jogging wait if self._state != ProgramExecutorStates.RUNNING:
self._logger.info(f"State changed to {self._state.name}. Execution will now stop!")
return
if self._loop: if self._loop:
self._logger.debug("Looping program") self._logger.debug("Looping program")
else: else:
self._logger.debug("Program ended") self._logger.info("Program ended")
break self._state = ProgramExecutorStates.DONE
return
self._state = ProgramExecutorStates.DONE