Home > OS >  Designing a system to centrally manage series of events on different systems
Designing a system to centrally manage series of events on different systems

Time:05-27

I have a problem at work where I need to perform series of sequential tasks on different devices. These devices do not need to interact with each other and also each of the sequential tasks can be performed on each of the devices independently.

Assuming I have Tasks (A->B->C->D)(Ex: End of A triggers B and end of B triggers C and so on), Devices(dev1, dev2) can execute these tasks independent of each other.

How can I design a centralized system that executes each task on each device. I cannot use Threading or Multiprocessing due to Infra limitations. I'm looking for some design suggestions(Classes) and How I can go about designing it. First approach I thought about was brute force where I blindly use loops to loop over devices and perform each task. Second approach I was reading about State Design Pattern and I was not sure how I can implement it.

CodePudding user response:

I probably try something with flask for super simple api and a client app on devices that "pool" data from center api and post results so center server know the progress and what is current used. client app would be super simple loop with sleep so it wont 100% cpu without needed.

CodePudding user response:

I have used State design pattern to handle this. I have a Device class which is concrete class and have a method called "perform_task". This method changes behavior based on the state it is in. At a given point it can be in TaskA TaskB or etc.

class Device():
    _state = None
    def __init__(self):
        """Constructor method"""
        self.switch_to(TaskA())

    def switch_to(self, state):
        self._state = state
        self._state.context = self
    
    def perform_task(self):
        self._state.perform_task()

Then I have a State Abstract class which has abstract methods. Followed by State classes itself.

class State(ABC):
    @property
    def context(self):
        return self._context

    @context.setter
    def context(self, context):
        self._context = context

    @abstractmethod
    def perform_task(self):
        pass
class TaskA():
    def perform_task(self):
        # Do something
        self.context.switch_to(TaskB())
class TaskB():
    def perform_task():
         # Do something.
         pass

Doing so we can extend this to any number of states in the future and handle new conditions too.

  • Related