def add_children_to_stack_and_child_id_to_parent(
        self,
        parent_stack: orderStackData,
        child_stack: orderStackData,
        parent_order: Order,
        list_of_child_orders: listOfOrders,
    ):

        parent_log = parent_order.log_with_attributes(self.log)

        list_of_child_ids = put_children_on_stack(
            child_stack=child_stack,
            list_of_child_orders=list_of_child_orders,
            parent_log=parent_log,
            parent_order=parent_order,
        )
        if len(list_of_child_ids) == 0:
            return None

        success_or_failure = add_children_to_parent_or_rollback_children(
            child_stack=child_stack,
            parent_order=parent_order,
            parent_log=parent_log,
            parent_stack=parent_stack,
            list_of_child_ids=list_of_child_ids,
        )

        if success_or_failure is success:
            log_successful_adding(
                list_of_child_orders=list_of_child_orders,
                list_of_child_ids=list_of_child_ids,
                parent_order=parent_order,
                parent_log=parent_log,
            )
Пример #2
0
    def add_instrument_and_list_of_contract_orders_to_stack(
        self,
            instrument_order: instrumentOrder,
            list_of_contract_orders: listOfOrders
    ):

        instrument_stack = self.instrument_stack
        contract_stack = self.contract_stack
        parent_log = instrument_order.log_with_attributes(self.log)

        # Do as a transaction: if everything doesn't go to plan can roll back
        # We lock now, and
        instrument_order.lock_order()
        try:
            parent_order_id = instrument_stack.put_order_on_stack(
                instrument_order, allow_zero_orders = True)

        except Exception as parent_order_error:
            parent_log.warn(
                    "Couldn't put parent order %s on instrument order stack error %s" %
                    (str(instrument_order), str(parent_order_error)))
            instrument_order.unlock_order()
            return None

        ## Parent order is now on stack in locked state
        ## We will unlock at the end, or during a rollback

        # Do as a transaction: if everything doesn't go to plan can roll back
        # if this try fails we will roll back the instrument commit
        list_of_child_order_ids =[]

        try:
            # Add parent order to children
            # This will only throw an error if the orders already have parents, which they shouldn't
            for child_order in list_of_contract_orders:
                child_order.parent = parent_order_id

            # this will return eithier -
            #     - a list of order IDS if all went well
            #     - an empty list if error and rolled back,
            #      - or an error something went wrong and couldn't rollback (the outer catch will try and rollback)
            list_of_child_order_ids = put_children_on_stack(child_stack = contract_stack,
                                                            parent_log=parent_log,
                                                            list_of_child_orders=list_of_contract_orders,
                                                            parent_order = instrument_order)

            if len(list_of_child_order_ids)==0:
                ## We had an error, but manged to roll back the children. Still need to throw an error so the parent
                ##   will be rolledback. But because the list_of_child_order_ids is probably zero
                ##   we won't try and rollback children in the catch statement
                raise Exception("Couldn't put child orders on stack, children were rolled back okay")

            ## All seems to have worked

            # still locked remember
            instrument_stack.unlock_order_on_stack(parent_order_id)
            instrument_stack.add_children_to_order_without_existing_children(
                parent_order_id, list_of_child_order_ids
            )


        except Exception as error_from_adding_child_orders:
            # okay it's gone wrong
            # Roll back parent order and possibly children
            # At this point list_of_child_order_ids will eithier be empty (if succesful rollback) or contain child ids

            rollback_parents_and_children_and_handle_exceptions(child_stack=contract_stack,
                                          parent_stack=instrument_stack,
                                          list_of_child_order_ids=list_of_child_order_ids,
                                          parent_order_id=parent_order_id,
                                        error_from_adding_child_orders=error_from_adding_child_orders,
                                                                parent_log=parent_log)

        # phew got there
        parent_log.msg("Added parent order with ID %d %s to stack" % (parent_order_id, str(instrument_order)))
        log_successful_adding(list_of_child_orders=list_of_contract_orders,
                              list_of_child_ids=list_of_child_order_ids,
                              parent_order=instrument_order,
                              parent_log=parent_log)