Exemple #1
0
def handle_failed_ride(sender, signal_type, ride, status, **kwargs):
    from ordering.enums import RideStatus
    from ordering.models import FAILED
    from fleet.fleet_manager import cancel_ride
    from sharing.station_controller import send_ride_in_risk_notification
    from notification.api import notify_passenger

    if ride.status == RideStatus.FAILED:
        logging.info("handling FAILED ride: %s" % ride.id)

        current_lang = translation.get_language()
        # cancel ride
        cancel_ride(ride)

        # notify us
        send_ride_in_risk_notification(u"Ride failed because it was not accepted in time", ride.id)

        # cancel orders and notify passengers
        for order in ride.orders.all():
            order.change_status(new_status=FAILED)
            translation.activate(order.language_code)

            notify_passenger(order.passenger, _("We're sorry but we couldn't find a taxi for you this time. (Order: %s)") % order.id)

        translation.activate(current_lang)
def dispatching_cron(request):
    from sharing.station_controller import send_ride_in_risk_notification

    rides_to_dispatch = SharedRide.objects.filter(
        debug=settings.DEV,
        status=RideStatus.PENDING,
        depart_time__lte=default_tz_now() + DISPATCHING_TIME)
    logging.info(u"cron: dispatch rides: %s" % rides_to_dispatch)
    for ride in rides_to_dispatch:
        deferred.defer(dispatch_ride, ride)

    rides_to_monitor = SharedRide.objects.filter(
        debug=settings.DEV,
        depart_time__gte=default_tz_now() - START_MONITORING_TIME,
        depart_time__lte=default_tz_now() + STOP_MONITORING_TIME,
        status__in=[
            RideStatus.PROCESSING, RideStatus.ACCEPTED, RideStatus.PENDING,
            RideStatus.ASSIGNED, RideStatus.VIEWED
        ])
    logging.info(u"cron: monitored rides: %s" % rides_to_monitor)
    for ride in rides_to_monitor:
        if default_tz_now(
        ) - ride.depart_time >= MUST_ACCEPT_TIME and ride.status != RideStatus.ACCEPTED:
            ride.change_status(new_status=RideStatus.FAILED)
        elif ride.depart_time <= default_tz_now(
        ) + SHOULD_ACCEPT_TIME and ride.status != RideStatus.ACCEPTED:
            send_ride_in_risk_notification(u"Ride was not accepted by station",
                                           ride.id)
        elif ride.depart_time <= default_tz_now(
        ) + SHOULD_VIEW_TIME and ride.status not in [
                RideStatus.VIEWED, RideStatus.ACCEPTED
        ]:
            send_ride_in_risk_notification(
                u"Ride was not viewed by dispatcher", ride.id)

    rides_to_complete = SharedRide.objects.filter(
        debug=settings.DEV,
        status=RideStatus.ACCEPTED,
        depart_time__lte=default_tz_now() - MARK_COMPLETE_TIME)
    for ride in rides_to_complete:
        if not ride.change_status(old_status=RideStatus.ACCEPTED,
                                  new_status=RideStatus.COMPLETED):
            logging.error(u"ride [%s] was not marked COMPLETED" % ride.id)

    return HttpResponse("OK")
