CrumpetDev / crumpet

GNU Affero General Public License v3.0
18 stars 1 forks source link

Improve automatic transition execution logic #78

Open tomtitherington opened 6 months ago

tomtitherington commented 6 months ago

Background & Scope

Refactor execute_automatic_transitions to call execute_manual_transition with all valid transitions to improve speed.

Proposed Changes

Implement the TODO in the code below (flow_instance.py) and pass these valid transitions to execute_manual_transition.

def execute_automatic_transitions(self):
        """
        Executes all available automatic transitions for this flow instance.
        """
        if self.state != self.FlowState.ACTIVE:
            return
        with transaction.atomic():
            for active_step in self.active_steps.all():
                transition_count = 0
                # TODO: Filter the transitions to only include the ones that can transition
                for transition in active_step.outgoing_transitions.filter(
                    transition_schema__type=TransitionSchema.TransitionType.AUTOMATIC
                ):
                    if transition.can_transition:
                        self.execute_manual_transition([transition])
                        transition_count += 1
                    else:
                        continue
                if transition_count:
                    # Only want to set the active step to completed if at least one transition was executed
                    active_step.state = StepInstance.StepState.COMPLETED
                    active_step.save()
        # Check if there are no more steps to execute
        self._check_completed()