Example #1
0
    def run_process(*args, **kwargs):
        try:
            run_process_old(*args, **kwargs)
        except (KeyboardInterrupt, SystemExit):
            raise
        except Exception:
            exc_type, exc_value, exc_trace = sys.exc_info()
            print(repr(sys.exc_info()))

            # Handle Flask's broken pipe when a client prematurely ends
            # the connection.
            if str(exc_value) == '[Errno 32] Broken pipe':
                pass
            else:
                logger.critical('Unhandled patched exception ({}): "{}".',
                                exc_type, exc_value)
                sys.excepthook(exc_type, exc_value, exc_trace)
Example #2
0
 def post(self, request, *args, **kwargs):
     # 订单校验成功后修改订单信息(post异步回调的逻辑)
     data = request.data.dict()
     # QueryDict类型没有pop方法,request.query_params.dict()和request.data.dict() 都能直接转化为dict类型
     sign = data.pop('sign')
     # 签名确保安全性
     result = alipay.verify(data, sign)
     # 订单号
     out_trade_no = data.get('out_trade_no')
     # 订单状态
     trade_status = data.get("trade_status")
     # 异步回调,签名sign校验通过,以及订单状态成功或完成,才能确定支付宝支付成功了
     if result and trade_status in ("TRADE_SUCCESS", "TRADE_FINISHED"):
         # print(1111111111111)
         models.Order.objects.filter(out_trade_no=out_trade_no).update(order_status=1)
         logger.critical('订单:%s - %s - 支付成功' % (out_trade_no, trade_status))
         return Response('success')
     return Response('failed')
Example #3
0
 def post(self, request, *args, **kwargs):
     # 默认是QueryDict类型,不能使用pop方法
     request_data = request.data.dict()
     # 必须将 sign、sign_type(内部有安全处理) 从数据中取出,拿sign与剩下的数据进行校验
     sign = request_data.pop('sign')
     result = alipay.verify(request_data, sign)
     # 异步回调:修改订单状态
     if result and request_data["trade_status"] in ("TRADE_SUCCESS",
                                                    "TRADE_FINISHED"):
         out_trade_no = request_data.get('out_trade_no')
         logger.critical('%s支付成功' % out_trade_no)
         try:
             order = models.Order.objects.get(out_trade_no=out_trade_no)
             if order.order_status != 1:
                 order.order_status = 1
                 order.save()
                 return Response('success')  # 必须返回success字符串,8次异步回调机制
         except:
             pass
     return Response('failed')