def dispatching_cron(request):
    from sharing.station_controller import send_ride_in_risk_notification

    rides_to_dispatch = SharedRide.objects.filter(debug=settings.DEV, status=RideStatus.PENDING, depart_time__lte=default_tz_now() + DISPATCHING_TIME)
    logging.info(u"cron: dispatch rides: %s" % rides_to_dispatch)
    for ride in rides_to_dispatch:
        deferred.defer(dispatch_ride, ride)

    rides_to_monitor = SharedRide.objects.filter(debug=settings.DEV, depart_time__gte=default_tz_now() - START_MONITORING_TIME, depart_time__lte=default_tz_now() + STOP_MONITORING_TIME, status__in=[RideStatus.PROCESSING, RideStatus.ACCEPTED, RideStatus.PENDING, RideStatus.ASSIGNED, RideStatus.VIEWED])
    logging.info(u"cron: monitored rides: %s" % rides_to_monitor)
    for ride in rides_to_monitor:
        if default_tz_now() - ride.depart_time >= MUST_ACCEPT_TIME and ride.status != RideStatus.ACCEPTED:
            ride.change_status(new_status=RideStatus.FAILED)
        elif ride.depart_time <= default_tz_now() + SHOULD_ACCEPT_TIME and ride.status != RideStatus.ACCEPTED:
            send_ride_in_risk_notification(u"Ride was not accepted by station", ride.id)
        elif ride.depart_time <= default_tz_now() + SHOULD_VIEW_TIME and ride.status not in [RideStatus.VIEWED, RideStatus.ACCEPTED]:
            send_ride_in_risk_notification(u"Ride was not viewed by dispatcher", ride.id)

    rides_to_complete = SharedRide.objects.filter(debug=settings.DEV, status=RideStatus.ACCEPTED, depart_time__lte=default_tz_now() - MARK_COMPLETE_TIME)
    for ride in rides_to_complete:
        if not ride.change_status(old_status=RideStatus.ACCEPTED, new_status=RideStatus.COMPLETED):
            logging.error(u"ride [%s] was not marked COMPLETED" % ride.id)

    return HttpResponse("OK")
                    update_ride_add_order(ride, ride_data, order)
                    ride.unlock()

                except Exception, e:
                    #TODO_WB: handle this error in some way - try again, create a new ride
                    logging.error(traceback.format_exc())
                    ride.unlock()

                    raise Exception(e.message)
            else:
                logging.info("ride lock failed: creating a new ride")
                ride = create_shared_ride_for_order(ride_data, order)

    except Exception, e:
        logging.error(traceback.format_exc())
        send_ride_in_risk_notification("Failed during post billing processing: %s" % e.message, ride_id)

    if discount_data.promo_code:
        promo_activation = PromoCodeActivation.objects.get(passenger=order.passenger, promo_code=discount_data.promo_code)
        promo_activation.redeem()

def create_shared_ride_for_order(ride_data, order):
    logging.info(u"creating shared ride from ride_data %s" % ride_data)

    ride = SharedRide()
    ride.debug = order.debug
    ride.depart_time = order.depart_time
    ride.arrive_time = order.depart_time + datetime.timedelta(seconds=ride_data.duration)
    ride.distance = ride_data.distance
    ride.cost_data =  ride_data.cost_data
    if order.type == OrderType.PRIVATE:
    if not ride.change_status(old_status=RideStatus.PENDING, new_status=RideStatus.PROCESSING):
        logging.warning(u"Ride dispatched twice: %s" % ride.id)
        return # nothing to do.

    try:
        station = assign_ride(ride)
    except Exception, e:
        logging.error("assign ride raised exception: %s" % traceback.format_exc())
        station = None

    if not station:
        from sharing.station_controller import send_ride_in_risk_notification

        #TODO_WB: how do we handle this? cancel the orders?
        ride.change_status(old_status=RideStatus.PROCESSING, new_status=RideStatus.PENDING)
        send_ride_in_risk_notification(u"No station found for ride: %s" % ride.id, ride.id)


def assign_ride(ride, force_station=None):
    station = force_station or choose_station(ride)
    assigned_station = None

    logging.info(u"assigning ride [%s] to station: %s" % (ride.id, station))
    if station:
        try:
            old_station = ride.station
            ride.station = station
            ride.dn_fleet_manager_id = station.fleet_manager_id
            if old_station:
                remove_ride(old_station, ride)
        return  # nothing to do.

    try:
        station = assign_ride(ride)
    except Exception, e:
        logging.error("assign ride raised exception: %s" %
                      traceback.format_exc())
        station = None

    if not station:
        from sharing.station_controller import send_ride_in_risk_notification

        #TODO_WB: how do we handle this? cancel the orders?
        ride.change_status(old_status=RideStatus.PROCESSING,
                           new_status=RideStatus.PENDING)
        send_ride_in_risk_notification(
            u"No station found for ride: %s" % ride.id, ride.id)


def assign_ride(ride, force_station=None):
    station = force_station or choose_station(ride)
    assigned_station = None

    logging.info(u"assigning ride [%s] to station: %s" % (ride.id, station))
    if station:
        try:
            old_station = ride.station
            ride.station = station
            ride.dn_fleet_manager_id = station.fleet_manager_id
            if old_station:
                remove_ride(old_station, ride)