Example #4
0
    def __worker_changed_update_routepools(self):
        less_coords: bool = False
        if not self._is_started:
            return True
        temp_coordplist: List[Location] = []
        if self.mode in ("iv_mitm", "idle"):
            logger.info('Not updating routepools in iv_mitm mode')
            return True
        with self._manager_mutex:
            logger.debug("Updating all routepools")
            if len(self._workers_registered) == 0:
                logger.info(
                    "No registered workers, aborting __worker_changed_update_routepools..."
                )
                return False

            if len(self._workers_registered) > len(
                    self._current_route_round_coords):
                less_coords = True
                new_subroute_length = len(self._current_route_round_coords)
            else:
                try:
                    new_subroute_length = math.floor(
                        len(self._current_route_round_coords) /
                        len(self._workers_registered))
                    if new_subroute_length == 0:
                        return False
                except Exception as e:
                    logger.info('Something happens with the worker - breakup')
                    return False
            i: int = 0
            temp_total_round: collections.deque = collections.deque(
                self._current_route_round_coords)

            # we want to order the dict by the time's we added the workers to the areas
            # we first need to build a list of tuples with only origin, time_added
            logger.debug("Checking routepool: {}", self._routepool)
            reduced_routepools = [(origin, self._routepool[origin].time_added)
                                  for origin in self._routepool]
            sorted_routepools = sorted(reduced_routepools, key=itemgetter(1))

            logger.debug("Checking routepools in the following order: {}",
                         sorted_routepools)
            compare = lambda x, y: collections.Counter(
                x) == collections.Counter(y)
            for origin, time_added in sorted_routepools:
                entry: RoutePoolEntry = self._routepool[origin]
                logger.debug("Checking subroute of {}", origin)
                # let's assume a worker has already been removed or added to the dict (keys)...

                new_subroute: List[Location] = []
                j: int = 0
                while len(temp_total_round) > 0 and (
                        j <= new_subroute_length or i == len(self._routepool)):
                    j += 1
                    new_subroute.append(temp_total_round.popleft())

                i += 1
                if len(entry.subroute) == 0:
                    logger.debug(
                        "{}'s subroute is empty, assuming he has freshly registered and desperately needs a "
                        "queue", origin)
                    # worker is freshly registering, pass him his fair share
                    entry.subroute = new_subroute
                    # let's clean the queue just to make sure
                    entry.queue.clear()
                elif len(new_subroute) == len(entry.subroute):
                    logger.debug(
                        "{}'s subroute is as long as the old one, we will assume it hasn't changed (for now)",
                        origin)
                    # apparently nothing changed
                    if compare(new_subroute, entry.subroute):
                        logger.info("Apparently no changes in subroutes...")
                    else:
                        logger.critical(
                            "Subroute of {} has changed. Replacing entirely",
                            origin)
                        # TODO: what now?
                        logger.debug('new_subroute: {}', new_subroute)
                        logger.debug('entry.subroute: {}', entry.subroute)
                        logger.debug('new_subroute == entry.subroute: {}',
                                     new_subroute == entry.subroute)
                        entry.subroute = new_subroute
                elif len(new_subroute) == 0:
                    logger.info("New subroute of {} is empty...", origin)
                    entry.subroute = new_subroute
                elif len(entry.subroute) > len(new_subroute) > 0:
                    logger.debug(
                        "{}'s subroute is longer than it should be now (maybe a worker has been "
                        "added)", origin)
                    # we apparently have added at least a worker...
                    #   1) reduce the start of the current queue to start of new route
                    #   2) append the coords missing (check end of old routelength, add/remove from there on compared
                    #      to new)
                    old_queue: collections.deque = collections.deque(
                        entry.queue)
                    while len(old_queue) > 0 and len(
                            new_subroute) > 0 and old_queue.popleft(
                            ) != new_subroute[0]:
                        pass

                    if len(old_queue) == 0:
                        logger.debug(
                            "{}'s queue is empty, we can just pass him the new subroute",
                            origin)
                        # just set new route...
                        entry.queue = collections.deque()
                        for location in new_subroute:
                            entry.queue.append(location)
                    else:
                        # we now are at a point where we need to also check the end of the old queue and
                        # append possibly missing coords to it
                        logger.debug(
                            "Checking if the last element of the old queue is present in new subroute"
                        )
                        last_el_old_q: Location = old_queue[len(old_queue) - 1]
                        if last_el_old_q in new_subroute:
                            # we have the last element in the old subroute, we can actually append stuff with the
                            # diff to the new route
                            logger.debug(
                                "Last element of old queue is present in new subroute, appending the rest of "
                                "the new subroute to the queue")
                            new_subroute_copy = collections.deque(new_subroute)
                            while len(new_subroute_copy
                                      ) > 0 and new_subroute_copy.popleft(
                                      ) != last_el_old_q:
                                pass
                            logger.debug(
                                "Length of subroute to be extended by {}",
                                len(new_subroute_copy))
                            # replace queue with old_queue
                            entry.queue.clear()
                            entry.queue = old_queue
                            while len(new_subroute_copy) > 0:
                                entry.queue.append(new_subroute_copy.popleft())
                        else:
                            # clear old route and replace with new_subroute
                            # maybe the worker jumps a wider distance
                            entry.queue.clear()
                            new_subroute_copy = collections.deque(new_subroute)
                            while len(new_subroute_copy) > 0:
                                entry.queue.append(new_subroute_copy.popleft())

                elif len(new_subroute) > len(entry.subroute) > 0:
                    #   old routelength < new len(route)/n:
                    #   we have likely removed a worker and need to redistribute
                    #   1) fetch start and end of old queue
                    #   2) we sorta ignore start/what's been visited so far
                    #   3) if the end is not part of the new route, check for the last coord of the current route
                    #   still in
                    #   the new route, remove the old rest of it (or just fetch the first coord of the next subroute and
                    #   remove the coords of that coord onward)
                    logger.debug(
                        "A worker has apparently been removed from the routepool"
                    )
                    last_el_old_route: Location = entry.subroute[
                        len(entry.subroute) - 1]
                    old_queue_list: List[Location] = list(entry.queue)
                    old_queue: collections.deque = collections.deque(
                        entry.queue)

                    last_el_new_route: Location = new_subroute[
                        len(new_subroute) - 1]
                    # check last element of new subroute:
                    if last_el_new_route is not None and last_el_new_route in old_queue_list:
                        # if in current queue, remove from end of new subroute to end of old queue
                        logger.debug(
                            "Last element of new subroute is in old queue, removing everything after that "
                            "element")
                        del old_queue_list[old_queue.index(last_el_new_route):
                                           len(old_queue_list) - 1]
                    elif last_el_old_route in new_subroute:
                        # append from end of queue (compared to new subroute) to end of new subroute
                        logger.debug(
                            "Last element of old queue in new subroute, appending everything afterwards"
                        )
                        missing_new_route_part: List[
                            Location] = new_subroute.copy()
                        del missing_new_route_part[0:new_subroute.
                                                   index(last_el_old_route)]
                        old_queue_list.extend(missing_new_route_part)

                    entry.queue = collections.deque()
                    [entry.queue.append(i) for i in old_queue_list]

                if len(entry.queue) == 0:
                    [entry.queue.append(i) for i in new_subroute]
                # don't forget to update the subroute ;)
                entry.subroute = new_subroute

                if less_coords:
                    new_subroute_length = 0

            logger.debug("Done updating subroutes")
            return True